Looking at Some Real-World Examples
Here are a few cool examples of functions in action. Enjoy!
Checking Network Connectivity
This function is essentially the same as the one that I’ve just shown you in the previous section. The only real difference is that I’m using an if..then
construct instead of the case..esac
construct.
Also, I placed this function into the actual executable script. But, you can easily move it into a library file if you desire. (I can’t show you the entire script here due to book formatting constraints, but you can download it from the Github repository. It’s the network_func.sh
file.) Anyway, here’s the important part that I really want you to see:
#!/bin/bash
network() {
if [[ $# -eq 0 ]]; then
site="google.com"
else
site="$1"
fi
. . .
. . .
}
network
network www.civicsandpolitics.com
network donnie.com
You see that after I defined the network()
function, I called it...