Can someone please help....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can someone please help....
# 1  
Old 08-25-2013
Need help with "case statement" and :while loop"..

..with how to use basic arithmetic options in a case state statement and then loop them with say, a "while" loop? Please be gentle as I am definitely a newbie.

Example: I need a very simple option "case statement" for these all together.

Code:
sum=$((num1 + num2))
diff=$((num1 - num2))
prod=$((num1 *\ num2))
quot=$((num1 / num2))

and then "while" looped.

thanks a bunch,

joe.

Last edited by jefferj54; 08-25-2013 at 01:12 PM.. Reason: Please use code tags
# 2  
Old 08-25-2013
Hi, welcome to these forums. Could you elaborate some more on what your problem is and what you are trying to accomplish?

Perhaps something like this may give you some idea:
Code:
echo 'enter "num1" operation "num2" (space separated)'
while read num1 operation num2
do
  case $operation in
    (+)  result=$((num1 + num2)) ;; 
    (-)  result=$((num1 - num2)) ;; 
    (\*) result=$((num1 * num2)) ;; 
    (/)  result=$((num1 / num2)) ;; 
    (*)  result="Invalid input"
  esac
  printf "%s\n" "$result"
done

# 3  
Old 08-26-2013
thank you

Thanks very much for your help.

joe.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question