0

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

7
  • couldn't you use something like operator=$(echo "5 + 10" | cut -d' ' -f2) to extract the operator, then use if statements to check value inside of $operator and do corresponding commands? (Sorry if I understood your question wrongly)
    – Garid Z.
    Sep 26, 2022 at 2:53
  • sorry I don't exactly understand, I added a comment under the answer below. Maybe that will clear up my question. Apprecite your reply Sep 26, 2022 at 4:25
  • Please show us what code you've already got. You can then explain which specific part is the problem Sep 26, 2022 at 6:28
  • I have added an explanation to understand the question better Sep 26, 2022 at 18:16
  • This looks like a homework question. see How do I ask and answer homework questions? and Open letter to students with homework problems
    – Bodo
    Sep 26, 2022 at 18:16

3 Answers 3

0

This question can be split into two simple question:

  1. How can I four arithmetic operations?
  2. How can I know which number is less/grater?

Four arithmetic operations

You can use $((expr)) notation to do that. You may want to see man bash for more info. Examples:

$ echo $((6 + 6))
12
$ echo $((6 * 6))
36

less/greater number

You can compare two numbers in [[ expr ]] or [ expr ] form. I would prefer [[ expr ]] form, so below is in that form. To compare number, [[ $num1 -lt $num2 ]] if $num1 is less than $num2. And -gt for check whether grater. Note that, -lt and -gt can't be written with < and >; they compare by lexicographical order, not by numeric order.

Example:

function max() {
  first=$1
  second=$2
  if [[ $first -lt $second ]]; then
    echo $second
  else
    echo $first
  fi
}

function min() {
  first=$1
  second=$2
  if [[ $first -gt $second ]]; then
    echo $second
  else
    echo $first
  fi
}

usage:

$ max 20 40
40
$ min 20 40
20
3
  • @RaymondReddington Maybe you want something like this: echo "(6+10)*4" | bc Sep 26, 2022 at 7:07
  • 1
    @RaymondReddington You can save into a variable your input and apply: echo $variable | bc Sep 26, 2022 at 7:09
  • What do you mean by “four” in “How can I four arithmetic operations?” (Same question for your suggested edit.) Sep 26, 2022 at 8:29
0

Do you mean print input and your calculated values in single line?

Let list.txt be your list of problems as such:

$ cat list.txt
1 + 2
3 + 4
5 + 10

Let calculating.sh be your script that takes string input, and print outs calculated values (here I only wrote for + operation)

$ cat calculating.sh
#!/bin/bash

varA=$(echo $1 | cut -d' ' -f1)
varB=$(echo $1 | cut -d' ' -f3)
operator=$(echo $1 | cut -d' ' -f2)

echo $(($varA + $varB))

To calculate every entry in list.txt with calculating.sh script you could do following:

$ cat list.txt | xargs -I {} sh -c "echo -n {} '= ' && ./calculating.sh '{}'"

1 + 2 = 2
3 + 4 = 6
5 + 10 = 10

xargs makes run "echo -n {} '= ' && ./calculating.sh '{}'" command every line of list.txt, where {} refers to the each line (think it as a looping each line). echo -n {} = will prints your problem-line with = character, and without newline character. and Then ./calculating.sh '{}' will prints the calculated value.

1
  • 1
    I suppose you mean echo $(($varA $operator $varB)) (or echo $((varA $operator varB))). Though if you do break the input line apart like that, you might want to also check that the values and the operator are valid.
    – ilkkachu
    Sep 26, 2022 at 20:13
0

If you want a quick and dirty one, you can just shove the entered expression into the shell's arithmetic expansion:

$ cat arith.sh
#!/bin/bash
read -r expr
if [[ $expr = *\$* || $expr = *\`* ]]; then
    echo "invalid entry!" >&2
    exit 1
fi
echo "$(($expr))"

and then e.g.

$ ./arith.sh 
(10+4)*5
70

The test in the if clause rejects inputs that contain dollar signs or backticks, as those could be used to insert command substitutions. Invalid arithmetic syntax would give an error from the shell.

For comparisons, you can use the C-style comparison operators </<=/>/>= and the ternary operator, e.g. $((a > b ? a : b)) would give the maximum of a and b.

So to expand the script with max and min:

$ cat arith2.sh
#!/bin/bash

read -r expr

if [[ $expr = *\$* || $expr = *\`* ]]; then
    echo "invalid entry!" >&2
    exit 1
fi

if [[ $expr = "max "* ]]; then
    read -r op a b <<< "$expr"
    echo "$((a > b ? a : b))"
elif [[ $expr = "min "* ]]; then
    read -r op a b <<< "$expr"
    echo "$((a < b ? a : b))"
else 
    echo "$(($expr))"
fi

That'll break with inputs like max 1 2 3 due to how read works, and also floats are not supported in Bash so they'll also give errors. Depending on where the input data comes from, you may want to consider sanity checking it.

0

You must log in to answer this question.

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