LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-07-2009, 03:55 PM   #16
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550

Quote:
Originally Posted by tuxdev View Post
For doing stuff that's mathematical, it's actually better to use (()) arithmetic test construct

One thing I have learned recently, which can be seen in another couple of threads somewhere here, is using `bc` to do the math -- not only does it not gag when you input "Strings" of numbers, but it does floating point too. Very handy for anything beyond the most mundane basic math, and simple do use:

shell$ result="$(echo "2.54 * 3.6" | bc -l)"

Anyhow, maybe I'm drifting off topic here

Sasha
 
Old 12-07-2009, 04:08 PM   #17
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Yeah, with a decimal point, you're pretty much stuck with bc.

The nice thing about (( )) is that it doesn't gag either. When all you area doing is the most mundane basic math, it's much nicer to do it directly in the shell rather than fork off a process.
 
Old 12-08-2009, 02:24 PM   #18
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Quote:
Originally Posted by tuxdev View Post
Yeah, with a decimal point, you're pretty much stuck with bc.

The nice thing about (( )) is that it doesn't gag either. When all you area doing is the most mundane basic math, it's much nicer to do it directly in the shell rather than fork off a process.
or awk ...
 
Old 12-08-2009, 02:49 PM   #19
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
For those interested:

I played around with different shebangs at the beginning of my script; using #!/bin/sh works, as does #!/bin/bash --posix, but using just #!/bin/bash causes weird errors here and there.

So, the good news is that (at least in theory, and in practice during my testing) my script is POSIX compliant, which was my hope.

Sasha
 
Old 12-16-2009, 03:52 PM   #20
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Since this thread is also related to all this stuff:

http://www.linuxquestions.org/questi...script-775058/
http://www.linuxquestions.org/questi...rings.-775622/

.. and the above stuff is [solved], I consider this issue solved too. The problem was in part that bash --posix is not actually posix compliant, and code that will run under that, will NOT necessarily run in a real POSIX shell.

Also, FWIW, AFAICT-IIRC using [[..]] is also not POSIX compatible, so I've opted to keep all of my [..] tests as they are, at least for the project I'm currently working on.

Thanks for everyones interest & input here

Sasha
 
Old 12-16-2009, 06:07 PM   #21
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
You've discovered the hard way a lot of what's said here, but it might be an interesting read nonetheless:
http://mywiki.wooledge.org/BashGuide...hooseYourShell
 
Old 12-16-2009, 06:30 PM   #22
MBybee
Member
 
Registered: Jan 2009
Location: wherever I can make a living
Distribution: PC-BSD / FreeBSD / Debian / Ubuntu / Win7 / OpenVMS
Posts: 438

Rep: Reputation: 57
Quote:
Originally Posted by GrapefruiTgirl View Post
OK, cool -- thanks you guys for the tips about the double brackets; I've read of it MANY times but never found that I actually needed to do it, although I have encountered the operator errors within the [ .. ] constructs, and generally got around it by switching operator type, such as from | to -o or from != to -ne. I always wrote this off as bash telling me that I was using the wrong operator for the type of variable, i.e. trying to compare $string to $integer ]. I had pretty much given up on < > and switched to -lt -gt instead, but this double-bracket thing will have me re-examining this stuff.


Thanks!!
Sasha
With single [ you should always stick to the POSIX compliant operators, as pointed out already. If you're going pure bash you might as well take advantage of [[ ]] and such

This seems complex enough that you may want to look into awk or Perl (as I see was mentioned earlier), just because shell script can get 'interesting' when you go beyond a certain level.

Last edited by MBybee; 12-16-2009 at 06:32 PM. Reason: edited for context :D
 
  


Reply

Tags
bash, gawk, sh


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Really strange problem - RedHat /bin/sh vs /bin/bash leeharris Programming 4 02-13-2009 07:35 AM
How to add paths to sbin, bin/bash, bin sh for users gopi.d Fedora 3 12-07-2007 01:47 AM
Change /bin/bash to /bin/zsh Smokey Slackware 12 07-14-2004 02:06 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 07:42 PM
bin/bash:usr/bin/lpr NO SUCH FILE OR DIRECTORY Adibe_Hamm Linux - Newbie 3 10-14-2003 03:30 AM


All times are GMT -5. The time now is 10:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration