2

This is a bash script I made to calculate numbers in Unix:

echo "Please enter the calculation(operation) type (+)(-)(*)(/)"
        read $opr
echo "Enter the first number"
        read $num1
echo "Enter the second number"
        read $num2
if [[ $opr = "+" ]]; then
        num=$(($num1 + $num2))
                echo "The sum is = $num"
elif [[ $opr = "-" ]]; then
        num=$(($num1 - $num2))
                echo "The sum is = $num"
elif [[ $opr = "*" ]]; then
        num=$(($num1 * $num2))
                echo "The sum is = $num"
elif [[ $opr = "/" ]]; then
        num=$(($num1 / $num2))
                echo "The sum is = $num"
fi

It runs, but it outputs "The sum is = ", and doesn't give a number. Can you see anything that is causing this problem?

  • Run the script with bash -x. – muru Dec 13 '15 at 18:13
  • You have no shebang line. How are you running the script? And are you seeing any error output? Also, if you align the lines within the same context on the same line (i.e., put read and echo on the same lines as the preceding line) the script is going to be more readable. – l0b0 Dec 13 '15 at 18:37
  • 3
    Replace all read $ by read. – Cyrus Dec 13 '15 at 19:01
4

It seems the current way you have of obtaining input via the read is wrong so the math is being done on nothing at all, so you end up with nothing for an answer. You simply have to fix this aspect and the rest of your code should work.

How to read

An excerpt from your original posted code, shows that you were trying to save the first number like this:

echo "Enter the first number"
        read $num1

Instead, try using -p for the prompt and name the variable num without dollar sign. You can even test the following read -p ... on your command prompt, so you see:

$ read -p "Enter the first number" num1
Enter the first number:

Now enter 1:

$ read -p "Enter the first number" num1
Enter the first number: 1

Now if you were to echo $num1, you successfully see the value:

$ echo $num1
1
  • -p the_prompt_text is a way to include a prompt
  • compared with echo, echo appends a line return at the end. But since -p does not, it is a good idea to have an extra white-space like I have done, notice after the colon :, I have a space: "Enter the first number: ". This is just so the user's input does not appear run-on right against the colon.
  • when specifying variables to save the response to, the correct syntax with read, is to not have the $, thus we had read and then num1

So with this, you should be able to adapt the read parts of your script and it would work fine.

| improve this answer | |
2

user454038 and Cyrus correctly described how to fix your problem, but not what you're doing wrong.  In the shell (or at least in bash) you use $ when you're referencing the value of a variable, but not when you're referencing the variable itself (i.e., when you're setting the value).  This is confusing; in most (all?) normal programming languages, you use the same syntax either way.  You seem to understand this concept partially; you say

num=$(($num1 + $num2))

rather than making the common mistake of saying

$num=$(($num1 + $num2))                                                (Don't do this!)

This applies to the read statement, too.  When you say read $opr, the $opr gets expanded (i.e., replaced with the current value of the opr variable) — and the current value of the opr variable is an empty string.  So the command ends up looking like

read

And you might expect that to be an error; in fact, it is equivalent to read REPLY.  So,

$ read $num1
17
$ echo $num1
                                                                    (Blank line output)
$ echo $REPLY
17

This example might illustrate the mechanism better:

$ superman=clark_kent
$ read $superman
man of steel
$ echo $superman
clark_kent
$ echo $clark_kent
man of steel

But you shouldn't do things like this (especially the above example) because they are too cryptic; readers/maintainers will have trouble understanding what the code is doing.

TL;DR

Your read statements should be read opr, read num1, and read num2 (without the $s).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.