case construction for basic Arithmetics calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting case construction for basic Arithmetics calculation
# 1  
Old 11-27-2010
case construction for basic Arithmetics calculation

the scrip (q4.sh) should perform the following calcuation (+, -, / and *) it should be used like this:
q4.sh number1 operation number2

I wrote it already but the "*" does not work.
Code:
#!/bin/bash
#Date: 2010.10.19
# un script qui utilisera une instruction case pour effectuer des opérations arithméties :
#+ addition
#- subtraction
#x multiplication
#/ division
#Le nom du script doit etre 'q4' et il s'utilisera comme suit :
#$ ./q4 20 / 3


case $2 in "+" )
   echo "`expr $1 + $3`" ;;
"-" )
   echo "`expr $1 - $3`" ;;
"*" )
   echo "`expr $1 * $3`" ;;
"/" )
   echo "`expr $1 / $3`" ;;
esac

#END

Please give me your feedbacks how to improve it. and fix the "*". Only bash.
BR.
# 2  
Old 11-27-2010
Have you tried calling the script like this
Code:
./q4 2 \* 2

# 3  
Old 11-27-2010
Quote:
Originally Posted by Scrutinizer
Have you tried calling the script like this
Code:
./q4 2 \* 2

Just tested but it returns an error

Code:
alioune@baccus ~/bashtuto/bashExercise $ ./q4.sh 2 \* 2
expr: syntax error

alioune@baccus ~/bashtuto/bashExercise $

# 4  
Old 11-27-2010
Try:
Code:
echo "`expr $1 \* $3`" ;;

# 5  
Old 11-27-2010
Quote:
Originally Posted by Scrutinizer
Try:
Code:
echo "`expr $1 \* $3`" ;;

file modified
Code:
case $2 in "+" )
   echo "`expr $1 + $3`" ;;
"-" )
   echo "`expr $1 - $3`" ;;
"*" )
   echo "`expr $1 \* $3`" ;;
"/" )
   echo "`expr $1 / $3`" ;;
esac

test
Code:
alioune@baccus ~/bashtuto/bashExercise $ ./q4.sh 2 + 2
4
alioune@baccus ~/bashtuto/bashExercise $ ./q4.sh 2 - 2
0
alioune@baccus ~/bashtuto/bashExercise $ ./q4.sh 2 / 2
1
alioune@baccus ~/bashtuto/bashExercise $ ./q4.sh 2 * 2
alioune@baccus ~/bashtuto/bashExercise $ 
alioune@baccus ~/bashtuto/bashExercise $

Still no output for "*".
# 6  
Old 11-27-2010
You need to call it like this:
Code:
./q4.sh 2 \* 2

---------- Post updated at 11:53 ---------- Previous update was at 11:45 ----------

You need to always escape the * on the command line to avoid expansion by the shell.

In the script, instead of using expr you can do this:
Code:
case $2 in
  +)  echo "$(( $1 + $3 ))" ;;
  -)  echo "$(( $1 - $3 ))" ;;
  \*) echo "$(( $1 * $3 ))" ;;
  /)  echo "$(( $1 / $3 ))" ;;
esac

then you do not need the inside the arithmetic expression and you are using a shell builtin, which is faster.

In this case you could even do this:
Code:
echo "$(($@))"

but that is probably not what you are after Smilie
# 7  
Old 11-27-2010
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command construction

Hi, I have an array variable "arr" that reads string from a file "vari.txt". Thus, the array will be of variable length depending how many entries are present in "vari.txt" I use a for loop to traverse through the array. However, i need a grep command to grep for a file "output.txt" and... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Arithmetics in if else statement

hello, i'm trying to get some deeper view into the shell. at the moment I have following problem with the bash: I read some numbers via some awk scripts A=`awk '/id="identifier/ {......}` which works fine, then I want to make an if statement whth some calculations. but then it does... (4 Replies)
Discussion started by: freeze
4 Replies

3. Web Development

Website construction nowaday!

Hello, Actually I am not sure how to ask this question, which is about the technique advance on website construction/design. Compared with HTML/CSS, which ones are the most popular for website construction? My colleague recommends Drupal for Linux platform. Any other? Thanks! Yifang (4 Replies)
Discussion started by: yifangt
4 Replies

4. Shell Programming and Scripting

Help with regex construction

I am trying to construct a regular expression that will filter a log file containing the following table: aP mP mC,mI oP oC oF oI tP tI hP ... (8 Replies)
Discussion started by: euval
8 Replies

5. Shell Programming and Scripting

Matrix construction

Hi I have to construct a rectangular matrix with 6012 rows and 2221 columns. Here the rows and columns were given by alphanumeric ids in a file named row.txt and column.txt respectively. with this row nd column ids I have to construct a matrix ie 6012*2221 and compare the column ids with... (0 Replies)
Discussion started by: riyabio
0 Replies

6. Shell Programming and Scripting

Matrix construction

Hi experts How to construct a rectangular matrix for a text file with 6012 rows and 2221 columns. Thank You (1 Reply)
Discussion started by: riyabio
1 Replies

7. Cybersecurity

Password Construction

Could someone please help, I am new to unix and I am trying to do the following: o Each password must have at least eight characters. Only the first eight characters are significant. PASSLENGTH is found in /etc/default/passwd and is set to 6. ... (1 Reply)
Discussion started by: tumbikikani
1 Replies
Login or Register to Ask a Question