Fedora Linux Support Community & Resources Center
Sections ›› Home | Forums | Guidelines | Forum Help | Fedora FAQ | Fedora News 

Go Back   FedoraForum.org > The Community Lounge > Programming

Programming A place to discuss programming and development

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 2004-05-31, 12:41 PM CDT
Erik-joix Offline
Registered User
 
Join Date: May 2004
Location: Berlin, Germany
Age: 33
Posts: 32
unary operator expected

I have this in my shell script.

Code:
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?
Reply With Quote
  #2  
Old 2004-05-31, 06:33 PM CDT
wolveso Offline
Registered User
 
Join Date: May 2004
Location: Leeds, UK
Posts: 45
Placing quotes around things removes the "unary operator expected" message when just pressing enter..
Code:
read -p "Number ? " choice ; tput cuu1

    if [ "$choice" = "1" ] && [ "$choice1" = "0" ] ; then
	while [ "$target" != "2" ] ; do
Hope this helps?!
Reply With Quote
  #3  
Old 2004-05-31, 10:17 PM CDT
Erik-joix Offline
Registered User
 
Join Date: May 2004
Location: Berlin, Germany
Age: 33
Posts: 32
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.

Last edited by Erik-joix; 2004-05-31 at 10:26 PM CDT.
Reply With Quote
  #4  
Old 2004-05-31, 10:44 PM CDT
crackers's Avatar
crackers Offline
Retired Community Manager
 
Join Date: Feb 2004
Location: Seattle, WA, USA
Age: 53
Posts: 3,420
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):
Code:
[ $choice -eq 1 -a $choice1 -eq 0 ] && while [ $target -ne 2 ] ; do
But it's not as easily readable, as you can see...
Reply With Quote
  #5  
Old 2004-06-01, 01:54 AM CDT
Erik-joix Offline
Registered User
 
Join Date: May 2004
Location: Berlin, Germany
Age: 33
Posts: 32
Well i tried this.
Code:
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.
Reply With Quote
  #6  
Old 2004-06-06, 12:59 PM CDT
theurge's Avatar
theurge Offline
Registered User
 
Join Date: Mar 2004
Location: Kansas City, USA
Age: 32
Posts: 46
Another solution is to enclose the test condition inside of double paranthesis, such as:

Code:
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.
Reply With Quote
  #7  
Old 2004-06-07, 01:35 AM CDT
bradthemad's Avatar
bradthemad Offline
Registered User
 
Join Date: May 2004
Location: Atlanta, GA
Posts: 101
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.

Code:
# 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.
__________________
?
Reply With Quote
Reply

Tags
expected, operator, unary

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointers & operator overloads PantheraLeo Programming 0 2009-09-02 09:30 PM CDT
What is the operator * ./. . . * royeo General Support 1 2006-09-03 05:13 PM CDT
gcc wont compile code with ?: operator John V. Pope gmane.linux.redhat.fedora.general 6 2004-06-14 04:23 AM CDT
gcc wont compile code with ?: operator John V. Pope gmane.linux.redhat.fedora.general 6 2004-06-14 04:20 AM CDT
gcc wont compile code with ?: operator John V. Pope gmane.linux.redhat.fedora.general 6 2004-06-14 04:17 AM CDT

Automatic Translations (Powered by Powered by Google):
Afrikaans Albanian Arabic Belarusian Bulgarian Catalan Chinese Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician German Greek Hebrew Hindi Hungarian Icelandic Indonesian Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Taiwanese Thai Turkish Ukrainian Vietnamese Yiddish

All times are GMT -7. The time now is 03:27 PM CST.

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
Hosting provided by ThePlanet



All trademarks, and forum posts in this site are property of their respective owner(s).

FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact | Founding Members
Designed By Ewdison Then | Powered by vBulletin ©2000-2009, Jelsoft Enterprises Ltd.
FedoraForum is Powered by Open Source Projects and Products
vB Enterprise Translator (vBET) coded by NLP-er