Help needed with conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed with conditions
# 1  
Old 05-04-2012
Question Help needed with output-redirection based on output-messages

Hi Folks,

Scenario:
I used the following shell script to see ping status, and got a report out of that based on the standard output value, and it worked:

HTML Code:
for i in `cat /tmp/momin/servers`
do
echo PINGING $i....
ping $i 1
if [ $? -eq 0 ]
then
echo ping test passed!!!
echo $i >> /tmp/momin/passed
echo -------------------------------------------
else
echo ping Failed!
echo $i >> /tmp/momin/failed
echo --------------------------------------------
echo
fi
done
In the output I noticed three types of output messages:
1. ping: unknown host tordb2
2. no answer mondb1
3. vandb7 is alive

The hostnames with error messages 1 and 2 went to the 'failed' report file, and the hostnames with error message 3 were directed to the 'passed' file.

Help Needed Here:
What logic can I use in my shell script / scripted command, to generate 3 files based on the 3 messages? Smilie

Thanks in advance, Smilie
Momin

Last edited by momin; 05-04-2012 at 03:34 PM..
# 2  
Old 05-04-2012
you can write a case statement based on pre known exit codes of the ping command. Assuming 0,1,2 are the exit code of ping you can have something like:
Code:
case $? in  
0) printf  "$i server found\n" >> file1;; 
1) printf  "$i server not found\n" >>file2;; 
2) printf  "$i server doesn't exist\n">>file3;; 
*) printf "Unknown error\n">>file4;; 
esac

# 3  
Old 05-04-2012
Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditions in if

I'm using the below one.. #!/bin/ksh File=$3 if ; then echo "Script" elif ] ;then echo "Passed k or f option" else "Please check the Input passed" fi Command line argument is "k" or -f and file is exist then... (3 Replies)
Discussion started by: Roozo
3 Replies

2. Shell Programming and Scripting

Errors in if conditions with to many OR conditions

Hi ALL I have a script where in i need to check for several values in if conditons but when i execute the script it throws error such as "TOO MANY ARGUMENTS" if then msg="BM VAR Issue :: bmaRequestVAR=$bmaRequestVAR , nltBMVAR=$nltBMVAR , bmaResponseVAR=$bmaResponseVAR ,... (10 Replies)
Discussion started by: nikhil jain
10 Replies

3. Shell Programming and Scripting

awk with conditions

Hi All, I have a file with below contents. "en2"/10.185.81.0:cluster_interconnect,"en5"/10.185.81.0:cluster_interconnect,"en6"/169.181.146.0:public I want to take the interface name from the file and convert it as ipaddress using ifconfig command get the output like below en6 ->... (2 Replies)
Discussion started by: kamauv234
2 Replies

4. Shell Programming and Scripting

If conditions need

Dear Expert, Below code is for to take the backup of database by daily time stamp. I need vital help to make my script automatic sending me email if it sucess or fail. echo on @REM Seamonkey's quick date batch (MMDDYYYY format) @REM Setups %date variable @REM First parses month, day, and... (6 Replies)
Discussion started by: Alone
6 Replies

5. UNIX for Dummies Questions & Answers

If + multiple conditions

Hello Unix-Forums! It has been a long time since my last post, but finally I've got a new question: I know in case you can use multiple patterns by case $var in a|b|c|ab) and so on. But how would I place an OR between if ] then ... if ] then ... I want to execute the "..." if... (3 Replies)
Discussion started by: intelinside
3 Replies

6. Shell Programming and Scripting

IF OR with two conditions

I have this IF working fine, testing if a char is a digit: if ; then _VALUE=$_VALUE$_CHAR else _ISDIGIT="false" fi Then I add a second condition to test if the char is either a digit or a * if ]; then _VALUE=$_VALUE$_CHAR ... (11 Replies)
Discussion started by: Flavius
11 Replies

7. Shell Programming and Scripting

While with three conditions

Currently this is what I am trying while || && ]; do I want to continue if the first condition or both the second and third are true but I am getting a too many arguments error. Can someone help me out? (5 Replies)
Discussion started by: whdr02
5 Replies

8. Shell Programming and Scripting

conditions

./script 89 The script will extract the last digit of the input parameter. example, that is 4. This will be compared to the last digit of the current day of the month ( like day 14; that is 4). A message will displayed on the screen indicating if the digits are the same or not. (1 Reply)
Discussion started by: singh is king
1 Replies

9. UNIX for Dummies Questions & Answers

2 or more if conditions

Hello, I have a file as follows: col no:1 2 3 4 5 6 7 8 9 10 11 a 4 226 226 ch:95024048-95027592, 1y224 of 3545 223 224 ident b 53 235 235 ch:148398-148401255, 1y184 of 3187 180 186 ident awk... (3 Replies)
Discussion started by: dr_sabz
3 Replies

10. Shell Programming and Scripting

if then statements with two conditions...?

Is it possible to setup two conditions for an if then statement in a pbash script? For example, depending on the text value of a variable I parse out of an xml file, I want to assign it a numerical values. Example: if ; then VAR="25" fi if ; then VAR="25" fi Can these two... (3 Replies)
Discussion started by: audiophile
3 Replies
Login or Register to Ask a Question