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 <[email protected]>
_______________________________________________
luv-main mailing list
[email protected]
http://lists.luv.asn.au/listinfo/luv-main

Reply via email to