简体   繁体   中英

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 XXXX >/dev/null || exit;

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

ping -c 1 xxxx >/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.

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.

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

Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM