[SOLVED] #!/bin/sh vs #!/bin/bash -- script executes differently; each way = different bugs
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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.
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.
.. 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.
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 05:32 PM.
Reason: edited for context :D
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.