[K12OSN] bash script help

Les Mikesell les at futuresource.com
Wed Oct 31 13:23:04 UTC 2007


Doug Simpson wrote:
> Is it just not possible to make a bash script run in a continuous loop?

Yes, note the outer loop of
  while :
   do
[...]
   done
The : statement does nothing but evaluates 'true' so the loop runs forever.

> Your solution looks to be about the easiest to do, but, like the rest,
 > has no provision for the "restart at the top of the list again" part.

Try it. It will wait 30 seconds after it restarts the loop.

> In your solution, would I need the text to log in quotes if it were more than one word so it wouldn't think it was more than one variable?
> 
> like:
> 10.40.12.3 "This hostname is down."

In a read statement, the line is split on white space (actually $IFS so 
it coud be changed) and assigned to the variables in the list.  All 
remaining variables go to the last variable.  So you don't need to 
quote, although multiple spaces might get collapsed to one if you don't 
quote the variable in the echo like "$TEXT"

> And, will it not report anything if the host is *not* down? For the
 > purpose, I need it to sit there quietly running until it sees that a
> host is actually down before it takes any action. Then it reports,
 > and continues on to the next one and so on.

Yes the || construct means 'or' and the right side won't happen unless 
the left side fails (returns a non-zero status).

> What does this line do?:
>   done </path/to/file

It makes the file become stdin to the inner loop so the read statement 
consumes lines from the file.

> I'd make a text file in the form:
> 
> ip_address <white space> text to log
> 
> And use a script like:
> 
> #!/bin/bash
> while :
>   sleep 30
>   do
>   while read ADD TEXT
>   ping -i 3 -c 3 $ADD || echo $TEXT >>serverdown.txt
>   done </path/to/file
> done

-- 
   Les Mikesell
    les at futuresource.com




More information about the K12OSN mailing list