From 37aba9f6a5d102b42e365161ee3d3e0b6d01ae3b Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 8 Apr 2021 17:09:44 +0200 Subject: [PATCH] improved argument parsing --- scripts/blinky-feed.sh | 45 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/scripts/blinky-feed.sh b/scripts/blinky-feed.sh index 46d2207..c581a3a 100755 --- a/scripts/blinky-feed.sh +++ b/scripts/blinky-feed.sh @@ -3,6 +3,8 @@ # author: Daniel Sommer # license: MIT +# default_ + # blinky blinky_url="127.0.0.1:3000" blinky_mode="morph" @@ -17,11 +19,10 @@ thermal_zones=( "3" ) -# argument to lowercase -arg="${1,,}" - -# functions - +############# +# functions # +############# +# check for root permissions function checkRoot() { [[ "$EUID" != 0 ]] && printf "error: permission denied!\n" && exit 1 } @@ -74,8 +75,36 @@ function getTemperature() { [[ "$result" -ge "$max_temp" ]] && printf "warning: maximum temperature reached\n" && blinky_mode="pulse" && blinky_duration="1500" && return 0 } -# main part -case "$arg" in + +############# +# main part # +############# +argument_count="$#" +index=1 + +[[ "$argument_count" -lt 1 ]] && printf "error: missing arguments\n" && exit 1 + +url="${@: $argument_count: 1}" + +while [[ "$index" -lt $(( $argument_count -1 )) ]]; do + argument="${@: $index: 1}" + argument="${argument,,}" + (( index++ )) + case "$argument" in + -bm|--blinky-mode) + blinky_mode="${@: $index: 1}" + ;; + -bd|--blinky-duration) + blinky_duration="${@: $index: 1}" + ;; + -m|--mode) + mode="${@: $index: 1}" + ;; + esac +done + +printf "checking mode '$mode'...\n" +case "$mode" in -t|--temp|--temperature) checkRoot getTemperature @@ -83,6 +112,6 @@ case "$arg" in sendPOST "color=$result" "mode=$blinky_mode" "duration=$blinky_duration" ;; *) - printf "error: no argument given or argument unknown!\n" && exit 1 + sendPOST "color=random" "mode=$blinky_mode" "duration=$blinky_duration" ;; esac