It is currently Tue, 17 May 2022 19:19:48 GMT



 
Author Message
 stating a specific condition...
hello all,

can someone help me with specifying a condition within an 'if' statement...

since i'm new to this, i'm not really sure if this is possible.

what i am doing is writing a little script to ping ip addresses contained within
a list called ip.txt.  upon being ping'd i want to state a condition where if
the output contains the word "alive" in it, then it should go to a file called
alive.txt.  anything other than that should go to a file called dead.txt.

i wrote something like this:

#!/bin/sh
iplist = `cat ip.txt`
for i in $iplist
do
/usr/sbin/ping $i 1
if [ $1 = "$i is alive" ]; then
echo $1 >> alive.txt
else
echo $1 >> dead.txt
fi
done

as you pros out there will know, the above doesn't quite work the way it is
supposed to.  any help would be greatly appreciated.

to add, i was wondering... is there a way to send output to a file specifying
the condition of a date it has to match with?

for example, if i wanted to run my completed script everyday, every 5 hours for
a week - mon to sun, could i let the output append to the monday file... but the
moment it turned tuesday, a new tuesday file would be created which would append
until it was wednesday, from which another new wednesday file is created... and
so on...

in turn, i would get 14 scripts...  7 days alive.txt and 8 days dead.txt

thanks.



 Sat, 11 Oct 2003 22:10:18 GMT   
 stating a specific condition...

"$1" is the first argument given to the script when you execute it:

Example% scriptname first_argument second_argument

Within the script, $1's value will be "first_argument". $2's value will be
"second_argument".

What you want to compare is the exit code of ping. Within a shell, the exit
code of the just-finished command is accessed by $?. A value of 0 indicates a
success, and non-zero means a failure of some sort. Some commands use the exit
codes to indicate the failure. So one way to go about it could look like this:

/usr/bin/ping $i 1
if [ "$?" = "0" ] ; then
echo "$i is alive." >> alive.txt
else
....

A more elegant solution:

if /usr/bin/ping $i 1 ; then
...



 Sat, 11 Oct 2003 22:52:46 GMT   
 stating a specific condition...
Thanks for the input Mathew.

I modified it based on the exit code of the ping and it worked fine.

Just wanted to know if you had any advice as to my second question regarding
appending to a specific file based on the date, and then creating and appending
to a new file when the date changes.

Thanks!

In article <3AE5933E.758B1...@kodak.com>, Mathew Kirsch says...



 Sun, 12 Oct 2003 01:08:40 GMT   
 stating a specific condition...

"goku" <nos...@newsranger.com>

How about

#!/bin/sh

for ip in `cat iplist`
do
        if /bin/ping -c 1 ${ip} 1> /dev/null 2>&1; then
                echo "${ip}" >> ip.`date +%Y%m%d`.good
        else
                echo "${ip}" >> ip.`date +%Y%m%d`.bad
        fi
done



 Sun, 12 Oct 2003 04:01:41 GMT   
 stating a specific condition...
Hi Phineas!

Just wanted to thank you for the advice regarding the script.  It worked like a
charm!

Regards,

Goku

In article <988142833.6514.0.nnrp-13.9e985...@news.demon.co.uk>, Phineas T.
Barnham says...



 Sat, 18 Oct 2003 22:52:37 GMT   
 stating a specific condition...

"goku" <nos...@newsranger.com>

cheers.



 Sun, 19 Oct 2003 01:47:13 GMT   
 
   [ 6 post ] 

Similar Threads

1. State Database and State Database Replicas problem?

2. Questions about the Ohio State, Purdue or Iowa State console servers

3. Suggest TASK_KILLABLE state to overcome most D state trouble

4. nat rdr rules fail pf: BAD state: pf: State failure

5. Modulate state and Keep state in PF

6. determining socket state without affecting its state.

7. Specific files in a specific directory?

8. host-specific <-> network-specific routing precedence

9. filter lines with a specific expression at a specific position

10. checking for specific date and specific number of files


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software