LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2011, 02:25 PM   #1
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Rep: Reputation: 0
-gt: unary operator expected


Quote:
echo "enter cost price of article"
read cp
echo "enter selling price of articel "
read sp
a= expr $sp - $cp
if [ $a -gt 0 ]
then
echo "PROFIT! "
echo "profit=" $a
else
echo "LOSS! "
echo "loss=" $(expr $cp - $sp)
fi

I wrote the abovee code to calculte the profit o loss made by the shopkeeper.

I gives error:
Quote:
enter cost price of article
65
enter selling price of articel
89
24
./10da: line 9: [: -gt: unary operator expected
LOSS!
loss= -24
Although it should print profit statement it prints loss statement.

It looks if[] statement is never being read.

Please explain the error.

Last edited by hd_pulse; 05-26-2011 at 03:10 PM.
 
Old 05-26-2011, 02:51 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Hints:
  • Your variable assignment is wonky.
  • It's normally a good idea to quote variables when doing logical tests.
 
Old 05-26-2011, 03:10 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
This is what anomie means.
Code:
a= expr $sp - $cp
There should be no space after the '='. Also read up on command substitution to solve this.
 
1 members found this post helpful.
Old 05-26-2011, 03:12 PM   #4
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
# It's normally a good idea to quote variables when doing logical tests.
Please show to quote in logical tests in the above code.
 
Old 05-26-2011, 03:16 PM   #5
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
There should be no space after the '='. Also read up on command substitution to solve this.
After making the changes (as per ur suggestion ) it adds to the errors.
Quote:
enter cost price of article
43
enter selling price of articel
89
./10da: line 8: 89: command not found
./10da: line 9: [: -gt: unary operator expected
LOSS!
loss= -46
ie
Quote:
./10da: line 8: 89: command not found
another error is added.
 
Old 05-26-2011, 03:27 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by hd_pulse View Post
After making the changes (as per ur suggestion ) it adds to the errors.
Did you use command substitution? Remember that
expr ...

is a command and you want the result to assign to a.
 
Old 05-27-2011, 12:08 AM   #7
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
echo "enter cost price of article"
read cp
echo "enter selling price of articel "
read sp
a=expr $sp - $cp
if [ $a -gt 0 ]
then
echo "PROFIT! "
echo "profit=" $a
else
echo "LOSS! "
echo "loss=" $(expr $cp - $sp)
fi
this is what i wrote and it still gives the same error i.e
Quote:
./10da: line 9: [: -gt: unary operator expected
 
Old 05-27-2011, 01:47 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Code:
a=expr $sp - $cp
You did not as suggested. Read my other post again. You only followed the first advice.

Last edited by crts; 05-27-2011 at 04:27 AM.
 
Old 05-27-2011, 02:25 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
My advice is to use the right tools for the job, remembering also that bash does not do decimal arithmetic:
Code:
echo "enter cost price of article"
read cp
echo "enter selling price of articel "
read sp
(( a = sp - cp ))
if (( a > 0 ))
then
    echo "PROFIT! "
    echo "profit=" $a
else
    echo "LOSS! "
    echo "loss=" $(( cp - sp ))
fi
 
Old 05-27-2011, 04:29 AM   #10
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
What grail said, or use backticks / $() to get a command executed:

a=expr $sp - $cp

is going wrong; you saw this error:
Quote:
./10da: line 8: 89: command not found
which led to the next error; you were not assigning a proper value to the variable; and thus you get a "zero" variable-comparison which is being complained about.

You want to assign the value which is the outcome of the command; Hence, you need to tell the system that it is a command to be executed, like this:

a=`expr $sp - $cp` # using backticks
OR
a=$(expr $sp - $cp) # using $()

as a command execution; then you can do if [ $a -gt 0 ]
 
Old 05-28-2011, 07:57 AM   #11
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Ramurd View Post

a=`expr $sp - $cp` # using backticks
OR
a=$(expr $sp - $cp) # using $()
Thanks Man! It really worked.

Thanks Alot!
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] unary operator expected hd_pulse Programming 8 05-26-2011 11:01 PM
error: unary operator expected ?? Lynda_M Programming 3 11-29-2008 08:03 PM
unary operator expected error! Lynda_M Programming 3 11-29-2008 12:04 PM
unary operator expected MONKEYJUDO Linux - Newbie 1 07-13-2008 06:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration