0

I have a problem at line 14 and 16 where there are the nested if statments. It returns me command not found error, but I tested that part of code and it's ok used elsewhere.

#passing the argument page page.html
cat $1 | grep -o "wp-cli.org/commands/cache/\w*/\"" > temp.txt
#creating all pdf files
for i in $(cat temp.txt)
do 
source=$(echo $i | grep -o -P "wp-cli.org/commands/cache/\w+")
dest=$(echo $i | grep -o -P "\w+/\"" | grep -o -P "\w+")
#echo $source $dest
wkhtmltopdf $source $dest".pdf"
pdfCount=$(ls *pdf | wc -l)
echo $pdfCount
if [ ! -f sample.pdf ]
then
 if [$pdfCount -eq 1] 
 then
    firstPdf=$dest".pdf" 
 fi
 if [$pdfCount -eq 2]
 then
    pdfunite $firstPdf $dest".pdf" sample.pdf
 fi
else
    pdfunite $dest".pdf" oldsample.pdf sample.pdf
    mv sample.pdf oldsample.pdf
fi
done

Anyone has an idea about? Thanks...

6
  • Where did you learn to write this code? Commented Apr 15, 2017 at 15:35
  • I read some books but it's a long time I haven't created scripts, why? Commented Apr 15, 2017 at 15:40
  • 4
    learn to use (and understand the output of) shellcheck.net . As a matter of defensive programming, there are very few cases where you don't want to use dbl-quotes around variables, so "$firstPdf" will save you from some headaches in the future if you start getting filenames like Contract 2017. Good luck. Commented Apr 15, 2017 at 15:43
  • Thanks I need to study more about programming in bash... Commented Apr 15, 2017 at 15:44
  • What do you want achieve? (wondering about the "big picture") :) Too many problematic constructions in the script (grepping html, unquoted variables, UUOC, UUOE, reading lines using for ... cat, etc) - so... just wondering. :) Commented Apr 15, 2017 at 16:24

2 Answers 2

10

You need a space after the [ and before the ]

Sign up to request clarification or add additional context in comments.

1 Comment

... and a space after the if too!
1

You need spaces after the opening bracket and before the closing one in a test statement :

[$pdfCount -eq 1]

Should be :

[ $pdfCount -eq 1 ]

Suppose pdfCOunt contains 1, then the command the shell will be looking for is [$pdfCount, which is [1, and is a command that does not exists, hence the error message you are seeing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.