[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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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:
.. 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.
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