
I have a rc.local script that runs fine in csh, but I'd like to change it to sh, but I've had no success at it. #! /usr/bin/tcsh if `ping -w 1 -c 1 -n 192.168.1.3 | grep -o "1 packets"` == "1 packets" mount /nfs/bulk2 While we are at it, is there a more elegant way of mounting the remote partition only if that machine is on?

On Thu, Dec 12, 2013 at 10:33:32PM +1100, Allan Duncan wrote:
I have a rc.local script that runs fine in csh, but I'd like to change it to sh, but I've had no success at it.
#! /usr/bin/tcsh if `ping -w 1 -c 1 -n 192.168.1.3 | grep -o "1 packets"` == "1 packets" mount /nfs/bulk2
the simplest conversion to sh (with some fixes*) would be something like: #!/bin/sh if $(ping -w 1 -c 1 -n 192.168.1.3 | grep -q "1 packets") ; then mount /nfs/bulk2 fi or as a one-liner: #!/bin/sh ping -w 1 -c 1 -n 192.168.1.3 | grep -q "1 packets" && mount /nfs/bulk2 * fixes: 1. backticks are pretty much deprecated. use $(...) instead - it's clearer/more readable, avoids most shell quoting/escaping problems, and can be nested. 2. there's no need to check grep's *output* (in fact that seems redundant because the reason you're using grep is to check ping's output). grep exits with 0 on found, 1 for not found, and 2 on error. so you just need to suppress grep's normal stdout (-q does that) and check the exit code.
While we are at it, is there a more elegant way of mounting the remote partition only if that machine is on?
there's lots of ways to make it more complicated (some good, like adding error checking and perhaps multiple retries before giving up), but the above is about as simple as it gets. what do you mean by "elegant"? craig -- craig sanders <cas@taz.net.au>

Craig Sanders <cas@taz.net.au> wrote:
On Thu, Dec 12, 2013 at 10:33:32PM +1100, Allan Duncan wrote:
I have a rc.local script that runs fine in csh, but I'd like to change it to sh, but I've had no success at it.
#! /usr/bin/tcsh if `ping -w 1 -c 1 -n 192.168.1.3 | grep -o "1 packets"` == "1 packets" mount /nfs/bulk2
the simplest conversion to sh (with some fixes*) would be something like:
#!/bin/sh if $(ping -w 1 -c 1 -n 192.168.1.3 | grep -q "1 packets") ; then mount /nfs/bulk2 fi
You shouldn't have the $() here at all. They're unnecessary and cause confusion. Remove them. Further, don't bother checking ping's output with grep's return code. Check ping's return code instead: If ping -w1 -c1 -n 192.168.3.1 >/dev/null 2&>1; then
or as a one-liner:
#!/bin/sh ping -w 1 -c 1 -n 192.168.1.3 | grep -q "1 packets" && mount /nfs/bulk2
Again, grep unnecessary. -- Sent from my Android device with K-9 Mail. Please excuse my brevity.

