Hi,
I have an assignment for my Unix class to write a program asking a user to enter a number. the user then chooses an option from a menu of whether they want to count down to zero from the number they entered, or count up from zero to the number. The error i keep getting is binary operator expected for lines 9 and 14 , than when i change that it says unary operator expected. Also I want to know if I coded this whole thing corretly. any ideas?
echo "Enter a number"
read $number
echo "What do you want to do to this number?"
echo "Enter d, to count down to zero"
echo "Enter u, to count up from zero"
read choice
case $choice in
d) while [ "$number" \>= "0" ]
do
echo $number
$number = `expr $number - 1`
done;;
u) while [ "0" \<= "$number" ]
do
echo $number
$number = `expr $number + 1`
done;;
esac
echo "bye"
~
It looks like you might be hitting problems with some of your variables not getting set correctly. To debug, try adding an echo line for each variable just before you go to use it to check everything is as it should be.
Also, check that you are using the right comparison operator, I'm pretty sure = works for numbers, not quoted strings.
Rules of unix.com prevent very explicit help on classwork but I'm sure we can help you on useful debugging steps.
Hi, Thank you Smiling Dragon. i fixed the problems with not having the dollar sign in front of the choice variable. however, now i can run the script and I get no errors, but nothing outputs except the "bye" at the end. I know you cant give me the answer, but would you know how to point in the right direction?
As Smiling Dragon mentioned it's against the forum's rules posting homework..., but at least you have put some effort:
echo "Enter a number"
read number
echo "What do you want to do to this number?"
echo "Enter d, to count down to zero"
echo "Enter u, to count up from zero"
read choice
case $choice in
d) while [ "$number" -ge 0 ]
do
echo $number
number=`expr $number - 1` # no spaces here before/after = sign
done;;
u) while [ 0 -le "$number" ]
do
echo $number
number=`expr $number + 1`
done;;
esac
echo "bye"
~