Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
pan64's point is that you should run that fragment. You do not need to add it to any code, you can run that script fragment as is, by adding a shebang at the top and putting that portion into a temporary script file, debug it and find out if it runs. Per smallpond's comment, you need to properly quote the ping command when you assign the result to a variable, give this a read about some ways to assign the output of a bash command to a variable.
A further suggestion is that you check the manual page for ping(8) where it talks about what ping returns on error. I do believe you are using it correctly, however I always do prefer to double check.
A final suggestion is to put "set -xv" at the top of that script, along with the shebang at the top of the script. Yes I realize you were saying that this is a code fragment, however we recommend that you test just this fragment to validate that you have this section correct.
Last edited by rtmistler; 05-15-2017 at 08:03 AM.
Reason: edit: added reference for command result to variable
you're not passing to $check what you think you are passing to $check, thus why you need to read the links pan64 linked. So to answer the question, no it would not work.
Might consider a different connection method. Ping may be blocked while tcp connections still work. More and more ping is becoming a poor network tool.
Well perhaps the suggestion therefore would be to try multiple contact techniques and have a few if-else tests, once one of them succeeds, then the host is deemed available.
I'm also assuming that their script once it deems a host to be available, tries to contact it using some other protocol. Therefore a test for verification that the next command succeeds would be in order. I tend to code defensively and check return status for all that I can verify.
Not sure this needs to iterate as a discussion over whether or not to use ping. The coder may know their network or their situation and feel that ping is perfectly acceptable, and may always have been. They may be pinging an internal network address, they may be pinging something which is on a purely private network. As an example, chassis based systems for Telco which I've worked on years ago, had internal networks which were 10.x.x.x and they were internal to the chassis. Finding another card meant engaging it using some form of network protocol, one of which could be ping.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.