From cb6cb1e6882ca1620a623bb25b42443d094daf7f Mon Sep 17 00:00:00 2001 From: gafusss Date: Sun, 14 Oct 2018 00:55:01 +0300 Subject: [PATCH] Modified scripts to use sh instead of bash --- scripts/termux-brightness | 10 +++- scripts/termux-call-log | 2 +- scripts/termux-dialog | 31 +++++++------ scripts/termux-media-player | 2 +- scripts/termux-media-scan | 15 +++--- scripts/termux-microphone-record | 46 +++++++++---------- scripts/termux-notification | 6 +-- scripts/termux-sensor | 79 +++++++++++++++++++------------- scripts/termux-toast | 10 ++-- scripts/termux-volume | 11 +++-- scripts/termux-wallpaper | 2 +- 11 files changed, 119 insertions(+), 95 deletions(-) diff --git a/scripts/termux-brightness b/scripts/termux-brightness index 4e19c6a..396ca9c 100755 --- a/scripts/termux-brightness +++ b/scripts/termux-brightness @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/bash +#!/data/data/com.termux/files/usr/bin/sh set -e -u SCRIPTNAME=termux-brightness @@ -12,7 +12,13 @@ if [ $# != 1 ]; then show_usage fi -if ! [[ $1 =~ ^[0-9]+$ ]]; then +case "$1" in + ("" | *[!0-9]*) + echo "ERROR: Arg must be a number between 0 - 255!" + show_usage +esac + +if [ "$1" -lt 0 ] || [ "$1" -gt 255 ]; then echo "ERROR: Arg must be a number between 0 - 255!" show_usage fi diff --git a/scripts/termux-call-log b/scripts/termux-call-log index f890f6b..612f887 100755 --- a/scripts/termux-call-log +++ b/scripts/termux-call-log @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/bash +#!/data/data/com.termux/files/usr/bin/sh set -u PARAM_LIMIT=10 diff --git a/scripts/termux-dialog b/scripts/termux-dialog index 92a4d15..6cbbd9f 100755 --- a/scripts/termux-dialog +++ b/scripts/termux-dialog @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/bash +#!/data/data/com.termux/files/usr/bin/sh set -e -u DEFAULT_WIDGET="text" @@ -13,7 +13,7 @@ show_usage() { exit 0 } -declare -a widgets=("confirm" "checkbox" "counter" "date" "radio" "sheet" "spinner" "speech" "text" "time") +widgets="confirm checkbox counter date radio sheet spinner speech text time" # Descriptions for various options that multiple widgets can use OPT_HINT_DESC="[-i hint] text hint (optional)" @@ -74,7 +74,7 @@ FLAGS=0 # Show usage help for specific widget widget_usage () { - echo -n -e "$1 - " + printf "%s - " "$1" case "$1" in "confirm") @@ -141,7 +141,7 @@ list_widgets() { echo "Supported widgets:" echo - for w in "${widgets[@]}"; do + for w in ${widgets}; do widget_usage $w echo done @@ -149,8 +149,8 @@ list_widgets() { # Checks to see if widgets array contains specified widget has_widget () { - for w in "${widgets[@]}"; do - [ "$w" == "$1" ] && return 0 + for w in ${widgets}; do + [ "$w" = "$1" ] && return 0 done return 1 } @@ -181,7 +181,8 @@ parse_options() { } options_error () { - echo -e "ERROR: Invalid option(s) detected for '$1'\n" + echo "ERROR: Invalid option(s) detected for '$1'" + echo echo "Usage '$1'" widget_usage $1 exit 1 @@ -193,11 +194,15 @@ if [ $# -eq 0 ]; then elif ! has_widget $1; then # we didn't receive a widget as an argument, check to see if we # at least options (even if they're illegal) - if ! [[ $1 =~ -[a-z] ]]; then - echo -e "ERROR: Illegal argument $1\n" - show_usage - fi - WIDGET="$DEFAULT_WIDGET" + case "$1" in + -[a-z]) + WIDGET="$DEFAULT_WIDGET" + ;; + *) + echo "ERROR: Illegal argument $1" + echo + show_usage + esac else WIDGET="$1" shift @@ -244,7 +249,7 @@ case "$WIDGET" in ;; "checkbox" | "radio" | "sheet" | "spinner") - if [ "$ARG_V" == "" ]; then + if [ "$ARG_V" = "" ]; then echo "ERROR: '$WIDGET' must be called with $OPT_VALUES_DESC" exit 1 fi diff --git a/scripts/termux-media-player b/scripts/termux-media-player index 800c962..3835047 100755 --- a/scripts/termux-media-player +++ b/scripts/termux-media-player @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/bash +#!/data/data/com.termux/files/usr/bin/sh set -e SCRIPTNAME=termux-media-player diff --git a/scripts/termux-media-scan b/scripts/termux-media-scan index f2d8c2f..f3aa4d3 100755 --- a/scripts/termux-media-scan +++ b/scripts/termux-media-scan @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/bash +#!/data/data/com.termux/files/usr/bin/sh set -e -u SCRIPTNAME=termux-media-scan @@ -12,14 +12,13 @@ show_usage () { get_paths() { for ARG in "$@"; do - if [[ -e "$ARG" ]]; then + if [ -e "$ARG" ]; then realpath "$ARG" - else - if [[ "$ARG" =~ ^/ ]]; then - echo "$ARG" - else - echo "`pwd`/$ARG" - fi + else + case "$ARG" in + /*) echo "$ARG";; + *) echo "`pwd`/$ARG" + esac fi done } diff --git a/scripts/termux-microphone-record b/scripts/termux-microphone-record index a127ece..899ad88 100755 --- a/scripts/termux-microphone-record +++ b/scripts/termux-microphone-record @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/bash +#!/data/data/com.termux/files/usr/bin/sh set -e SCRIPTNAME=termux-microphone-record @@ -15,6 +15,11 @@ show_usage () { echo "-q Quits recording" } +ARG_F="" +OPT_F="" +ARG_L="" +OPT_L="" + usage_error () { echo show_usage @@ -28,50 +33,45 @@ sole() { fi } -add_params() { - for i in "$@"; do - params[index++]="$i" - done -} - call_api () { /data/data/com.termux/files/usr/libexec/termux-api MicRecorder "$@" + exit 0 } while getopts h,f:,l:,d,i,q option do case "$option" in h) sole "$@" + show_usage + exit 0 ;; - f) record=yes - dir="$(dirname "$OPTARG")" + f) dir="$(dirname "$OPTARG")" file="$(basename "$OPTARG")" dir="$(realpath "$dir")" - add_params --es file "$dir/$file" + ARG_F="--es file" + OPT_F="$dir/$file" ;; # API takes limit in milliseconds - l) record=yes - add_params --ei limit $((OPTARG * 1000)) + l) ARG_L="--ei limit" + OPT_L=$((OPTARG * 1000)) ;; - d) record=yes + d) sole "$@" + call_api -a record ;; i) sole "$@" - add_params -a info + call_api -a info ;; q) sole "$@" - add_params -a quit + call_api -a quit ;; *) usage_error ;; esac done -if [ -v record ]; then - add_params -a record -fi +set -- +if [ -n "$ARG_F" ]; then set -- "$@" $ARG_F "$OPT_F"; fi +if [ -n "$ARG_L" ]; then set -- "$@" $ARG_L "$OPT_L"; fi +set -- "$@" -a record -if [ -v params ]; then - call_api "${params[@]}" -else - show_usage -fi +call_api "$@" diff --git a/scripts/termux-notification b/scripts/termux-notification index bd480c2..6a46f2b 100755 --- a/scripts/termux-notification +++ b/scripts/termux-notification @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/bash +#!/data/data/com.termux/files/usr/bin/sh set -e -u -f SCRIPTNAME=termux-notification @@ -53,7 +53,7 @@ content:,help,id:,\ led-color:,led-on:,led-off:,\ on-delete:,\ priority:,sound,title:,vibrate: \ - -s bash \ + -s sh \ -- "$@"` eval set -- "$TEMP" @@ -101,7 +101,7 @@ if [ -n "$OPT_SOUND" ]; then set -- "$@" --ez sound "$OPT_SOUND"; fi if [ -n "$OPT_TITLE" ]; then set -- "$@" --es title "$OPT_TITLE"; fi if [ -n "$OPT_VIBRATE" ]; then set -- "$@" --ela vibrate "$OPT_VIBRATE"; fi -if [ -v OPT_CONTENT ]; then +if [ ! -z ${OPT_CONTENT+x} ]; then # Note that we want to accept an empty content (--content ""). echo ${OPT_CONTENT:=""} | /data/data/com.termux/files/usr/libexec/termux-api Notification "$@" else diff --git a/scripts/termux-sensor b/scripts/termux-sensor index 2dc73b0..4f031ef 100755 --- a/scripts/termux-sensor +++ b/scripts/termux-sensor @@ -1,5 +1,5 @@ -#!/data/data/com.termux/files/usr/bin/bash -set -e -u +#!/data/data/com.termux/files/usr/bin/sh +set -e SCRIPTNAME=termux-sensor show_usage () { @@ -16,7 +16,7 @@ show_usage () { } # ensure we properly release resources if needed on ^C -trap handle_interrupt SIGTERM SIGINT +trap handle_interrupt TERM INT if [ $# -eq 0 ]; then echo "No arguments supplied!" @@ -24,13 +24,14 @@ if [ $# -eq 0 ]; then exit 1; fi -SENSOR_FLAG=1 -ALL_SENSORS_FLAG=2 -LIST_FLAG=4 -CLEANUP_FLAG=8 -DELAY_FLAG=16 -LIMIT_FLAG=32 -FLAGS=0 +ARG_A="" +OPT_A="" +ARG_S="" +OPT_S="" +ARG_D="" +OPT_D="" +ARG_N="" +OPT_N="" handle_interrupt () { echo "Caught interrupt.. Finishing..." @@ -38,7 +39,7 @@ handle_interrupt () { } cleanup () { - if [ $((FLAGS & (ALL_SENSORS_FLAG | SENSOR_FLAG))) != 0 ]; then + if [ -n "$ARG_A" ] || [ -n "$ARG_S" ]; then echo "Performing sensor cleanup" call_api -a cleanup else @@ -52,10 +53,17 @@ get_sensors () { fi } +is_number () { + case "$1" in + ("" | *[!0-9]*) + return 1 + esac +} + get_delay () { if [ $# -gt 1 ]; then usage_error "Too many arguments for -d delay" - elif ! [[ $1 =~ ^[0-9]+$ ]]; then + elif ! is_number "$1"; then usage_error "Illegal argument! -d delay arg should be a number!" fi } @@ -63,17 +71,14 @@ get_delay () { get_limit () { if [ $# -gt 1 ]; then usage_error "Too many arguments for -n limit" - elif ! [[ $1 =~ ^[0-9]+$ ]]; then + elif ! is_number "$1"; then usage_error "Illegal argument! -n limit arg should be a number!" fi } call_api () { /data/data/com.termux/files/usr/libexec/termux-api Sensor "$@" -} - -set_flag () { - FLAGS=$((FLAGS | $1)); + exit 0 } usage_error () { @@ -82,33 +87,41 @@ usage_error () { exit 1 } +sole() { + if [ "$1 $2" != "-$option $OPTARG" ] || [ $# -gt 2 ]; then + echo "ERROR: No other options should be specified with -$option!" + usage_error + fi +} + -PARAMS=() while getopts :h,a,c,l,s:d:n: option do case "$option" in h) show_usage ;; - a) set_flag $ALL_SENSORS_FLAG; PARAMS+=(-a sensors --ez all true);; - c) set_flag $CLEANUP_FLAG; PARAMS=(-a cleanup) ;; - l) set_flag $LIST_FLAG; PARAMS=(-a list) ;; - s) set_flag $SENSOR_FLAG; get_sensors "$OPTARG"; PARAMS+=(-a sensors --es sensors "$OPTARG") ;; - d) set_flag $DELAY_FLAG; get_delay $OPTARG; PARAMS+=(--ei delay $OPTARG) ;; - n) set_flag $LIMIT_FLAG; get_limit $OPTARG; PARAMS+=(--ei limit $OPTARG) ;; + a) ARG_A="--ez all"; OPT_A="true" ;; + c) sole "$@"; call_api -a cleanup ;; + l) sole "$@"; call_api -a list ;; + s) get_sensors "$OPTARG"; ARG_S="--es sensors"; OPT_S="$OPTARG" ;; + d) get_delay $OPTARG; ARG_D="--ei delay"; OPT_D="$OPTARG" ;; + n) get_limit $OPTARG; ARG_N="--ei limit"; OPT_N="$OPTARG" ;; ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1; esac done -shift $((OPTIND -1)) # Validate options were set properly -if [ $((FLAGS & CLEANUP_FLAG)) -ne 0 ] && [ $FLAGS -ne $CLEANUP_FLAG ]; then - usage_error "No other options should be specified with -c cleanup!" -elif [ $((FLAGS & LIST_FLAG)) -ne 0 ] && [ $FLAGS -ne $LIST_FLAG ]; then - usage_error "No other options should be specified with -l list!" -elif [ $((FLAGS & DELAY_FLAG)) -ne 0 ] && [ $((FLAGS & (ALL_SENSORS_FLAG | SENSOR_FLAG))) -eq 0 ]; then - usage_error "Use -s or -a option when using -d delay!" -elif [ $((FLAGS & ALL_SENSORS_FLAG)) -ne 0 ] && [ $((FLAGS & SENSOR_FLAG)) -ne 0 ]; then +if [ -n "$ARG_A" ] && [ -n "$ARG_S" ]; then usage_error "Listed sensors will be ignored with -a all option!" +elif [ -n "$ARG_D" ] && ! ([ -n "$ARG_A" ] || [ -n "$ARG_S" ]); then + usage_error "Use -s or -a option when using -d delay!" fi -call_api "${PARAMS[@]}" +set -- +if [ -n "$ARG_A" ]; then set -- "$@" $ARG_A "$OPT_A"; fi +if [ -n "$ARG_S" ]; then set -- "$@" $ARG_S "$OPT_S"; fi +if [ -n "$ARG_D" ]; then set -- "$@" $ARG_D "$OPT_D"; fi +if [ -n "$ARG_N" ]; then set -- "$@" $ARG_N "$OPT_N"; fi +set -- "$@" -a sensors + +call_api "$@" diff --git a/scripts/termux-toast b/scripts/termux-toast index 528d40f..9261ee2 100755 --- a/scripts/termux-toast +++ b/scripts/termux-toast @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/bash +#!/data/data/com.termux/files/usr/bin/sh set -e -u SCRIPTNAME=termux-toast @@ -19,10 +19,10 @@ while getopts :hsc:b:g: option do case "$option" in h) show_usage;; - s) PARAMS=" --ez short true";; - c) PARAMS+=" --es text_color $OPTARG";; - b) PARAMS+=" --es background $OPTARG";; - g) PARAMS+=" --es gravity $OPTARG";; + s) PARAMS="$PARAMS --ez short true";; + c) PARAMS="$PARAMS --es text_color $OPTARG";; + b) PARAMS="$PARAMS --es background $OPTARG";; + g) PARAMS="$PARAMS --es gravity $OPTARG";; ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1; esac done diff --git a/scripts/termux-volume b/scripts/termux-volume index 410c771..aaf4a53 100755 --- a/scripts/termux-volume +++ b/scripts/termux-volume @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/bash +#!/data/data/com.termux/files/usr/bin/sh set -e -u SCRIPTNAME=termux-volume @@ -20,10 +20,11 @@ if [ $# -gt 0 ]; then show_usage fi - if ! [[ $2 =~ ^[0-9]+$ ]]; then - echo "ERROR: Volume must be a number" - show_usage - fi + case "$2" in + ("" | *[!0-9]*) + echo "ERROR: Volume must be a number" + show_usage + esac ARGS="-a set-volume --es stream $1 --ei volume $2" fi diff --git a/scripts/termux-wallpaper b/scripts/termux-wallpaper index 58fbdb4..27414d5 100755 --- a/scripts/termux-wallpaper +++ b/scripts/termux-wallpaper @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/bash +#!/data/data/com.termux/files/usr/bin/sh set -e