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 ?
|
This program will return 1 on failure, 0 on success
Code:
#!/bin/sh |
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 ? |
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. |
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. |