Erik-joix
2004-05-31, 11:41 AM PDT
I have this in my shell script.
read -p "Number ? " choice ; tput cuu1
if [ $choice = 1 ] && [ $choice1 = 0 ] ; then
while [ "$target" != 2 ] ; do
When i just hit "Enter" i get following message.
./menu.sh: line 623: [: =: unary operator expected
This doesn't prevent the script from running but i would like the script to handle this kind of input better. How could i achieve that?
wolveso
2004-05-31, 05:33 PM PDT
Placing quotes around things removes the "unary operator expected" message when just pressing enter..
read -p "Number ? " choice ; tput cuu1
if [ "$choice" = "1" ] && [ "$choice1" = "0" ] ; then
while [ "$target" != "2" ] ; do
Hope this helps?!
Erik-joix
2004-05-31, 09:17 PM PDT
Yep, that's the fix.
Thanks wolveso!
BTW: What's the "policy" on these quotation marks? In the books i use, people sometimes use them and sometimes don't. They're obviously important but i can't seem to find any words about their importance and when to use them and when not.
crackers
2004-05-31, 09:44 PM PDT
If you read "man test" you'll note that the "=" comparison operator is for strings, while "-eq" is used for numerical comparisons.
If you want to get really fancy, you can also write your code as (not tested, so try at your own risk):
[ $choice -eq 1 -a $choice1 -eq 0 ] && while [ $target -ne 2 ] ; do
But it's not as easily readable, as you can see... :D
Erik-joix
2004-06-01, 12:54 AM PDT
Well i tried this.
read -p "Number ? " choice ; tput cuu1
if [ $choice -eq 1 ] && [ $choice1 -eq 0 ] ; then
while [ $target -ne 2 ] ; do
The script also worked fine but i also got that error message again. I then put quotation marks around it but the error message just changed to something like "integer expected". Well i have an idea what causes this but don't really want to hunt that problem down since the script works fine with wolveso's suggestion. Thanks crackers! Man test helped anyway.
theurge
2004-06-06, 11:59 AM PDT
Another solution is to enclose the test condition inside of double paranthesis, such as:
read -p "Number ? " choice ; tput cuu1
if (( choice = 1 && choice1 = 0 )) ; then
while (( target != 2 )) ; do
This also saves you from having to precede your variable names with the $, if that bothers you. :)
bradthemad
2004-06-07, 12:35 AM PDT
The "policy" on quotation marks is to put them around any variable that might be empty. That way, after variables have been expanded, there will at least be a set of empty quotes to denote a null value, rather than absolutely nothing, generating a syntax error.
# Script without quotes:
if [ $choice = 1 ] ;
# after variable expansion, when $choice is null:
if [ = 1 ] ;
# error!
# Script with quotes:
if [ "$choice" = 1 ] ;
# after variable expansion, $choice is null:
if [ "" = 1 ] ;
# syntax is okay, test fails since "" does not equal 1
Your integer comparison fails when $choice is empty because it is expecting an integer to compare to an integer, and can't compare a blank space to an integer. Considering you're looking at user input, you should probably stick to string (unary) comparison, since a user could type in anything and cause the script to error out.
It helps to put a "set -x" before a section of script that's giving you problems.