LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Return true or false if I have ping reply (https://www.linuxquestions.org/questions/programming-9/return-true-or-false-if-i-have-ping-reply-260247/)

Menestrel 11-28-2004 03:51 PM

Return true or false if I have ping reply
 
I want to make a program that returns me 0 or 1 if I have a ping reply or not to a station on my network.. Can anybody give me some hints ?

randyding 11-28-2004 04:19 PM

This program will return 1 on failure, 0 on success

Code:

#!/bin/sh
ping -c 1 "$1" >/dev/null


Menestrel 11-28-2004 05:20 PM

ok, I'm a newbie, I did a
#!/bin/bash
if [ ping -c 1 "$1" >/dev/null ]; then
echo '0';
else
echo '1';
fi

and I get ./script_ping: line 2: [: too many arguments
111

what am I doing wrong ?

randyding 11-28-2004 09:39 PM

Hi, I'm very happy to help out here.

You want to type the program exactly like this, and lets call it myping.sh
and you put it in /bin so its in your path.

#!/bin/sh
ping -c 1 "$1" >/dev/null

Then to use it in another script you would do this

#!/bin/sh
if myping.sh 192.168.1.1; then
echo "The address 192.168.1.1 is alive"
fi

As a side note, anything you place inside the [ ] has to be "test" syntax.
See "man test" for more information.
The difference is that in the myping.sh program it returns exit status 0 or 1. In your
program you were attempting to print "0" or "1" to stdout, which is not the same thing.

Menestrel 11-29-2004 12:40 AM

thanks randyding, you are of great help, can I also ask, how could I make the myping program or the other program to take the IP value from a file ?


All times are GMT -5. The time now is 11:51 AM.