LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-14-2003, 09:19 AM   #1
amaze
LQ Newbie
 
Registered: Aug 2003
Location: UK
Posts: 19

Rep: Reputation: 0
echo $?


Hi,

Below is a simple part of a script I am building. Is there any way of capturing if there IS a host or not?

I thought the var $? would capture if there is a host or not but it seems to just capture if it fails or not...

Any clues?

--script--

#!/bin/bash

host $1

echo $?

--/script--
 
Old 08-14-2003, 10:47 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,698

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
See man pages for host for proper syntax.
 
Old 08-14-2003, 10:52 AM   #3
Half_Elf
LQ Guru
 
Registered: Sep 2001
Location: Montreal, Canada
Distribution: Slackware; Debian; Gentoo...
Posts: 2,163

Rep: Reputation: 46
horrible syntax.

what's about :
$HOST = `cat /etc/hostname`
if [ $HOST != "" ]; then
echo $HOST
fi
 
Old 08-14-2003, 05:23 PM   #4
amaze
LQ Newbie
 
Registered: Aug 2003
Location: UK
Posts: 19

Original Poster
Rep: Reputation: 0
I don't think you have understood my question.

Basically I am using this command for example:

./dohost.sh 193.233.92.23

This script would look like this:

#!/bin/bash

host $1

---

I want to be able to know IF the host name resolves? For example it returns 0 ($?) if the host resolves and 1 if it doesn't resolve...

I hope I explained it a bit better.

Thanks
 
Old 08-14-2003, 06:51 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
The $? variable contains the exit status of the last command. I just ran two commands:

host www.tamu.edu

and

host blah.blah.blah

Both got the same exit status: 0. www.tamu.edu is a valid url, and the "blah" is obviously bad. Similarly, for dotted notation, both good and bad IP addresses received 0 as return status from the host command.

So, your script will need to be more intelligent. You need to grep the results for specific strings. When host could not resolve, it printed something like:

Host blah.blah.blah not found: 3(NXDOMAIN)

If you grep for "not found:", then that will tell you if the address resolved. In other words:

Code:
#!/bin/bash

# Store the line of text that contains 'not found:'
# from the host command's output
host_resolved=`host $1 | grep "not found:"`

# Did the grep give us a NULL/empty string
if [ -z "$host_resolved" ] ; then
  echo "Host resolved successfully"
  exit 0
fi

# If we got here, that means we didn't exit, which
# means the address didn't resolve.
exit 1

Last edited by Dark_Helmet; 08-14-2003 at 06:52 PM.
 
Old 08-15-2003, 05:36 AM   #6
amaze
LQ Newbie
 
Registered: Aug 2003
Location: UK
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks Dark_Helmet that was a excellent answer.

Thanks again
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Echo onelung02 Slackware 6 08-26-2005 04:50 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM
echo chrismiceli Linux - General 4 04-09-2003 05:25 PM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 01:13 PM
Echo linux_freak Linux - General 1 06-05-2001 02:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration