Math operator in Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Math operator in Variable
# 1  
Old 09-11-2015
Math operator in Variable

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.
Code:
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 ....

Last edited by vbe; 09-11-2015 at 03:52 PM.. Reason: code tags
# 2  
Old 09-11-2015
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...
Code:
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...
Code:
AMIGA:barrywalker~> o=$(( $m $MULT $n ))
AMIGA:barrywalker~> echo $o
50
AMIGA:barrywalker~> _


Last edited by wisecracker; 09-11-2015 at 03:36 PM.. Reason: Added the extra shell lines to include the variable 'o'...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue. #!/bin/bash PLATFORM="AIX" NEEDSPC=3000 set -x if ; then lsvg | grep -v rootvg | while... (6 Replies)
Discussion started by: port43
6 Replies

2. UNIX for Dummies Questions & Answers

Math

i have file (my_file.txt) that looks like this: 000000000000010000 000000000000010000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 all said and one, it should look... (11 Replies)
Discussion started by: lawsongeek
11 Replies

3. Shell Programming and Scripting

Missing Assigned Variable within logic operator

Hey , I'm trying to perform the following command, however it cannot read the variable assigned earlier. I'm not sure why this happen. Please help thanks while : do echo "what's ur name? (if none just press )" read name changeName = echo $name | sed "s/on/ey/" echo $changeName #this... (8 Replies)
Discussion started by: sexyTrojan
8 Replies

4. Shell Programming and Scripting

How can I use a pipe operator in a variable: OPTION="| command"?

I have a string of commands I am piping some data through and I want to allow command line switches to select which commands are used. I want to do something like this: OPTION="| command3" command1 -a -b c.txt | command2 -d -e $OPTION >result.txt I want to do it that way because OPTION may be... (1 Reply)
Discussion started by: KenJackson
1 Replies

5. Shell Programming and Scripting

math help

$ x=1 $ y=1.5 $ z=$((x*y)) bash: 1.5: syntax error: invalid arithmetic operator (error token is ".5") What's wrong? (2 Replies)
Discussion started by: rockbike
2 Replies

6. Shell Programming and Scripting

Checking if file exists using a NOT operator and shell variable

Hi, I'm new to UNIX, at least shell programming and am having trouble figuring out a problem i'm having. In one section in my nested if statement, i want the program to test if the file does not exist, based on an argument supplied at the command line by the user. What i have is elif ; then... (3 Replies)
Discussion started by: rowlf
3 Replies

7. Shell Programming and Scripting

! operator in variable

Can any body kindly tell me the purpose of ! operator in the following expression: y=${!x} Actually when i execute this statement on my solaris 10 terminal, i get empty output. (1 Reply)
Discussion started by: mmunir
1 Replies

8. Shell Programming and Scripting

scalar variable assignment in perl + { operator

When reading over some perl code in a software document, I came across an assignment statement like this $PATH = ${PROJECT}/......./.... In this particular form of scalar variable assignment, what does the curly braces operators do ? Also, what is the benefit in doing scalar assignment this... (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. Programming

something about <math.h>

Hi, I got an easy problem for you but really difficult for me 'cause I am pretty new to this field I got header file <math.h> included in my .c file , then I write the code as below: k = sqrt(i); /* both variables k and i are int */ then I cc temp.c it says like this undefined... (4 Replies)
Discussion started by: blf0
4 Replies

10. Shell Programming and Scripting

POSIX - variable math

I want to add two numeric variables in a shell script. I am running HP POSIX. Can someone tell me if this is possible and if how? THX for any help. (2 Replies)
Discussion started by: vslewis
2 Replies
Login or Register to Ask a Question