0

Hi I am doing a project where I must write a bash script calculator that allows you to perform multiple arithmetic operations and also to the power of. I have done the basic bit but have been told I must make it non interactive and was just wondering how would I do this? My code has been provided below.

clear bash while [ -z "$arg1" ] do read -p "Enter argument1: " arg1 done while [ -z "$arg2" ] do read -p "Enter argument2: " arg2 done echo "You've entered: arg1=$arg1 and arg2=$arg2" let "addition=arg1+arg2" let "subtraction=arg1-arg2" let "multiplication=arg1*arg2" let "division=arg1 / arg2" let "power=arg1**arg2" echo -e "results:\n" echo "$arg1+$arg2=$addition" echo "$arg1-$arg2=$subtraction" echo "$arg1*arg2=$multiplication" echo "$arg1/$arg2=$division" echo "$arg1^$arg2=$power" 

I was thinking of trying to make it so that the user does not have to type in 2 numbers but I am still pretty new to bash and scripts as a whole so I am wondering how to do this.

3

1 Answer 1

1

Use getopts to process arguments to your script. Here is an example from this site. You could replace the lines from your first line to your echo "You've entered …" with modified code from the other answer I linked to. You might want to read the PARAMETERS section of man bash, in particular understand what the Special Parameters are (*, @, #, ?, -, $, !, and 0). You should know those cold if you are scripting bash.

An alternative is to use the builtin read. I would use getopts.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.