Skip to main content

Posts

Showing posts with the label bash

Shell script to wait until your site becomes reachable and returns code 200

Sometimes you don't know when a server becomes reachable and you just need to wait, while continuously pinging it with a certain interval. This is a shell script that does everything for you (including a timeout if host is not available during a specified period of time). It accepts two parameters: hostname of your site and a timeout in seconds.

Accepting long options with getopts in your shell script

Normally you can only have single-character options when processing your arguments with a standard getopts utility in Linux or Mac OS X. You can use getopt for long options, but it is not included in the standard Mac OS X installation. So this is a trick that will allow you to simulate long options with a standard getopts, that will work on all *nix platforms without additional software installations. This function will also print a usage tip if you execute the script without parameters (or with -h parameter).