LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-04-2010, 06:35 PM   #1
gazagelen
LQ Newbie
 
Registered: May 2010
Posts: 3

Rep: Reputation: 0
Unhappy Shell Script Calculator


hi guys i m writing a simple calculator and have just one difficulty at my program. here is my code

Code:
echo "choose the operator";
read oper;

if test $oper = +; then
echo "you want to make addition";
echo "first number";
read num1;
echo "second number";
read num2;
expr $num1 + $num2;

elif test $oper = -; then
echo "you want to make subtraction";
echo "first number";
read num1;
echo "second number";
read num2;
expr $num1 - $num2;

elif test $oper = *; then
echo "you want to make multiplication";
echo "first number";
read num1;
echo "second number";
read num2;
expr $num1 \* $num2;

elif test $oper = /; then
echo "you want to make diversion";
echo "first number";
read num1;
echo "second number";
read num2;
expr $num1 / $num2;
fi

exit 0;
the thing is program run almost perfect but when i want to use * operator it gave me an error.
"hm.sh: line 3: test: too many arguments"

can you guys help me with this problem?
 
Old 05-04-2010, 07:19 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Almost certainly, this is happening because * is a wildcard in bash. Try putting it in double or single quotes.

You may have to do some quoting also in the script.
 
Old 05-05-2010, 04:11 AM   #3
gazagelen
LQ Newbie
 
Registered: May 2010
Posts: 3

Original Poster
Rep: Reputation: 0
@pixellany
nope nothing happened i tried to put double or single quote but problem is same. i don't have any problem with other operators by the way
 
Old 05-05-2010, 04:25 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

pixellany is correct, the * is expanded. Try setting this in the beginning of the script (immediately after the hashbang):

set -o noglob

Hope this helps.
 
1 members found this post helpful.
Old 05-05-2010, 05:15 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
A case statement would allow the same to be done with less lines
Code:
#!/bin/bash

echo "choose the operator"
read oper

case $oper in
    '+' )
        echo "you want to make addition"
        ;;
    '-' )
        echo "you want to make subtraction"
        ;;
    '*' )
        echo "you want to make multiplication"
        ;;
    '/' )
        echo "you want to make division"
        ;;
    * )
        echo "invalid operator '$oper'" >&2
        exit 1
esac

echo "first number"
read num1
echo "second number"
read num2

expr "$num1" "$oper" "$num2"

exit 0
Notes:
  1. It is not wrong but not necessary to terminate commands with ";" when they are followed by line end.
  2. It is conventional and useful to send error messages to standard error also known as stderr or stream 2. That is what >&2 does.
  3. The arithmetic operation is division, not diversion.
  4. expr only handles integers. You can ensure that the numbers are integers with code like this
    Code:
    if [[ ! $num1 =~ ^[0-9]+$ ]]; then
        echo "'$num1' is not an integer" >&2
        exit 1
    fi
 
1 members found this post helpful.
Old 05-05-2010, 05:23 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by gazagelen View Post
@pixellany
nope nothing happened i tried to put double or single quote but problem is same. i don't have any problem with other operators by the way
Did you put quotes around both test operands as in
Code:
elif test "$oper" = '*'; then
That should have worked. The test command and its alternate implementation [ ], are more prone to this sort of trouble than the later test facility [[ ]] for reasons explained here so you can get off to a good start learning shell scripting by getting into the habit of using [[ ]] instead of test. What are you using to learn shell scripting? If it advises using the test command you would be better off with something else (unless you want to write scripts to run under older or simpler shells that bash).

Last edited by catkin; 05-05-2010 at 05:24 AM. Reason: speeling as she is wrotten
 
Old 05-05-2010, 07:34 AM   #7
gazagelen
LQ Newbie
 
Registered: May 2010
Posts: 3

Original Poster
Rep: Reputation: 0
@catkin
thank you catkin, that works.i also made a change at my script. i use bc command for float

Code:
answer=$(echo "scale=2;$num1 $oper $num2" |bc);
echo "$answer";
and i also use this type of quote,it didn't work either
Code:
elif test "$oper" = '*'; then
 
Old 05-05-2010, 07:45 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by gazagelen View Post
i also use this type of quote,it didn't work either
Code:
elif test "$oper" = '*'; then
Strange -- works for me
Code:
#!/bin/bash

oper='*'  # Tried changing this
if test "$oper" = '*' ; then
    echo true
else
    echo false
fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Permission bit calculator script ... ??? newbie01.linux Linux - General 3 05-03-2010 05:01 AM
Simple Shell Script Calculator chellemybelle Linux - Newbie 12 11-06-2009 12:44 AM
LXer: bc - The shell maestro’s calculator LXer Syndicated Linux News 0 04-06-2007 09:47 PM
shell calculator DropSig Linux - Newbie 3 11-01-2004 10:18 PM


All times are GMT -5. The time now is 02:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration