View Full Version : unary operator expected

31st May 2004, 07:41 PM
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?

1st June 2004, 01:33 AM
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?!

1st June 2004, 05:17 AM
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.

1st June 2004, 05:44 AM
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

1st June 2004, 08:54 AM
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.

6th June 2004, 07:59 PM
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. :)

7th June 2004, 08:35 AM
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.