0

I am new here and hope my question is not so noob. I am learning shell programming. My following script giving me error. I cannot seem to figure out why

#!/bin/bash 
#This is our sixth script switch.

echo "Print two numbers" 
read num1 num2 
echo "What operation do you want to do?" 
operations=’add subtract multiply divide exponentiate modulo all quit’ 
select oper in $operations ; do 
    case $oper in 
        "add" ) echo "$num1 + $num2 =" $[$num1 + $num2] ;; 
        "subtract" ) echo "$num1- $num2 =" $[$num1- $num2] ;; 
        "multiply" ) echo "$num1 * $num2 =" $[$num1 * $num2] ;; 
        "exponentiate" ) echo "$num1 ** $num2 =" $[$num1 ** $num2] ;; 
        "divide" ) echo "$num1 / $num2 =" $[$num1 / $num2] ;;  
        "modulo" ) echo "$num1 % $num2 =" $[$num1 % $num2] ;; 
        "all" ) 
            echo "$num1 + $num2 =" $[$num1 + $num2] 
            echo "$num1- $num2 =" $[$num1- $num2] 
            echo "$num1 * $num2 =" $[$num1 * $num2] echo "$num1 ** $num2 =" $[$num1 ** $num2] 
            echo "$num1 / $num2 =" $[$num1 / $num2] echo "$num1 % $num2 =" $[$num1 % $num2] 
            *) 
            exit ;; 
    esac 
done

When I run it shows

$ ./script6.txt
Print two numbers
22 22
What operation do you want to do?
./script6.txt: line 7: subtract: command not found
./script6.txt: line 21: syntax error near unexpected token `)'
./script6.txt: line 21: `

Thanks in advance

  • `$[ ... ]` has been obsolete since the beginning of the 1990s, when the POSIX.2 standard was published specifying `$(( ... ))` as the correct way to enter an arithmetic context. See also https://wiki.bash-hackers.org/scripting/obsolete – Charles Duffy Jun 19 '22 at 22:56
  • BTW, https://www.shellcheck.net/ can find your problem here automatically. (Well, most of them; it misses that you needed but don't have a `;;` at the end of the `all` block, giving only a generic syntax error instead of pointing out the specific issue) – Charles Duffy Jun 19 '22 at 22:58

1 Answers1

0

You are using the wrong quote character in defining 'operations'. You have ’, and you need ' or ".

Also, as per https://stackoverflow.com/a/41081481/131433, that syntax for arithmetic is at best very old.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • Using "smart quotes" instead of ASCII quotes is a typographic error, no? – Charles Duffy Jun 19 '22 at 22:55
  • Well, the error message strikes me as consistent with having made that typographic error in the script and not just in this SO post. – bmargulies Jun 19 '22 at 23:07
  • I agree. That said, see #2 in https://stackoverflow.com/help/on-topic -- questions _caused by a typographical error_ are off-topic, so the last bullet point (linking to above page) in _Answer Well-Asked Questions_ from [How to Answer](https://stackoverflow.com/help/how-to-answer) applies. – Charles Duffy Jun 19 '22 at 23:20
  • Well, hmm. I think this is a gray area; I think of typos more as mispellings. But if the question gets closed, I won't vote to reopen it. – bmargulies Jun 20 '22 at 16:30
  • Thanks its working now after changing quotes and using proper semicolons – Md Rahman Jun 20 '22 at 21:21