[SWLUG] si,ple shell in bash not working

Jonathan Wright jonathan at netwrker.co.uk
Fri May 4 11:12:31 UTC 2007


Stephen Constantinou wrote:
> <content starts>
>   choice=5
>   mid=7
>   if [ choice < mid ]
>   then
>    echo " choice is less than mid"
>   else
>    echo " choice is greater than mid"
>   fi
> <content ends>

> I read a very good online tutorial and I am lost as to what I have done 
> wrong.

With bash, all variables are recalled in code using the $ prefix to the 
name, but changes from most programs in that the first time you create a 
  variable, it needs to be done without $ at the start.

So although 'choice=5' and 'mid=7' are fine, the 'if' statement on the 
next line is incorrect, and without the preceding $ in front of the 
variables, the system is treating them as commands:

choice=5
mid=7

if [ $choice < $mid ]; then
   echo " ..."
else
   echo " ..."
fi

-- 
  Jonathan Wright                           jonathan at netwrker.co.uk
                                              http://netwrker.co.uk

  cat /dev/random (you never know, you may see something you like!)

  2.6.20-gentoo-r7-netwrker-b2 AMD Athlon(tm) XP 2100+
  up 4:06, 2 users, load average: 1.34, 0.88, 0.54



More information about the Swlug mailing list