1

This is the shellchecked and updated code. I would be very grateful for any help! More specifically, the code will either not run, or it will run but it will return an error message, and I will be unable to enter any numbers that will be the price of a meal. The error message that pops up when I attempt to run the code is: stinput: command not found and then : command not found which repeats twice. Update: I tried the code the way that user down below suggested so this is the updated code and the error message:

    #!/bin/bash
NumberofInputs="$1"
total=$(( $1 * $2))
total=$(("$total" / 100))
total=$(("$total" +  $3 + "$NumberofInputs"))
echo "The price of the meal is: $1"
echo "The tax for the meal is: $2"
echo "The tip for the meal is: $3"
echo "The total amount paid is: $total"
exit 1

UPDATE: When I remove the first part of user000001 code, I get this error: bash: * : syntax error: operand expected (error token is "* ") bash: "" / 100: syntax error: operand expected (error token is """ / 100") bash: "" + + : syntax error: operand expected (error token is """ + + ") But when I leave the 1st part of user000001's code in, I get this error message: echo I'm sorry, that is incorrect, please enter only 3 parameters.: command not found

UPDATE: I figured out the answer, it was removing the quotations and spacing all the variables together. Thank you to everyone for your help!

7
  • 2
    It is good to shell check your code for obvious things that need fixing, and then update your question with what exactly will not be working. Currently, almost every line needs fixing in order to become valid bash syntax.
    – thanasisp
    Dec 10, 2020 at 6:48
  • 1
    Please add to your question how you're trying to run this code. Please also add the actual error messages you're receiving, so that someone here can explain to you what they mean - and therefore how you should fix them Dec 10, 2020 at 18:08
  • 1
    What is the code supposed to do? What are $1stinput, $2ndinput etc? What do you mean it "either not runs, or runs with errors"? Which one is it? What errors do you get?
    – ilkkachu
    Dec 10, 2020 at 18:25
  • The 1st input and 2nd input are the the price of the meal, the tax that I pay for the meal, and the 3rd input is the tip. The error messages are: stinput: command not found and then : command not found repeated twice
    – user445702
    Dec 10, 2020 at 19:52
  • @user445702 Please update the question with that information, and also how you call the script.
    – Kusalananda
    Dec 10, 2020 at 19:58

1 Answer 1

1

To do arithmetic in bash you need double parentheses, also the positional arguments are $1 $2 etc;

I think you want

total=$(($1 * $2))

The complete script would be

$ cat script.sh 
#!/bin/bash
if [[ $# != 3 ]]
then
   echo "Usage: $0 price tax tip" 1>&2
   exit 1
fi

tax=$(( $1 * $2 / 100 ))
total=$(( tax + $3 + $1 ))

echo "The price of the meal is:  $1"
echo "The tax for the meal is: $tax"
echo "The tip for the meal is: $3"
echo "The total amount paid is: $total"

$ ./script.sh 10 20 4
The price of the meal is:  10
The tax for the meal is: 2
The tip for the meal is: 4
The total amount paid is: 16
6
  • 1
    Thank you for your help but I am getting this error message: * : syntax error: operand expected (error token is "* ") bash: "" / 100: syntax error: operand expected (error token is """ / 100") bash: "" + + "stinput": syntax error: operand expected (error token is """ + + "stinput"")
    – user445702
    Dec 10, 2020 at 20:54
  • @user445702 PLEASE read the comments against your question and edit your question to provide the missing information - how exactly you run your code, the error messages, etc. DO NOT write in the comments - they are for us to ask you for clarification Dec 10, 2020 at 20:55
  • @user445702: Good point, I added a usage notice
    – user000001
    Dec 10, 2020 at 20:58
  • user000001's code returned a error message of command not found, I posted it above. Maybe there's an issue with my putty?
    – user445702
    Dec 10, 2020 at 23:13
  • It you take the time to format your updates to your question, code as code, etc. you'll make it much easier for people to help you Dec 10, 2020 at 23:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .