[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Shell scripting in Bash



In article <[🔎] Pine.LNX.3.93.960426124628.7659C-100000@dwarf.polaris.net>,
Dale Scheetz  <dwarf@polaris.net> wrote:
>I have been trying to learn shell scripting using the man page for bash.
>Asside from the need for a lot of reading between the lines, there seem to
>be several errors. == is declared to be the equality operator, but = is
>the correct syntax. || is declared to be the or operator, but although:
>
>if [ a != b ]
>
>works fine
>
>if [ a != b || c != d ]
>
>fails with several errors ( ']' not found, and != command not found )

'[' is really an alias for the "test" command, and there is
a manpage for it -- "man test".

When you realize that '[' is just a command (it is built in to
most shells, but there probably _is_ a '[' in /usr/bin) you
can see that 

if [ a != b || c != d ]
   ^        ^^
   |        End of command; shell starts interpreting next command
   +-- the command '['

doesn't work. The first '[' doesn't find a trailing ']' and tells
you so, and the shell tries to interpret "c != d" as the next command.
If it said "!= command not found" then you probably used $c != $d and
$c was empty.

Try:

if [ "$a" != "$b" -o "$c" != "$d" ]

or, calling the '[' command twice and using shell logic:

if [ "$a" != "$b" ] || [ "$c" != "$d" ]
then
   ...
fi

The quoting protects you when $a is empty - it produces an empty
string instead of just nothing at all. Remember that the shell
does substitution first before executing a command.

Good luck -- Mike.
--
+ Miquel van Smoorenburg   + Cistron Internet Services +  Living is a     |
| miquels@cistron.nl (SP6) | Independent Dutch ISP     |   horizontal     |
+ miquels@drinkel.ow.org   + http://www.cistron.nl/    +      fall        +


Reply to: