diff --git a/wled b/wled index 062dd35..775914e 100755 --- a/wled +++ b/wled @@ -10,6 +10,10 @@ NAME="wled.sh" COMMAND="wled" VERSION="0.1.2" +# Make the exit status of command pipelines the status of the last command +# with non-zero exit status - if supported by this shell. +(set -o pipefail 2>/dev/null) && set -o pipefail + print_usage() { echo "\ Usage: $COMMAND -f(norcenter)|-w(wohnzimmer)|-a(ll) [on|off] @@ -29,7 +33,13 @@ commands: on|off switch on/off [1-17] switch preset l, list list presets - -b,--brightness [1-255] set brightness" + -b,--brightness [1-255] set brightness + +exit status: + 0 no failure + 1 operating error + 2 child command error + 4 unexpected API response" } curl_fetch() { @@ -54,6 +64,7 @@ active_preset() { arg_inst="$1" arg_cmnd="$2" arg_param="$3" +exit_code=0 # Clear positional arguments. set -- @@ -132,6 +143,7 @@ for wled_host; do # Prepend description and mark active preset. sed --sandbox -e "1i\\Presets on $wled_host:" \ -e "s/^ \?$active_ps:.*/& */" + ;; "") # Missing command. echo "Error: missing command." >&2 @@ -145,15 +157,19 @@ for wled_host; do ;; esac - # Check curl exit status - curl_exit=$? - if test $curl_exit -ne 0; then - echo "Error: curl exited with exit code $curl_exit." >&2 + # Check curl (or maybe sed) exit status + child_exit=$? + if test $child_exit -ne 0; then + echo "Error: curl or sed exited with exit code $child_exit." >&2 + exit_code=$((exit_code|2)) fi - # Check API responce. + # Check API response. if test -n "$api_resp" && echo "$api_resp" | grep -qv '{\s*"success"\s*:\s*true\s*}'; then - echo "Warning: unexpected responce from WLED API." >&2 + echo "Warning: unexpected response from WLED API." >&2 + exit_code=$((exit_code|4)) fi done +exit $exit_code +