From 5211d4aacb33ed675a939fa094f862f7aa27634d Mon Sep 17 00:00:00 2001 From: dcarrillo Date: Sun, 17 Nov 2019 17:29:34 +0100 Subject: [PATCH] Make shell scripts (almost) POSIX compliant --- .config/polybar/launch.sh | 5 ++- .config/polybar/scripts/check_updates | 4 +- .config/polybar/scripts/check_vpn | 7 ++- .config/polybar/scripts/nmcli_manager | 48 ++++++++------------- .config/polybar/scripts/switch_window_state | 10 ++--- install.sh | 19 ++++---- 6 files changed, 38 insertions(+), 55 deletions(-) diff --git a/.config/polybar/launch.sh b/.config/polybar/launch.sh index 07ea758..e7f27d3 100755 --- a/.config/polybar/launch.sh +++ b/.config/polybar/launch.sh @@ -1,10 +1,11 @@ #!/usr/bin/env bash killall -q polybar -while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done +while pgrep -u "$(id -u)" -x polybar >/dev/null; do sleep 1; done export TERMINAL_CMD="tilix --profile orange --new-process -e" -export WM_CONTROL="$(dirname $0)/scripts/switch_window_state" +WM_CONTROL="$(dirname "$0")/scripts/switch_window_state" +export WM_CONTROL for monitor in $(polybar --list-monitors | cut -d":" -f1); do export MONITOR=$monitor diff --git a/.config/polybar/scripts/check_updates b/.config/polybar/scripts/check_updates index ab71894..07e0469 100755 --- a/.config/polybar/scripts/check_updates +++ b/.config/polybar/scripts/check_updates @@ -3,6 +3,6 @@ while true; do UPDATES=$(checkupdates 2>/dev/null | wc -l) - [[ $UPDATES -gt 0 ]] && echo " $UPDATES" && sleep 30 - [[ $UPDATES -eq 0 ]] && echo "" && sleep 300 + [ "$UPDATES" -gt 0 ] && echo " $UPDATES" && sleep 30 + [ "$UPDATES" -eq 0 ] && echo "" && sleep 300 done diff --git a/.config/polybar/scripts/check_vpn b/.config/polybar/scripts/check_vpn index 9fbfe71..901ab36 100755 --- a/.config/polybar/scripts/check_vpn +++ b/.config/polybar/scripts/check_vpn @@ -3,10 +3,9 @@ OUTPUT="" openvpn=$(pgrep -c openvpn$) -[[ $openvpn -gt 0 ]] && OUTPUT=$(eval printf "%.0s" {1..$openvpn}) +[ "$openvpn" -gt 0 ] && OUTPUT=$(eval printf "%.0s" "{1..$openvpn}") openfortivpn=$(pgrep -c openfortivpn$) -[[ $openfortivpn -gt 0 ]] && OUTPUT=${OUTPUT}$(eval printf '%.0s' {1..$openfortivpn}) - -echo $OUTPUT | sed -e 's/\(.\)/\1 /g' +[ "$openfortivpn" -gt 0 ] && OUTPUT=${OUTPUT}$(eval printf '%.0s' "{1..$openfortivpn}") +echo "$OUTPUT" | sed -e 's/\(.\)/\1 /g' diff --git a/.config/polybar/scripts/nmcli_manager b/.config/polybar/scripts/nmcli_manager index 9173d10..005a4ec 100755 --- a/.config/polybar/scripts/nmcli_manager +++ b/.config/polybar/scripts/nmcli_manager @@ -1,4 +1,3 @@ - #!/usr/bin/env bash # @@ -18,59 +17,46 @@ ROFI="rofi -dmenu -width 30 " -function build_rofi_menu() +build_rofi_menu() { - local menu - local name - local dtype - local device - local option - while read -r line do - name="$(cut -d':' -f1 <<< $line)" - dtype="$(cut -d':' -f2 <<< $line)" - device="$(cut -d':' -f3 <<< $line)" + name="$(echo "$line" | cut -d':' -f1)" + dtype="$(echo "$line" | cut -d':' -f2)" + device="$(echo "$line" | cut -d':' -f3)" - [[ -z $device ]] && status= || status= + [ -z "$device" ] && status= || status= dtype=${dtype##*-} - option=$(printf '%-35s | %-15s | %s\n' "$name" $dtype $status) + option=$(printf '%-35s | %-15s | %s\n' "$name" "$dtype" $status) menu="$menu # $option" done < <(nmcli -c no --terse -f NAME,TYPE,DEVICE connection show) - echo "$(cut -c 3- <<< $menu)" + echo "$menu" | cut -c 3- } -function switch_connections() +switch_connections() { - local name="$1" - local status=$2 + name="$1" + status=$2 - if [[ "$status" == "" ]]; then + if [ "$status" = "" ]; then nmcli connection down "$name" else nmcli connection up "$name" fi } -function main() +main() { - local menu - local choice - local name - local stat - menu=$(build_rofi_menu) - choice=$($ROFI <<< $menu) - [[ $? -ne 0 ]] && return 1 + choice=$(echo "$menu" | $ROFI) + [ "$?" -ne 0 ] && return 1 - name=$(cut -d '|' -f 1 <<< $choice | awk '{$1=$1;print}') - stat=$(cut -d '|' -f 3 <<< $choice | awk '{$1=$1;print}') + name=$(echo "$choice" | cut -d '|' -f 1 | awk '{$1=$1;print}') + stat=$(echo "$choice" | cut -d '|' -f 3 | awk '{$1=$1;print}') - switch_connections "$name" $stat + switch_connections "$name" "$stat" } - main - diff --git a/.config/polybar/scripts/switch_window_state b/.config/polybar/scripts/switch_window_state index b7efa92..929dd09 100755 --- a/.config/polybar/scripts/switch_window_state +++ b/.config/polybar/scripts/switch_window_state @@ -2,16 +2,16 @@ set -e -[[ -z $1 ]] && exit 1 +[ -z "$1" ] && exit 1 active_window=$(xdotool getactivewindow) window=$(xdotool search --name "$1") -[[ $? -ne 0 ]] && exit 1 +[ "$?" -ne 0 ] && exit 1 -if [[ $active_window == $window ]]; then - xdotool windowminimize $window +if [ "$active_window" = "$window" ]; then + xdotool windowminimize "$window" else - xdotool windowactivate $window + xdotool windowactivate "$window" fi exit 0 diff --git a/install.sh b/install.sh index b477216..295d88d 100755 --- a/install.sh +++ b/install.sh @@ -2,7 +2,7 @@ set -e -function dotfiles() +dotfiles() { echo "[INFO] Installing dot files..." @@ -17,29 +17,26 @@ function dotfiles() echo "" } -function dconf_loader() +dconf_loader() { - local file - local dconf_path - - if ! which dconf > /dev/null 2>&1; then + if ! command -v dconf > /dev/null 2>&1; then echo "[WARNING] dconf command not found" echo "" return 1 else for file in dconf/*; do - echo -e "[INFO] Loading $(basename $file) config..." - dconf_path=$(egrep -m1 '^#.+dconf-path=.+$' $file | cut -f2 -d "=") - dconf load $dconf_path < $file + echo "[INFO] Loading $(basename "$file") config..." + dconf_path=$(grep -E -m1 '^#.+dconf-path=.+$' "$file" | cut -f2 -d "=") + dconf load "$dconf_path" < "$file" done fi echo "" } -function main() +main() { - cd $(dirname $0) + cd "$(dirname "$0")" dotfiles dconf_loader