a couple of unixy questions...
if (netstat -rn | grep -q ^default); then echo online; fi
So, my first question: how come the use of parentheses and not brackets, like
if [ netstat -rn | grep -q ^default ]; then echo online; fi
with a reason more fulfilling than "Because it doesn't work with brackets."
My next topic, /bin/sh syntax questions:
Assume an if-then-{elif}-{else}-fi or a while/until/for-do-done construct. Suppose the test condition is a boolean AND or boolean OR of two or more conditions. Would an example of proper syntax be
until A || B;do
or does the conditional " A or B" need to be grouped in parentheses, brackets, braces, or whatever, as a single argument for the until keyword, kinda like this?
until (A || B);do
What if you had to group a more complex expression of boolean items, such as A and (B or (not C))? What would be the proper way to make the construct?
And lastly (woo-hoo!), suppose you've got a bash script startup item that is supposed to wait until the occurrence of an event before continuing? Well, I implemented that inside a until ...; do; sleep ${T}; done construct. Works like a charm, as long as the event eventually happens.
But, suppose the event doesn't happen, and the computer user issues a shutdown or reboot command from the blue apple in the menubar, or from the user login screen? Well, what happens is that the computer hangs in the reboot or shutdown process because since the wantedevent never occurred in the startup item script, the bash script is still infinitely looping in the until {wantedEvent} loop. That's no bueno.
I'd like to do a construct like this:
until ( {wantedEvent} || {shutdown} || {restart} ); do
So, I'm thinking that if I were to check for the existence of a pid for shutdown and/or reboot like this (put these two lines of code inside my until construct, with my sleep ${T})
pid1=`ps -axwww | grep '/\shutdown ' | awk '{ print $1 }'`
pid2=`ps -axwww | grep '/\restart ' | awk '{ print $1 }'`
and build the until statement like this
until ( {wantedEvent} || [ -n "${pid1}" ] || [ -n "${pid2}" ] ); do
then presumably, if either the wanted event occurs, or we sense a shutdown or reboot command from the user, we escape the until loop, the script file can continue to normal completion, and if it were a shutdown or reboot event, we're not left hangin' out to dry having to do a ruthless stop (followed by the ceremonious /sbin/fsck -yf in single-user mode).
If that looks like a viable approach to the experts here, then the next big question du jour is: are shutdown and restart the actual unixy names of these processes that I would want to use in these variable assignment statements ( pid1=..., pid2=...)???
Whew! Lotsa stuff!
TIA
2001 Quicksilver G4 (M8360LL/A), Mac OS X (10.4.8)