On Fri, Dec 13, 2013 at 08:29:12AM +1100, Matthew Cengia wrote:
if $(ping -w 1 -c 1 -n 192.168.1.3 | grep -q "1 packets") ; then
You shouldn't have the $() here at all. They're unnecessary and cause confusion.
you're right. actually, it's worse than that...in that context, they're broken because command substitution - backticks or $(...)" - is only needed if you want to examine the output of a command/pipeline or assign it to a variable or similar. the code above won't work at all. dunno what i was thinking....too sleepy to think straight last night.
Further, don't bother checking ping's output with grep's return code. Check ping's return code instead: If ping -w1 -c1 -n 192.168.3.1 >/dev/null 2&>1; then
true also. i tend to use fping rather than ping to check if a host on the LAN is responding: fping is less verbose, has a handy -q (quiet) option to suppress output, and you can specify the timeout in milliseconds (default 500ms) rather than seconds. 50ms or even 10ms should be plenty of time for a host on the local ethernet to respond (i'd expect under a millisecond or a few ms at worst on a home 100baseT or gigabit network - of course that also depends on how fast the pinged host is and how heavy its workload is at the time). if fping -t 50 -q 192.168.3.1 ; then ... ; fi or fping -t 50 -q 192.168.3.1 && .... craig -- craig sanders <cas@taz.net.au>

On 13 Dec 2013, at 09:31, Craig Sanders <cas@taz.net.au> wrote:
i tend to use fping rather than ping to check if a host on the LAN is responding:
fping is less verbose, has a handy -q (quiet) option to suppress output,
fping is also super nice in that it will return immediately on success, but retry 3 times if it initially times out. this gives the host time to return success ping if the first packet is dropped for whatever reason

On Thu, 12 Dec 2013, Craig Sanders wrote:
On Thu, Dec 12, 2013 at 10:33:32PM +1100, Allan Duncan wrote:
I have a rc.local script that runs fine in csh, but I'd like to change it to sh, but I've had no success at it.
#! /usr/bin/tcsh if `ping -w 1 -c 1 -n 192.168.1.3 | grep -o "1 packets"` == "1 packets" mount /nfs/bulk2
the simplest conversion to sh (with some fixes*) would be something like:
#!/bin/sh if $(ping -w 1 -c 1 -n 192.168.1.3 | grep -q "1 packets") ; then mount /nfs/bulk2 fi
bash only. (ie, #!/bin/bash)
or as a one-liner:
#!/bin/sh ping -w 1 -c 1 -n 192.168.1.3 | grep -q "1 packets" && mount /nfs/bulk2
* fixes:
1. backticks are pretty much deprecated. use $(...) instead - it's clearer/more readable, avoids most shell quoting/escaping problems, and can be nested.
Um, no, as I've said before, $() is a bashism. Yes it is neater, but try running that on HP-UX and see how well you go. -- Tim Connors

On 2013-12-13 14:20, Tim Connors wrote:
On Thu, 12 Dec 2013, Craig Sanders wrote:
On Thu, Dec 12, 2013 at 10:33:32PM +1100, Allan Duncan wrote:
I have a rc.local script that runs fine in csh, but I'd like to change it to sh, but I've had no success at it.
#! /usr/bin/tcsh if `ping -w 1 -c 1 -n 192.168.1.3 | grep -o "1 packets"` == "1 packets" mount /nfs/bulk2
the simplest conversion to sh (with some fixes*) would be something like:
#!/bin/sh if $(ping -w 1 -c 1 -n 192.168.1.3 | grep -q "1 packets") ; then mount /nfs/bulk2 fi
bash only. (ie, #!/bin/bash)
Incorrect. $() is POSIX: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag...
Um, no, as I've said before, $() is a bashism. Yes it is neater, but try running that on HP-UX and see how well you go.
Granted, only because HP-UX doesn't necessarily implement current POSIX. -- Regards, Matthew Cengia

On Fri, Dec 13, 2013 at 02:20:00PM +1100, Tim Connors wrote:
bash only. (ie, #!/bin/bash)
no, it's not. $() is POSIX standard. http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag... it's also supported by the korn shell. and bash too, of course. and zsh. see also: http://stackoverflow.com/questions/3789894/is-command-substitution-foo-bashi... also interesting: http://mywiki.wooledge.org/BashFAQ/082 http://www.in-ulm.de/~mascheck/various/cmd-subst/ http://utcc.utoronto.ca/~cks/space/blog/unix/PosixCommandSubstitution
1. backticks are pretty much deprecated. use $(...) instead - it's clearer/more readable, avoids most shell quoting/escaping problems, and can be nested.
Um, no, as I've said before, $() is a bashism. Yes it is neater,
you might have said it before but you were wrong then as you are wrong now. $() is NOT a bashism.
but try running that on HP-UX and see how well you go.
are you really running a pre-POSIX sh on HP-UX? that must be one truly ancient HPUX system. according to the man page at http://nixdoc.net/man-pages/hp-ux/man1/sh.1.html the original bourne sh was removed from HPUX starting with HP-UX 11i Version 1.5 and replaced with the POSIX sh. according to http://en.wikipedia.org/wiki/HP-UX 11i v1.5 was released in 2001. it's reasonable - and good practice - to point out REAL differences between POSIX sh and bash, but $() is not one of them. pre-POSIX versions of shell aren't in the least bit relevant to anyone running anything even resembling a modern "unix" system...and on linux (and *bsd) systems you'd be hard-pressed to find any kind of sh that wasn't POSIX (e.g. dash) or a superset of POSIX (e.g. bash). craig -- craig sanders <cas@taz.net.au>

On Fri, 13 Dec 2013, Craig Sanders wrote:
On Fri, Dec 13, 2013 at 02:20:00PM +1100, Tim Connors wrote:
bash only. (ie, #!/bin/bash)
no, it's not. $() is POSIX standard.
http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag...
Poo, misremembering details and jumping to conclusions. subject line "iptables-save output using dotted quad netmask rather than CIDR" from 18 Jun, it were {0..32} which is neat but not yet POSIX. -- Tim Connors

If it's available on your system automount (autofs) might do what you want. If it's part of your boot process remember to add a low --timeout to the mount. http://tldp.org/HOWTO/Automount.html Edward
On 12 Dec 2013, at 22:33, Allan Duncan <amd2345@fastmail.com.au> wrote:
I have a rc.local script that runs fine in csh, but I'd like to change it to sh, but I've had no success at it.
#! /usr/bin/tcsh if `ping -w 1 -c 1 -n 192.168.1.3 | grep -o "1 packets"` == "1 packets" mount /nfs/bulk2
While we are at it, is there a more elegant way of mounting the remote partition only if that machine is on? _______________________________________________ luv-main mailing list luv-main@luv.asn.au http://lists.luv.asn.au/listinfo/luv-main

On 12/12/13 22:33, Allan Duncan wrote:
I have a rc.local script that runs fine in csh, but I'd like to change it to sh, but I've had no success at it.
#! /usr/bin/tcsh if `ping -w 1 -c 1 -n 192.168.1.3 | grep -o "1 packets"` == "1 packets" mount /nfs/bulk2
Thanks for the suggestions, I ended up with #!/bin/sh fping -t 50 -a 192.168.1.3 && mount /nfs/bulk2
participants (6)
-
Allan Duncan
-
Craig Sanders
-
Edward Savage
-
Hannah Kramer
-
Matthew Cengia
-
Tim Connors