zeus_masta_funk zeus_masta_funk - 3 months ago 7
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:

#!/bin/sh

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

i=0

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

printf "\n"

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


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

You need spaces on your [] condition:

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

and

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

instead of

if [$2 -gt 1000]     #line 19

and

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.