Hi,
I am using korn shell.
until [[ $# -ne 0 ]]
do
echo "\$# = " $#
echo "$1"
shift
done
To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until [[ $# -le 0 ]] then it does work.
Why numeric comparison is not working with -ne and works with -le?
thank you
-ne means not equal to
-le means less than or equal to
If you run the script and do not pass any parameters, it will enter the loop as it currently it is waiting 'until' $# is not equal to 0, which it will be set to if you do not pass any parameters (it will then fail on the shift command).
If you run the script and do pass some parameters, it will not enter the loop because $# is already not equal to 0, so it has nothing to wait 'until' for.
I hope that has made some sense.
It is a matter of logic. Until the number of parameters is not equal to 0. If the number of parameters is 2 then it will not enter the loop, since the condition is satisfied ( 2 is unequal to 0 ).
You probably want a while
loop rather than an until
loop:
while [[ $# -ne 0 ]]
do
echo "\$# = " $#
echo "$1"
shift
done
It can also be written: while (($#))
Or:
until (( $# == 0 ))