1

The program works fine in case " + " ," - "," /"

Example :

./calculate.sh 1 + 2 ==> output is 3.

But the problem here is when I type :

./calculate.sh 1 * 2 ==>output is "Please ..."

I've tried "\*" like this but it seem the variable $2 can not get what I want.

Anyone can help me what is the problem here ?

#!/bin/sh
if [ $# -ne 3 ]
 then
      echo "Please input a number , an operator + or - or * or /"
 else
      case "$2" in 
            "+") echo "Sum is `expr $1 + $3`";;
            "-") echo "Substraction is `expr $1 - $3`";;
            "*") echo "Product is `expr $1 \* $3`";;
            "/") echo "Division is `expr $1 / $3`";;
      esac
fi
Barmar
  • 741,623
  • 53
  • 500
  • 612
vietanh vu
  • 27
  • 1
  • 6
  • It certainly does work with `./calculate.sh 1 '*' 2`. Please provide details of **exactly** what misbehavior you're seeing when quoting the argument; otherwise, this is CNR (cannot reproduce). – Charles Duffy May 18 '16 at 16:22
  • that said, `expr` is ancient -- the modern POSIX sh way to do math is `$(( $1 * $3 ))`, which is much more efficient (doesn't involve forking off a subshell). – Charles Duffy May 18 '16 at 16:23
  • (as another aside -- don't use a `.sh` extension on an executable script; executable scripts define commands, and commands don't have extensions -- you run `ls`, not `ls.elf`. Extensions are appropriate for shell libraries intended to be sourced, not scripts intended to be executed; see also https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/). – Charles Duffy May 18 '16 at 16:26
  • Did you try `\*` or `"\*"`? If you used quotes **and** backslash, the backslash will stay in the string so it won't match any of the cases. – Barmar May 18 '16 at 16:30
  • @Barmar, ...that's dependent on whether the quotes were intended to be literal or StackOverflow formatting. You've edited the question clarifying the ambiguity in favor of the latter interpretation, but I'd call it rash to make proclamations with any level of certainty until that assumption is verified by the OP. – Charles Duffy May 18 '16 at 16:40
  • @CharlesDuffy thank you. I'm new in this section.Really helpful information. – vietanh vu May 18 '16 at 17:00
  • @Barmar I use it in the in "\*") but it not work as well – vietanh vu May 18 '16 at 17:01
  • @vietanhvu, so you mean you only tried that in the case statement, not on the command line during invocation? It needs to be quoted **on the command line** by the user starting the script, or the `*` will be replaced with a list of filenames before your script is even started. – Charles Duffy May 18 '16 at 17:01
  • @CharlesDuffy That's why I asked him what he did. – Barmar May 18 '16 at 17:05
  • @CharlesDuffy My desire is when i type ./calculate.sh 1 * 2 , the output will be "Product is 2" , since I was new in this kind of programming, Sorry for really not understand everything you said. It causes me so confuse when I search and find in this tutorialspoint.com/unix/unix-basic-operators.htm , but it not work as I see. I have called in the other program calculate only product of 2 variable, and it has to be like this : `expr $1 \* $2` – vietanh vu May 18 '16 at 17:24
  • Making `./calculate 1 * 2` work is impossible, for the reason I described: Glob expansion (that is, replacing a `*` with a list of files) happens **before your script is started**. Thus, there's no way to prevent it from within that script: The `*` is already gone, and a bunch of filenames are in its place. – Charles Duffy May 18 '16 at 17:25
  • If that's your real question, it's duplicative of http://stackoverflow.com/questions/11456403/stop-shell-wildcard-character-expansion, http://stackoverflow.com/questions/2755795/how-do-i-pass-in-the-asterisk-character-in-bash-as-arguments-to-my-c-program, http://stackoverflow.com/questions/2718873/problem-of-in-command-line-argument, and more. – Charles Duffy May 18 '16 at 17:27

1 Answers1

0

You should use it like this

./calculate.sh 2 \* 3

Or

./calculate.sh 2 "*" 3

Or

./calculate.sh 2 '*' 3

You are using "\*" which is wrong.

khrm
  • 5,363
  • 1
  • 22
  • 26
  • The OP claims that they *are* using it with `"*"` (precisely equivalent to `\*` for our purposes) and it still fails. See fourth paragraph of the question. – Charles Duffy May 18 '16 at 16:24
  • @CharlesDuffy Yeah. Just re-read. Everything is working for me in ksh,zsh and bash, – khrm May 18 '16 at 16:26
  • @CharlesDuffy He said he tried `"\*"`. If he's using quotes and backslash it won't work -- it should just be one or the other. – Barmar May 18 '16 at 16:30
  • @Barmar, he said "\\*" -- if it'd been `"\*"`, that would have been much more clear in terms of whether the quotes were intended to be reader instructions or literal (well, to the shell, syntactical) content. – Charles Duffy May 18 '16 at 16:41
  • @CharlesDuffy I know, but it was obvious that he doesn't know how to do highlighting in the SO editor (I had to fix all of it). Since it didn't work for him, I think it's reasonable to assume that he didn't do the escaping or quoting correctly. – Barmar May 18 '16 at 17:04