PDA

View Full Version : Variables and math in Bourne Shell



mr.rhtuner
July 25th, 2012, 11:19 PM
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 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?

papibe
July 25th, 2012, 11:25 PM
Hi mr.rhtuner.

Try this:

X=$(( 10 + 2 * 7))
Regards.

mr.rhtuner
July 25th, 2012, 11:31 PM
Hi mr.rhtuner.

Try this:

X=$(( 10 + 2 * 7))
Regards.



Thank you, I have tried that before and just tried again.

X=$ (( 10 + 2 * 7 ))
Syntax error: 'X=$ unexpected

I'm digging around on the internet about this too, I don't know why this is so difficult lol:confused:

steeldriver
July 25th, 2012, 11:37 PM
no space between $ and (( ...

mr.rhtuner
July 25th, 2012, 11:44 PM
no space between $ and (( ...

I've tried that just a moment ago, X=$((10 + 2 * 7)) and it comes back with syntax error: `X=$` unexpected

I tried using the ` and ' to see if it will help but no go on that

Hetepeperfan
July 25th, 2012, 11:45 PM
no space between $ and (( ...

also no space after the =.



me@desktop:~/$ X=$((10 + 2 * 7 ))
me@desktop:~/$ echo $X
24works perfectly for me

Hetepeperfan
July 25th, 2012, 11:48 PM
also no space after the =.



me@desktop:~/$ X=$((10 + 2 * 7 ))
me@desktop:~/$ echo $X
24works perfectly for me

sorry I did this in the Bourne Again Shell I don't know it in the oldskool bourne shell.

mr.rhtuner
July 25th, 2012, 11:49 PM
also no space after the =.



me@desktop:~/$ X=$((10 + 2 * 7 ))
me@desktop:~/$ echo $X
24works perfectly for me


Wow, that is odd. I am doing this on a SCO Server which is running a Linux variation with the /Bin/Sh bourne shell.

Any ideas why this wouldn't be working for me?


edit: Ahh I see. I did the same command in my Red Hat Linux server using BASH and it worked as you said. But I am using the old school bourne shell on a SCO group server. Trust me, I don't want to be using SCO nor this old bourne shell at all.

Hetepeperfan
July 26th, 2012, 12:01 AM
edit: Ahh I see. I did the same command in my Red Hat Linux server using BASH and it worked as you said. But I am using the old school bourne shell on a SCO group server. Trust me, I don't want to be using SCO nor this old bourne shell at all.

you might just type bash at the commanline to see if it is installed

"/bin/sh"

Does not always default to bash.

anyway to do it in the bourne shell:


expr 2 \* 7 + 10should do the trick, since the $((integer operation)) syntax didn't exist in the orignal bourne shell.

cheers,

Maarten

steeldriver
July 26th, 2012, 12:04 AM
for the variable assignment part of your question does this work?


x=`expr 10 + 2 \* 7`; echo $x

diesch
July 26th, 2012, 12:07 AM
If you have a "SCO server" you are quite likely not running Linux but SCO OpenServer, UnixWare are one of their predecessors.

Your /bin/sh doesn't seem to be POSIX compliant and may not support this syntax. Try to find some docs for it or look for another shell.

mr.rhtuner
July 26th, 2012, 12:09 AM
for the variable assignment part of your question does this work?


x=`expr 10 + 2 \* 7`; echo $x


Thank you steeldriver. After searching another Unix/Linux forum, a member pointed me in the expr direction.

I was able to get it.



Here is what I wrote out:




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

echo $X

24'



So I got X as the variable holding the answer of 24....Thank you very much!


I wrote it out in a bit longer of a format but they both do the same thing.

mr.rhtuner
July 26th, 2012, 12:17 AM
If you have a "SCO server" you are quite likely not running Linux but SCO OpenServer, UnixWare are one of their predecessors.

Your /bin/sh doesn't seem to be POSIX compliant and may not support this syntax. Try to find some docs for it or look for another shell.


Sorry that was my mistake assuming it runs Linux. I was going to say Unix but wasn't sure if they would have a server running on such an older OS. Regardless...I am using the SCO server which I have access to through my school to practice Linux/Unix.

Thanks again to everybody :popcorn:

Bachstelze
July 26th, 2012, 12:33 AM
Sorry that was my mistake assuming it runs Linux. I was going to say Unix but wasn't sure if they would have a server running on such an older OS. Regardless...I am using the SCO server which I have access to through my school to practice Linux/Unix.


I suggest you stop using it, because its tools are apparently not POSIX-compliant, and you could pick up some bad habits...