You are not logged in.

#1 2015-05-19 19:57:12

woodson2
Member
Registered: 2014-06-30
Posts: 5

Infinite "while" loop subshell loses current date variable

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

#2 2015-05-19 20:23:37

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: Infinite "while" loop subshell loses current date variable

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

#3 2015-05-20 00:30:30

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,740

Re: Infinite "while" loop subshell loses current date variable

woodson2 wrote:

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

#4 2015-05-20 00:46:20

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Infinite "while" loop subshell loses current date variable

My esteemed colleagues have already idenitifed the issue, so I thought I'd just proffer some bash related advice:


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2015-05-21 12:33:29

esa
Member
Registered: 2011-12-29
Posts: 143
Website

Re: Infinite "while" loop subshell loses current date variable

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)


Author of: TUI (Text User Interface for scripts), VHS (Video Handler Script, using ffmpeg) and YASSI (Yet Another Simple Script Installer)

Offline

#6 2015-05-21 12:59:28

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,407
Website

Re: Infinite "while" loop subshell loses current date variable

esa wrote:

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

#7 2015-05-21 12:59:56

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: Infinite "while" loop subshell loses current date variable

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

Board footer

Powered by FluxBB