zeus_masta_funk zeus_masta_funk - 1 year ago 41
Linux Question

Bash script won't work: command not found

I'm trying to create a simple shell script to list the first input 6 times, a line, then report the size of the second input. Here is my script:


# script1.sh
# $1=filename $2=number


while [$i -lt 7] #line 11
i=$(($i + 1))
echo $1

printf "\n"

if [$2 -gt 1000] #line 19
echo 'This is a big number!'
echo 'This is a small number.'

Here is the error I receive when trying to use:

./script1.sh test 131234
./script1.sh: line 11: [0: command not found

./script1.sh: line 19: [131234: command not found
This is a small number.

I suppose it partially works but something about the command -lt and -gt is causing an error. Running on both Linux and Terminal (OS X) provide the same error.

Answer Source

You need spaces on your [] condition:

if [ $2 -gt 1000 ]     #line 19
    ^           ^


while [ $i -lt 7 ]    #line 11
       ^        ^

instead of

if [$2 -gt 1000]     #line 19


while [$i -lt 7]    #line 11

Note that otherwise it will not consider these expressions as it should. Instead, it understand them as a string called [$2 or [$i.