Variables and math in Old skool Bourne Shell

Thread Tools Search this Thread
# 1  
Variables and math in Old skool Bourne Shell

Hey everybody, I've been searching google and these forums and have found some solutions to the issues I've been having today within the OLD Bourne Shell. I am following chapter 6 of the Guide to Unix using Linux 4th Edition.

I am working on some basic calculations with variables in the BASH SHELL and doing the same within the Bourne Shell with modifications.

In BASH, I did the following:

let X=10+2*7
Type echo $X and press Enter. You see 24 on the screen.

Now I realize that the bourne shell does NOT have LET as a command, and I need to use expr.

I was able to get the math to work, by doing the following:

$ expr 10 + 2 '*' 7
24 is the answer.

But how do I incorporate the variable of X into this?

I tried:

$ X=%((10 + 2 '*' 7))
but get the error: synatx error: 'X=% unexpected

Anybody mind giving me an idea what I am doing wrong?
# 2  
X=`expr ...`

This User Gave Thanks to neutronscott For This Post:
# 3  
Originally Posted by neutronscott
X=`expr ...`

Thank you for the guidence.

I was able to get it.

Here is what I wrote out:

X=`expr 10 \+ 2 \* 7`
echo $X


So I got X as the variable holding the answer of 24....Thank you very much!
# 4  
Both expr and bc are external commands and therefore nothing to do with the Shell.

I prefer bc because it can handle both simple and complex formuli:
X=`echo "10 + (2 * 7)"|bc`
echo $X


Many will note that this works but is less easy to read:
X=`echo "10 + 2 * 7"|bc`
echo $X



Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Doing math using user defined input and system environment variables

Hi, I need some help to setup some environmental variables. for example...Get A -> userdefined/user input B -> a number. c -> system variable...for example $GETCONF PAGE_SIZE E = do some math using bc display a message "The value is E" setup the system/kernel paramter sysctl -p... (3 Replies)
Discussion started by: saravanapandi
3 Replies

2. Shell Programming and Scripting

Math calculation over shell

Hi I am trying to calculate the rate at which something is happening. I have 2 files- a1 and b1. I want to calculate something like this ((wc -l a1)/(wc -l a1 + wc -l b1))*100 over a loop for different a and b. Is this possible, help me out fellas. Thanks a lot :) (5 Replies)
Discussion started by: jamie_123
5 Replies

3. Shell Programming and Scripting

Bourne shell & Korn shell

Could some one tell me the difference btw Bourne shell and the Kshell? Which is more flexible and reliable in terms of portability and efficiency. When i type the following command .. $ echo $SHELL yields me /bin/sh Does this tells me that I am in Bourne shell. If yes, how can i get... (6 Replies)
Discussion started by: bobby1015
6 Replies

4. Shell Programming and Scripting

A Math problem using shell script

Have a bit complicated math query .. Basically i am given a number which is > 50 .. I am suppose to find the calculation to get a number which is equal or more than the input number and is also a multiple of any number between 20 - 30 . For example . Input number is 60 . Now 20x3 =60 ... (2 Replies)
Discussion started by: greycells
2 Replies

5. Shell Programming and Scripting

assigning two variables in bourne shell

there are two variables from a select query and these two variables has to assign in a update query . the two variables are two fields. How to assign without splitting by awk? (7 Replies)
Discussion started by: razen
7 Replies

6. Shell Programming and Scripting

How to activate Korn Shell functionnalities in Bourne Shell

Hi All I have writing a Korn Shell script to execute it on many of our servers. But some servers don't have Korn Shell installed, they use Borne Shell. Some operations like calculation don't work : cat ${file1} | tail -$((${num1}-${num2})) > ${file2} Is it possible to activate Korn Shell... (3 Replies)
Discussion started by: madmat
3 Replies

7. Shell Programming and Scripting

I need to understand the differences between the bash shell and the Bourne shell

I do not claim to be an expert, but I have done things with scripts that whole teams of folks have said can not be done. Of course they should have said we do not have the intestinal fortitude to git-r-done. I have been using UNIX actually HPUX since 1992. Unfortunately my old computer died and... (7 Replies)
Discussion started by: awk_sed_hello
7 Replies

8. UNIX for Dummies Questions & Answers

Can Any help me with the math on this shell script?

Develop a grade calculating program. This program will process all students in the file. This program should neatly display each field of each student's record *and* adds the following items: Course Average and Letter Grade. The course average is calculated by the following weights: 50% for quiz... (7 Replies)
Discussion started by: knp808
7 Replies

9. Programming

Math with user variables

Hi everybody: I have a problem about use variables. I 've created this variable: var=`wc -l file.txt | cut -c 1-2`; n_var="$var"-1 ; echo $n_var; In my case var is 8. When echo shows $n_var does not appear as I want. The question is how can I subtract, this is operate, to my variable.... (1 Reply)
Discussion started by: tonet
1 Replies

Featured Tech Videos