LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Any help with this linux script? (https://www.linuxquestions.org/questions/linux-networking-3/any-help-with-this-linux-script-677882/)

grande25 10-20-2008 03:43 PM

Any help with this linux script?
 
Hi! I'm running this script and it seems to "almost" work. It pings the IP address but when it gets a "1" condition it won't reboot like I want it to. Script:

ping -c 5 123.45.678.910
echo $?
if [ $? -ne 0 ] ; then shutdown -r -t 10 "now"
fi


That's it... I can run "shutdown -r -t 10 "now"" from the root and it works just fine.

Thanks!

indienick 10-20-2008 03:52 PM

The problem is that "echo" is treated as a command. It will print the exit code of the ping command, and then the exit code of the echo command is zero (0).

Something like this might work:
Code:

ping -c5 123.45.678.910

PING_EXIT_CODE=$?

if [ $PING_EXIT_CODE -ne 0 ]
then
  echo $PING_EXIT_CODE
  shutdown -r -t 10 now
fi

------ OR ------

ping -c4 123.45.678.910
if [ $? -ne 0 ]
then
  echo $?
  shutdown -r -t 10 now
fi

I do not know, for sure if the second example will give you the desired results, but the first one will, for sure.

PS. Do not post your question in more than one forum - the most appropriate place for this thread would be in the Programming forum. :)

grande25 10-20-2008 04:22 PM

Thanks!
 
That worked great, indienick! Thanks a bunch!

indienick 10-20-2008 08:38 PM

No problem. Did the second example work as expected?

grande25 10-21-2008 12:55 PM

Yes, the second part worked too
 
Hi indienick,

Yes, the second example worked. I just tested it and will probably leave it like that.

Can you tell me what "echo $?" does in the 4th line?

Also, in the first example, is PING_EXIT_CODE=$? a standard command that's listed in some compendium of Linux commnands?

I've been going nuts trying to find insight on how all this comes together in some simple way but it's been really slow going.

This isn't a regular task for me at work but it would be nice to be able to get something together if I need to.

Thanks again for basically writing the code for me. When I started looking into this, I thought it would be a pretty easy task but it was a serious PITA until you saved the day.

Iz

indienick 10-21-2008 02:23 PM

hah! *blushes* Well, thank you - all in a day's work.

The "echo $?" in the fourth line means to print the value of the special variable "?", which is the exit code of the last command, to screen. Keep in mind, all references to variables in Bash are prefixed with a dollar-sign "$". It works in my example, because there is no command issued between the "ping" command and the "echo $?".

In the first example, "PING_EXIT_CODE=$?" is just a variable assignment - assign the exit code of the previous command (ping) to PING_EXIT_CODE. That way, if you wanted to issue a few more commands before testing the exit code of the ping command, you do not need to worry about the exit code of the ping command being "tainted".

grande25 10-21-2008 02:56 PM

Cool
 
Well now when I look at this stuff I see $? as the bash variable syntax. I had assumed that since a "1" was displayed from the root when pings failed that an echo had taken place and that the logical thing to happen would be for the script to see it and execute a shutdown. But really, I need to tell it to "echo" the $? (1 if ping fails) when -ne 0 and now that it sees a -ne 0 condition it will go ahead and do the shutdown according to the if/then lines.

it still makes my brain hurt. thanks again for the insight.

indienick 10-21-2008 03:08 PM

No problem - just note that the "echo $?" in the second example is completely extraneous! It's meant for "debugging" purposes.

If you wanted to log it, you could substitute "echo $?" for "echo $? >> /path/to/file.log". :)


All times are GMT -5. The time now is 10:45 PM.