user2984368
user2984368

Reputation: 33

Ping on bash script, ping if down then exit script

I am making a bash script to configure some devices we use, but im trying to make a log in it, in other words.. when the script starts checks the date, time , user, and other values and then > this values to a csv in a remote server.

I need the ping to check if the server its available, and if it doesnt, then quit program.

so i tried.

ping -c 1 X.X.X.X >/dev/null || exit;

and it worked but i need to echo why it stopped, so i tried:

ping -c 1 x.x.x.x >/dev/null || echo " The remote server is unavailable" ; exit;

Buen when i do this the program exits even if the ping went well..

In less words, im trying after "||" to have an echo and then exit the script, and im not sure how to do it. Everything else works like sharm, and if i remove the echo, the exit; option works fine..

Thanks in advance for your help.

Noobie.

Upvotes: 3

Views: 2248

Answers (2)

alacerda
alacerda

Reputation: 165

Why dont you check $?. If it is 0 pig was successful.

Best regards

Upvotes: 1

devnull
devnull

Reputation: 123498

You can say:

ping -c 1 x.x.x.x >/dev/null || { echo " The remote server is unavailable" ; exit; }

{ ... } denotes command grouping and the commands within it would be executed in a sequence.

; is a command separator. So when you say:

ping -c 1 x.x.x.x >/dev/null || echo " The remote server is unavailable" ; exit; 

exit is invoked regardless of the result of the previous command.

Upvotes: 4

Related Questions