Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

Bash Calculator from arguments

Hello!

I have written a simple Bash calculator that operates with values given as parameters. In short, you give the operation as the first parameter, the first int as the second parameter and the other int as the third parameter. And it works.

Code
#!\bin\bash
op="$1"
nf="$2"
ns="$3"

#Arguments are stored in these variables for future usage

echo `expr $nf $op $ns`

How could I get it work with ANY number of parameters? Like the user gives a "+" as the first parameter, then 7 other numbers and it sums them up.

Regards from
[Image: qp0UIPa.png]
The Society

FYI: You aren't limited to $1, $2, $3, There are actually 9 (1-8)

But in this case just:
Code
if [ "$1" == "+" ]; then
    sum=0
    for i in ${@:2}; do
        sum=$(expr $sum + "$i")
    done
    echo $sum
fi
Thank you! From this example, I believe, I can build the other operations as well.

In the meantime, I have created a calc that handles *, / and % operations, too.

Code
#!\bin\bash
echo "ArgCalc - Welcome"
echo "Please choose your operation ( +, -, *, /, % )"
read op
echo "Give me the first number"
read nf
echo "Give me the second number"
read ns
case $op in
"*") result="expr $nf \* $ns";;
"/") result="expr $nf \/ $ns";;
"%") result="expr $nf \% $ns";;
*) result="expr $nf $op $ns";;
esac
echo "Result is `eval $result`"

Now, I am going to get it handle more arguments by your example. Thanks again!

Regards from
[Image: qp0UIPa.png]
The Society

Also I goofed on the ${@:1} bit, I skipped $0 like I should have but forgot about + being $1, needs to be ${@:2} to skip the + or else expr will freak out.

The second bit of code also assumed you have dc which not everyone does so its a bad idea to use.

If you want to do some real fancy stuff (like accept -blah arguments) take a look at getopts.

Edit: Now that I think about it.. sometimes you miss the forest for the trees..:
Code
out=$2
for i in ${@:3}; do
    out=$(expr $out "$1" "$i")
done
echo $out

I swear sometimes I just miss the simplest things. No need for individual handlers, just pass $1. (Forgive me, I'm only on my second cup of coffee)
requires escaping atm, + works as intended, / works, and - works as intended (even gives negatives), only way around having to do \ is maybe use x instead (ew) because otherwise you need to disable globbing for your script.
I appreciated your help for first sight, too, but thank you for giving more from it! I will implement the idea into my code tomorrow because I have written much today (no, it was not only the few lines of code I have posted above that took a day, more it was my economical essay about a corporation xD). However, I have assembled a small code as an other version for the same purpose I shew above

Code
#!\bin\bash

echo "ArgCalc - Welcome"
echo "Please choose your operation ( +, -, *, /, % )"
read op
echo "Give me the first number"
read nf
echo "Give me the second number"
read ns

if [[ \\$op == "\*" || \\$op == "\/" ]] || [[ \\$op == "\%" ]]; then
result="expr $nf \\$op $ns"
else
result="expr $nf $op $ns"
fi
echo "Result is `eval $result`"

Which way do you recommend from the two possible base codes I have shown for adding the written idea?

Regards from
[Image: qp0UIPa.png]
The Society

Case is usually the preferred way to go when you want to perform an action based on comparing a single value against multiple.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by Gliese 667CcAug 28, 2021 11:08 AM 3 192 Aug 28, 2021 11:08 AM
Last Post: Sierra-117
[.NET] by VUEDUEJul 11, 2021 06:45 AM 4 213 Jul 11, 2021 06:45 AM
Last Post: VUEDUE
[C] by ShallowMESep 24, 2020 07:47 PM 4 153 Sep 24, 2020 07:47 PM
Last Post: Sajuuk