In a bash script, I would like to determine the maximum or minimum value depending on my input: for example if my input is max 20 40
, then the output should be 40
. If the input is min 20 40
then the output should be 20
, and so on for any two numbers.
The same needs to be done for addition, subtraction, multiplication and division. If the input is 6 + 6
then the output is 12
. If the input is 6 * 6
then the output is 36
.
The problem I am having is that I am able to tell bash to echo two numbers and then display which is the min or max, or even do addition subtraction etc. For example if the input is 5 10
the output is 15
if I am running the addition file. However I do not understand how to do it all in one line, for example if the input is 5 + 10
the output is 15
.
we can forget about the max/min operation for now and focus on the arithmetic part of the solution. to explain the question a bit better, the code should run as follows.
bash filename.sh
6 * 7
42
the first line is me running the file, the second line is where I type my input, and the final line is the output (the answer) followed by a new line.
this is extremely easy if I need to multiply ONLY 6 * 7
, however this is not the case. After I run the file bash filename.sh
and click enter, I should be directed to the next line where I am able to type ANY simple arithmetic operation limited to addition subtraction multiplication and division.
more examples of how the code should run
bash filename.sh
10+ 5
15
bash filename.sh
14 / 7
2
bash filename.sh
17 - 3
14
The script should be able to do it for any values inputted thank you
operator=$(echo "5 + 10" | cut -d' ' -f2)
to extract the operator, then useif statements
to check value inside of$operator
and do corresponding commands? (Sorry if I understood your question wrongly)