DEV Community

Discussion on: Shellscripting: Conditional Execution

Collapse
thorstenhirsch profile image
Thorsten Hirsch • Edited on

These one-liners are great for early exits (like in assertions), but I wouldn't use 'em in the normal program flow. So my ping script would look like this:

#!/bin/bash

[ -z "$1" ] && echo "host required" && exit 1

ping -c 1
if [ $? -eq 0 ]; then
    echo "$1 reachable."
fi

exit 0

I think it's easier to see what part of the code belongs to error handling and what's the business logic.