0
#! /bin/bash
clear
sum=0
i="y"

echo " Enter one no."
read n1
echo "Enter second no."
read n2
while [ $i = "y" ]; do
    echo "1.Addition"
    echo "2.Subtraction"
    echo "3.Multiplication"
    echo "4.Division"
    echo "Enter your choice"
    read ch
    case $ch in
    1)
        sum=$(expr $n1 + $n2)
        echo "Sum ="$sum
        ;;
    2)
        sum=$(expr $n1 - $n2)
        echo "Sub = "$sum
        ;;
    3)
        sum=$(expr $n1 \* $n2)
        echo "Mul = "$sum
        ;;
    4)
        sum=$(expr $n1 / $n2)
        echo "Div = "$sum
        ;;
    *) echo "Invalid choice" ;;
    esac
    echo "Do u want to continue (y/n)) ?"
    read i
    if [ $i != "y" || $i != "Y" ]; then
        exit
    fi
done

I'm brand new to scripting and I can't seem to figure out how to allow the user input to accept anything more then one variable.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • `while [[ $i = [Yy] ]]; do` – Charles Duffy Nov 23 '20 at 20:40
  • BTW, `[ $i != "y" || $i != "Y" ]` is almost always false, because the command `[ $i != "y"` is always false (because it's a `test` syntax error: starting `[` but no trailing `]`); and `$i != "Y" ]` is _also_ false, unless `$i` is something with a truthy exit status when run, like `:` or `true`. You can use `||` as syntax inside `[[ ]]`, but not inside `[ ]`. – Charles Duffy Nov 23 '20 at 20:40
  • ...it's also bad logic (not in a bash sense, but in _every_ language). For any two values X and Y, if they're not identical to each other, `variable != X || variable != Y` is always true. (If it's X, then the `variable != Y` side is true; if it's Y, `variable != X` is true). – Charles Duffy Nov 23 '20 at 20:43
  • ...I assume what you _meant_ is `if [ "$i" != y ] && [ "$i" != Y ]; then ...`, which works fine. (Notice that the quotes are needed around the parameter expansions; they _aren't_ needed around constants that don't have any characters that might be wrongly interpreted as syntax). – Charles Duffy Nov 23 '20 at 20:44
  • Also, note that `expr` should be replaced with `sum=$(( n1 + n2 ))`, which is the POSIX-specified syntax for arithmetic in standard-compliant shells. `expr` is an external command, so running it requires starting up a whole new executable; it's _far_ slower than using shell-builtin functionality. – Charles Duffy Nov 23 '20 at 21:08
  • And keep your parameter expansions in your quotes. `echo "Sum = $sum"` -- that way you're quoting the whole thing, _including_ the expansion. Consider running your code through http://shellcheck.net/ and following the wiki links in its warnings. – Charles Duffy Nov 23 '20 at 21:10

0 Answers0