I wanted to write a bash script which take a variable X with math operator as (* or / or + or - or % and more ....). Instead of writing logic for each condition can we have a generic logic. Like sudo code below.
A=10, B=20
P = $A $X $B;
echo $P;
-------------
when X has value "*" echo $P, should return 200.
when X has value "/" echo $P, should return .5
etc ....
hi samcompda...
Be aware that most shells can only do INTEGER arithmetic. Floating point is not part of their makeup.
However there are tools that give floating point, trig, log and other functions.
1) man bc.
2) man dc.
3) man awk.
There may be others but these are the common ones.
Now to get to your manipulation of a shell expansion:-
Longhand manually using OSX 10.7.5, default bash terminal...
Last login: Fri Sep 11 17:55:13 on ttys000
AMIGA:barrywalker~> MULT='*'
AMIGA:barrywalker~> m=10
AMIGA:barrywalker~> n=5
AMIGA:barrywalker~> echo $(( $m $MULT $n ))
50
AMIGA:barrywalker~> _
EDIT:-
Added extra code lines to include variable 'o' also...
AMIGA:barrywalker~> o=$(( $m $MULT $n ))
AMIGA:barrywalker~> echo $o
50
AMIGA:barrywalker~> _