How to input the return value (1 or 0) ping cmd to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to input the return value (1 or 0) ping cmd to a variable
# 1  
Old 12-12-2010
How to input the return value (1 or 0) ping cmd to a variable

Hi

I would like to ask about my plan script

I have this several workstation want to monitor and execute a command without logging it we use "rsh $host "<command>" i create csh script using foreach to loop my several workstation, my problem with the rsh command is if it encounter a workstation that is being down or move therefore no connection the rsh cmd stop or even not continue. here my initial script and my problem:

Code:
#!/usr/bin/csh -f

foreach host(host1 host2 host.....)
ping -t 30 $host 5 > /dev/null
set status = ( I want the return value of the ping to be the "status" variable )
    if( status == 0 ) then
        rsh $host " uname -a"
    else
        echo " ---$host unavailable---"
    endif

echo " ----- END -----"
sleep 1

end

Any suggestion would be greatly appreciated,
thanks in advance

Last edited by DukeNuke2; 12-12-2010 at 08:26 AM..
# 2  
Old 12-12-2010
Code:
set status = $?
if ( $status ......

# 3  
Old 12-12-2010
By the way thanks for the reply i solve my problem by just not using a variable i guess csh automatically declare the return values of the previous command as status variable
Code:
#!/usr/bin/csh -f
foreach host(host1 host2 host.....)
ping -t 30 $host 5 >& /dev/null
    if( status == 0 ) then
        rsh $host " uname -a"
    else
        echo " ---$host unavailable---"
    endif

echo " ----- END -----"
sleep 1

end

I used redirection for the stdout and stderr so that no ping result command appears in my script

Last edited by Scott; 12-12-2010 at 06:08 PM.. Reason: Code tags, please...
# 4  
Old 12-12-2010
Quote:
Originally Posted by jao_madn
i guess csh automatically declare the return values of the previous command as status variable
So it does! Who'd have thunk it?

I still think you need the $, though.

Code:
if( $status == 0 ) then

This User Gave Thanks to Scott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

passing input into a cmd line that's waiting for a response

Hi, I am writing a little script to update a parameters on JMQ. however the JMQ requires a "y" confirmation to be input as part of the cmd I am running. However I want run this script to offline with no input from a user. it works if a I create a file with with just y in it and pass that in... (3 Replies)
Discussion started by: shropshirehobbi
3 Replies

2. Shell Programming and Scripting

run cmd based on input

Hi, This is what I have so far but it seems like a lot more than is necessary because....for example...user presses 2 or 3 ..... the script does the *same* thing it just depends on the directory it has to access....how can I improve this so that the code from 2 and 3 is only put once...? ... (4 Replies)
Discussion started by: holyearth
4 Replies

3. UNIX for Dummies Questions & Answers

input URL into cmd with a alias

I want to run wget "URL" -SO /dev/null 2>&1 | grep "HTTP/\\|Age:\\|Last-Modified:" but I want a alias so I can just type mywget and the URL and it will put the url in the right place and give me the output that I want without having to type that over and over again.:wall: I am newbie to all... (2 Replies)
Discussion started by: splitradius
2 Replies

4. Programming

How to take input from cmd line into file

Hi, I want to be able to write a simple program that takes in input from the command line. I;m am at the level of getchar and putchar. Any examples would be a great help thanks. I intend/prefer also to use the pipe command | eg: input | file1 ---------- Post updated at 04:08 PM ----------... (4 Replies)
Discussion started by: metros
4 Replies

5. Shell Programming and Scripting

perl return ips after successful ping

Hi, I have this script in ksh, what it does is loop every ip in the nodes_nso and produced another variable up_nodes_nso of only ip's that are up. nodes_nso=$(cat /var/tmp/nodes.txt) echo "ICMP Tests:" up_nodes_nso="" for ip in ${nodes_nso} ; do ping ${ip} 3 > /dev/null if ; then ... (1 Reply)
Discussion started by: borderblaster
1 Replies

6. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

7. Shell Programming and Scripting

ksh: cmd output to input of another script

I want to write a script in KSH that takes the output of one command and redisplays it. Something like: while true do read inpt date +"%I:%M:%S %p <-> $inpt" done and then some how get the output of the ping command to redirect to the input of this script. does that make sense? (2 Replies)
Discussion started by: IMTheNachoMan
2 Replies

8. UNIX for Dummies Questions & Answers

Is there way to ping and return boolean answer?

Hello im using sunos and i need to somehow ping other sun in the network but geting boolean return and not the "sun is alive" response can it be done ? (11 Replies)
Discussion started by: umen
11 Replies

9. Programming

How to get system() function executed cmd return value ?

Hi, How I can get system function executed command return value ? I want to know mv command success or not ? #include <stdio.h> main() { int ret; ret = system( "mv x.dat y.dat" ); printf( "system ret:\n", ret ); } (3 Replies)
Discussion started by: haiudhaya
3 Replies

10. Shell Programming and Scripting

perl "system" cmd return values..

perl 5.6.1: when i try a "system" command(with if loops for $?), i get this: child exited with value 1 what is meant by this $? values and what does it meant if it returns 1?.. (0 Replies)
Discussion started by: sekar sundaram
0 Replies
Login or Register to Ask a Question