Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 921
user2984368 Avatar asked Nov 19 '25 05:11

user2984368


1 Answers

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.

like image 111
devnull Avatar answered Nov 21 '25 19:11

devnull



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!