LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-28-2004, 03:51 PM   #1
Menestrel
Member
 
Registered: Oct 2003
Location: Bucharest
Distribution: Debian Sarge, Slackware Current, Ubuntu
Posts: 183

Rep: Reputation: 30
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 ?
 
Old 11-28-2004, 04:19 PM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
This program will return 1 on failure, 0 on success

Code:
#!/bin/sh
ping -c 1 "$1" >/dev/null
 
Old 11-28-2004, 05:20 PM   #3
Menestrel
Member
 
Registered: Oct 2003
Location: Bucharest
Distribution: Debian Sarge, Slackware Current, Ubuntu
Posts: 183

Original Poster
Rep: Reputation: 30
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 ?
 
Old 11-28-2004, 09:39 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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.
 
Old 11-29-2004, 12:40 AM   #5
Menestrel
Member
 
Registered: Oct 2003
Location: Bucharest
Distribution: Debian Sarge, Slackware Current, Ubuntu
Posts: 183

Original Poster
Rep: Reputation: 30
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 ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Slow First Ping Reply Fr33B5D Linux - Networking 4 08-27-2005 10:07 AM
true or false? alaios Programming 7 07-16-2005 10:54 AM
No ping reply bambolin Linux - Networking 6 10-29-2004 06:18 AM
no ping reply siriuz Linux - Networking 2 03-16-2004 01:53 AM
why does if [ ${var:0:2}='AA' ] always return true blish_blash Programming 1 12-31-2003 08:51 AM


All times are GMT -5. The time now is 05:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration