You are not logged in.
I have a simple script to log network connectivity to a set of systems.
However, as expected the date appended to the log never changes because the new variable is lost when the loop starts again. Can someone clue me in on how to get around this issue?
#!/bin/bash
LOG=/tmp/netlog
when=`/bin/date`
while true;
do
for server in srv1 srv2 srv3 srv4 srv5;
do
ping -c 1 $server > /dev/null
if [ $? -ne 0 ]; then
echo "$server-DOWN!" $when >> $LOG.$server
# mail
else
echo "$server-UP!" $when >> $LOG.$server
fi
done
done
Offline
You only actually run 'date' once at the start of the script. Then you just use that value over and over.
To fix it, I'd just replace the $when with date - that'd be simpler and more clear, there is no point in using a variable for that. But if you want it as a variable store the string "/bin/date" in the variable, not the result of the command, then eval that string as needed.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
However, as expected the date appended to the log never changes because the new variable is lost when the loop starts again.
No, it isn't. Trilby has the better solution. Your problem is the variable is set, but is never changed. It is not lost.
If you want it to change inside the loop, move it inside the (inner) loop.
Last edited by ewaller (2015-05-20 00:48:16)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
My esteemed colleagues have already idenitifed the issue, so I thought I'd just proffer some bash related advice:
Offline
Jason, from my experience, [] with quotes around variables is generaly more cross-over compatible (bsd'ish, deb'ish, rh'ish) than [[]] without quoting of variables.
More than once i've had unexpected behaviour caused by using [[ condition ]].
Last edited by esa (2015-05-21 12:36:48)
Offline
Jason, from my experience, [] with quotes around variables is generaly more cross-over compatible (bsd'ish, deb'ish, rh'ish) than [[]] without quoting of variables.
More than once i've had unexpected behaviour caused by using [[ condition ]].
That's why you'd use /bin/bash instead of /bin/sh .. (or rather /usr/bin/env bash - OpenBSD has bash in /usr/local/bin) - [[ is bash specific.
http://mywiki.wooledge.org/Bashism
Last edited by Alad (2015-05-21 13:01:27)
Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
Offline
esa, if that's the case you'd have a problem much earlier at the shebang. [[ ]] is a bash built in. If you explicity use bash in the shebang, then [[ ]] would be best. If you want it portable across various shells, then you shouldn't use [[ ]] but you also definitely shouldn't specify a bash: just use /bin/sh.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline