1
0
mirror of https://github.com/dcarrillo/dotfiles.git synced 2025-07-01 19:49:25 +00:00

Make shell scripts (almost) POSIX compliant

This commit is contained in:
2019-11-17 17:29:34 +01:00
parent 9d6bbeae7c
commit 5211d4aacb
6 changed files with 38 additions and 55 deletions

View File

@ -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