[Techtalk] Shell scripting for newbies
Kai MacTane
kmactane at GothPunk.com
Thu Feb 19 18:25:30 EST 2004
At 2/19/04 08:14 AM , Deanne Fountaine wrote:
>Here's a suggested script from
>http://www.mozilla.org/projects/thunderbird/linuxurls.html:
And here's my breakdown on just what does what:
>#!/bin/sh
>
>export MOZILLA_FIVE_HOME=/opt/firebird-builds/current
>export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MOZILLA_FIVE_HOME
Set the variable MOZILLA_FIVE_HOME to the value given, and make that
variable available to subprocesses of this one. Then append
$MOZILLA_FIVE_HOME to the end of $LD_LIBRARY_PATH, and make that one also
accessible to subprocesses. (The "export" part is what makes it available
to subprocesses.)
># get URL to load
>url=$1; [ -z $url ] && url=about:blank
Set the variable $url equal to whatever was the first command-line argument
passed to this script. I.e., if the script was called as
"/path/to/scriptname argument", then $url now equals 'argument'. That's the
stuff before the semicolon.
The stuff after the semicolon could most quickly be rendered in English as
"set $url to 'about:blank' if it didn't already contain anything". But you
want to learn, so for completeness' sake I'll break it all down into
component bits. If you already know some other programming languages, and
it's just bash syntax that you're unsure of, some of this will be old hat
to you. In which case, my apologies.
After the semicolon, we have a test (the part in square brackets). The
square brackets themselves tell bash to treat their contents as something
to be tested for truth or falsehood.
The -z operator tests to see if the string $url is zero characters long. If
so, the entire test (the square brackets and everything in them) returns
true. If *that* happens, then the short-circuit AND operator (the "&&")
evaluates the next part of the expression, which sets the value of $url
(and returns true, unless it somehow failed to assign the value to the
variable - in which case, something is *seriously* wrong).
There's also a -n test operator, which tests to see if a string's length is
non-zero. (In which case, it must be positive, because I can't imagine how
you could have a negative-length string.) So you could just as easily write
this as:
[ -n $url ] || url=about:blank
In either case, the short-circuit logic operator only evaluates the second
operand if it needs to. The && operator won't bother with the second
expression if the first was false, because it already knows the overall
answer must be true. The || operator won't bother with the second half if
the first half was true, because it already knows the whole thing is going
to come out true.
># try xremote first
>$MOZILLA_FIVE_HOME/mozilla-xremote-client openURL\($url\) && exit 0
Run the "mozilla-xremote-client" executable in the $MOZILLA_FIVE_HOME
directory, with the single argument "openURL($url)". (The backslashes in
the script escape the parentheses so that bash doesn't try to interpret
them.) Then the && operator tests the return value (or error code, or exit
value, or whatever you prefer to call it) from mozilla-xremote-client. In
bash, 0 evaluates to true and 1 (or any other integer) evaluates to false,
so an exit code of 0 is considered "true" (i.e., "it ran successfully").
So, if mozilla-xremote-client succeeded, the && operator evaluates the
second half of the expression, thus quitting the script. Otherwise, the &&
just stops, and the script continues on to the next line.
># if xremote failed, then launch the browser
>exec $MOZILLA_FIVE_HOME/MozillaFirebird $url
Pretty self-explanatory. Run MozillaFirebird, with $url as its sole
argument. The "exec" makes it take over the current script's PID and memory
space, without bothering to spawn a new process.
Hope that helped!
--Kai MacTane
----------------------------------------------------------------------
"Then, when they spill the demon seed
Turn and face into the wind.
All along you still believed...
Believed you were immune."
--Thomas Dolby,
"The Flat Earth"
More information about the Techtalk
mailing list