You seem to be confused regarding conditionals in shell scripts. Every shell command has an exit status, which is an integer between 0 and 255, with 0 meaning success and any other value meaning failure. Statements like
while that expect boolean operands inspect the exit status of the command and treat 0 (success) as true and any other value (failure) as false.
For example, the
grep command returns 0 if the pattern is found and 1 if the pattern is not found. So
while ifconfig | grep "192.168.100." > /dev/null; do …
repeats the loop as long as the pattern
192.168.100. is found in the output of
ifconfig. Note that the pattern
192.168.100. matches strings like
192x168 1007, because
. in a regular expression matches any character; to search for a literal string, pass the option
grep. To invert the condition, put
! in front.
while ! ifconfig | grep -F "192.168.100." > /dev/null; do …
Further in the script, you want to compare the value of a variable to a number. You use the
-gt operator, which is part of the syntax of the of conditional expressions understood by the
test command. The
test command returns 0 if the conditional expression is true and 1 if the conditional expression is false.
if test "$x" -gt 200; then
It is customary to use the alternate name
[ for the
test command. This name expects the command to end with the parameter
]. The two ways of writing this command are exactly equivalent.
if [ "$x" -gt 200 ]; then
Bash also offers a third way to write this command, with the special syntax
[[ … ]]. This special syntax can support a few more operators than
[ is an ordinary command subject to the usual parsing rules, while
[[ … ]] is part of the shell syntax.
Again, keep in mind that
[ is for conditional expressions, which are a syntax with operators like
[ doesn't mean “boolean value”: any command has a boolean value (exit status = 0?).
Detecting that the network is up
Your way of detecting that the network is up is not robust. In particular, note that your script will be triggered as soon as any network interface acquires an IP address within the specified range. In particular, it's quite possible that DNS won't be up yet at that point, let alone any network shares mounted.
Do you really need to run these commands when someone logs in? It's easier to make a command run automatically when the network is brought up. The way to do that depends on your distribution and whether you use NetworkManager.
If you need to run these commands as part of the login scripts, then test for the resource that you really need, not for the presence of an IP address. For example, if you want to test whether
/net/somenode/somedir is mounted, use
while ! grep -q /net/somenode/somedir </proc/mounts; do
If you have upstart or systemd…
then you can use it. For example, with Upstart, mark your job as
start on net-device-up eth0 (replace
eth0 by the name of the interface that provides the desired network connectivity). With Systemd, see Cause a script to execute after networking has started?