I have a much larger script that takes an input file of hosts and determines if we support them and checks to ensure the FQDN coincides with our DNS. For instance, the hostname may return a different FQDN when passed to the "host" command, so I keep the value of the output of the "host" command and also apply some conditions to determine if NXDOMAIN is returned.
In a stunning twist of fate I see that sometimes the file I'm reading returns the ip address of a machine with more than one NIC (and ip address), and usually that NIC is not in operation so it's screwing up my reporting. I need to put a condition in my script that not only checks to see if the ip returns a valid hostname using the "host" command, but now I also need to the script to ping the machine and ensure it is working.
The input file contains a hostname, and three fields following the hostname. Each field is reserved for an IP address - sometimes there's 1 ip, sometimes 2, or 3. I already have a test to determine if the ip address works:
This works fine as is. However now I need to add an additional condition that says "can the host be pinged also? If not exit the loop and move onto the next one". I tried to add this but it's somehow sending an invalid value to the host command:
I tried putting the ping condition in its own brackets separating the conditions by the ampersands but I'm still having a problem. Can anyone suggest how I can implement this additional check?
P.S. I've noticed that you're using a GNU syntax (most likely you're executing the script on Linux),
so the -q option of grep should be available
Oh thank you so much - it works now!!!
Quote:
Originally Posted by Corona688
You don't have to run cut hundreds of times in backticks. Use the read command once.
'host' might be outputting to stderr, try 2>&1
I'm sure my script is very novice and sophmoric and I do plan to learn more but I'm just in a situation where, figuratively (not really), I've got a boss standing over me saying "YOU BETTER GET THOSE REPORTS IN ORDER AND YOU'RE NOT LEAVING THE OFFICE TILL YOU DO" so I'm just trying to do my best to maintain. I will check the suggestion you mentioned and read more on the bash scripting guide. Thank you both!
No insult intended of course, but the difference is important. It's very easy to make excruciatingly slow scripts by running external utilities thousands of times more than you need to instead of learning the proper way first. One of my first shell scripting experiments was a line-wrapper which wrapped lines slower than you could read them due to pipe and external utility abuse...
Also, I apologize for misunderstanding your original question. I thought you meant grep wasn't working, not that it was printing garbage.
Last edited by Corona688; 04-12-2012 at 01:01 PM..
i have to test a condition for n times and if its value is greater than one each time for continuous 5 iterations it will take action
My Approach
run the same command 5 times in for loop and divert the output to a file and then read the values from file and use if else if and take action
... (1 Reply)
Without grep, I'd like to make a test condition so that any a word that does not have the successive letters car in it will be echoed. for example, bluecar will contain "car" so it will show up as a no
var=bluecar
$ echo $var|if ]; then echo "yes";fi
yes
this variable contains "car" so I... (5 Replies)
Dear All,
I need to build a mechanism by which I can log if any node in my DC is pinging my server or not ?
The purpose is al follows:
Incase any node in my Data Center turns off , I need to have a mechanism by which I am notified that a certain node is no longer responding.
On my... (1 Reply)
Hi there,
I tried to search for this almost everywhere, but didnt get any proper information on it.
What is the difference between
]
Some of the code works when I have only single condition i.e.
] && $dothis1 || $dothis2
But if i try to include another testcondition to the... (1 Reply)
Hello,
what is the better and correct way to perform a comparison:
I have been using the following with no problems:
if ]
then ....
fi
I have seen this also used :
if
then ....
fi
When I try :
if
then ....
fi
I get an error like .... the test condition expects a... (4 Replies)
Hi there,
When I try to do a condition on test:
$ str1=abcd
$ test $str1
$ echo $?
0
Is there anyway to display the answer to be 'TRUE' or 'YES'? rather than 0?
If so, how can I do it without using awk or sed. (2 Replies)
Hi ,
I have a doubt on condition test ( ] ). Pls refer blow program.
#!/bin/ksh
TEMP=
if ;then
echo $TEMP
else
print 'invalid option'
fi
Above script's TEMP variable has no value so it gives "invalid option" as output. But I got an error before priting the string .
Result :
... (2 Replies)
Guys look at this:
i have to write a script that takes a file as an argument. The script should be able to determine what permissions the owner, group and everybody has for the file passed in. The output should be displayed similar to this.
READ WRITE EXECUTE
OWNER LEE.BALLANCORE YES YES NO... (9 Replies)
need to check on some hosts and send an email if there status changes
I wanna put together a script in bash that will allow me to check the up/down state of a single host via ping
i want it to run in a continuous loop so I can just fire the script and forget about it(dont want cron to drive... (2 Replies)