Search code examples
linuxbashubuntuterminal

Please help me out on this:Whitespace madness on Ubuntu's .sh files


I have this code:

i=0
while [ $i -lt 100 ]
do
if [ $((i%3)) -equ 0 ] ; then
((i=i+1))
continue
fi
echo $i
((i=i+1))
done
#end

After I have compiled it is looping this message 100 times:

c2.sh: 4: [: 0: unexpected operator
0

I dont know where I am wrong tho maybe there are not enough/too many whitespaces? I dont know someone help me this Linux terminal case sensitivity is driving me nuts!


Solution

  • I've put you original script into a test.sh file and made the subtle change to -equ

    #/bin/bash
    
    i=0
    echo $i
    
    while [ $i -lt 100 ]
    do  
        if [ $(($i%3)) -eq 0 ] ; then
            ((i=$i+1))
            continue
        fi
        echo $i
        ((i=$i+1))
    done
    

    Some notes:

    1. You need the following line at the top:

      #/bin/bash

    2. You need to make the file executable by issuing the following:

      chmod +x

    I think the above is what you were looking for?