summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorJouke Witteveen <j.witteveen@gmail.com>2012-03-01 01:33:12 +0100
committerJouke Witteveen <j.witteveen@gmail.com>2012-03-01 01:35:58 +0100
commiteed3f590c9db7cd9cd4d5fb4722fc7257a278ea6 (patch)
treed34406a8af61dccbb9655b899da30d04ce61a623 /scripts
parent8d1c5e8ec6b637015e84bbb154ece9065c59f1c5 (diff)
downloadnetctl-eed3f590c9db7cd9cd4d5fb4722fc7257a278ea6.tar.gz
netctl-eed3f590c9db7cd9cd4d5fb4722fc7257a278ea6.tar.xz
Code quality upgrade
This is what you get when Dave Reisner points you at some bash anti-patterns. Also in this commit: - updated documentation - bugfix revision of the IPv6 SLAAC address/route bug
Diffstat (limited to 'scripts')
-rw-r--r--scripts/ifplugd.action8
-rwxr-xr-xscripts/netcfg-menu12
-rwxr-xr-xscripts/netcfg-wpa_actiond10
-rwxr-xr-xscripts/wifi-menu73
4 files changed, 52 insertions, 51 deletions
diff --git a/scripts/ifplugd.action b/scripts/ifplugd.action
index 602e61a..573131d 100644
--- a/scripts/ifplugd.action
+++ b/scripts/ifplugd.action
@@ -1,6 +1,6 @@
#!/bin/bash
#
-# ifplugd.action script for netcfg
+# ifplugd.action script for netcfg
. /etc/rc.conf
. /etc/rc.d/functions
@@ -10,8 +10,8 @@ shopt -s extglob
case "$2" in
up)
# Look for a dhcp based profile to try first
- # dhcp can actually outright fail, whereas
- # it's difficult to tell if static succeeded
+ # dhcp can actually outright fail, whereas
+ # it's difficult to tell if static succeeded
# Also check profile is same iface and is right connection
echo "up"
declare -a preferred_profiles
@@ -49,6 +49,6 @@ case "$2" in
*)
echo "Wrong arguments" > /dev/stderr
;;
-esac
+esac
exit 1
diff --git a/scripts/netcfg-menu b/scripts/netcfg-menu
index ff57005..f7c9019 100755
--- a/scripts/netcfg-menu
+++ b/scripts/netcfg-menu
@@ -5,7 +5,7 @@
if [[ ! -x /usr/bin/dialog ]]; then
echo "Please install 'dialog' to use netcfg-menu"
exit 1
-fi
+fi
check_make_state_dir
@@ -18,10 +18,8 @@ i=0
while read prof; do
# if there is a profile called "main", Use as default
[[ "$prof" = "main" ]] && DEFAULT="main"
- profiles[$i]="$prof"
- let i++
- profiles[$i]=$(. "$PROFILE_DIR/$prof"; echo "$DESCRIPTION")
- let i++
+ profiles[i++]="$prof"
+ profiles[i++]=$(. "$PROFILE_DIR/$prof"; echo "$DESCRIPTION")
done < <(list_profiles | sort) # JP: re-use list_profiles instead of duplicating it; avoid subshell we'd get by piping it to the while read...
if [[ ${#profiles} -eq 0 ]]; then
@@ -52,12 +50,12 @@ case $ret in
255) # timeout - use default
profile_up "$DEFAULT" # JP: use profile_up and catch $?
ret=$?
- if [[ $ret -eq 0 ]]; then echo "$DEFAULT" > "$STATE_DIR/menu"; fi
+ (( ret == 0 )) && echo "$DEFAULT" > "$STATE_DIR/menu"
;;
0) # User selection
profile_up "$(cat "$ANSWER")"
ret=$?
- if [[ $ret -eq 0 ]]; then mv "$ANSWER" "$STATE_DIR/menu"; fi
+ (( ret == 0 )) && mv "$ANSWER" "$STATE_DIR/menu"
;;
*) # Shouldnt happen
exit_err "Abnormal ret code from dialog: $ret"
diff --git a/scripts/netcfg-wpa_actiond b/scripts/netcfg-wpa_actiond
index 2e402df..c8a2867 100755
--- a/scripts/netcfg-wpa_actiond
+++ b/scripts/netcfg-wpa_actiond
@@ -43,11 +43,11 @@ fi
[[ -f "$IFACE_DIR/$interface" ]] && source "$IFACE_DIR/$interface"
if [[ -f "$CONN_DIR/interfaces/$interface" ]]; then
- netcfg -i $interface
+ netcfg -i "$interface"
fi
if [[ -n "$RFKILL" ]]; then # Enable radio if necessary
- enable_rf $interface $RFKILL $RFKILL_NAME || exit $?
+ enable_rf "$interface" "$RFKILL" "$RFKILL_NAME" || exit $?
fi
WPA_CONF="$(make_wpa_config_file $interface)"
@@ -65,9 +65,9 @@ fi | while read profile; do
[[ $INTERFACE != $interface ]] && exit 1
# Exclude wpa-config, the wpa_conf is 'complete' and doesn't fit in this scheme
[[ -z "$SECURITY" ]] && SECURITY="none"
- [[ $SECURITY == "wpa-config" ]] && exit 1
-
- echo -e "network={ \n$(make_wpa_config) \nid_str=\"$profile\" \n}" >> $WPA_CONF
+ [[ $SECURITY == "wpa-config" ]] && exit 1
+
+ printf "%s\n" "network={" "$(make_wpa_config)" "id_str=\"$profile\"" "}" >> $WPA_CONF
)
done
diff --git a/scripts/wifi-menu b/scripts/wifi-menu
index fc21a9b..25ac87e 100755
--- a/scripts/wifi-menu
+++ b/scripts/wifi-menu
@@ -46,12 +46,12 @@ init_profiles()
)
case $? in
2)
- GENERATED[${#GENERATED[@]}]="$profile"
+ GENERATED+=("$profile")
;&
1)
- PROFILES[$i]="$profile"
- ESSIDS[$i]="$essid"
- let i++
+ PROFILES[i]=$profile
+ ESSIDS[i]=$essid
+ (( ++i ))
;;
esac
done
@@ -60,29 +60,29 @@ init_profiles()
# Builds ENTRIES as an argument list for dialog based on scan results in $1.
init_entries()
{
- local i=0 flags security signal ssid
+ local i=0 flags signal ssid
while IFS=$'\t' read signal flags ssid; do
- ENTRIES[$i]="--" # $ssid might look like an option to dialog.
- let i++
- ENTRIES[$i]="$ssid"
- let i++
+ ENTRIES[i++]="--" # $ssid might look like an option to dialog.
+ ENTRIES[i++]=$ssid
if inarray "$ssid" "${ESSIDS[@]}"; then
if inarray "$(ssid_to_profile "$ssid")" "${GENERATED[@]}"; then
- ENTRIES[$i]="+" # Automatically generated
+ ENTRIES[i]="+" # Automatically generated
else
- ENTRIES[$i]="*" # Handmade
+ ENTRIES[i]="*" # Handmade
fi
else
- ENTRIES[$i]="-" # Not present
+ ENTRIES[i]="-" # Not present
fi
if [[ "$ssid" = "$CONNECTION" ]]; then
- ENTRIES[$i]="!" # Currently connected
+ ENTRIES[i]="!" # Currently connected
fi
- security="$(expr match "$flags" ".*\(WPA2\|WPA\|WEP\)")"
- : ${security:="NONE"}
- ENTRIES[$i]+=":${security,,}"
- ENTRIES[$i]+=" :$signal"
- let i++
+ if [[ "$flags" =~ WPA2|WPA|WEP ]]; then
+ ENTRIES[i]+=":${BASH_REMATCH[0],,}"
+ else
+ ENTRIES[i]+=":none"
+ fi
+ ENTRIES[i]+=" :$signal"
+ (( ++i ))
done < "$1"
}
@@ -91,8 +91,8 @@ ssid_to_profile()
{
local i
for i in $(seq 0 $((${#ESSIDS[@]}-1))); do
- if [[ "$1" = "${ESSIDS[$i]}" ]]; then
- printf "%s" "${PROFILES[$i]}"
+ if [[ "$1" = "${ESSIDS[i]}" ]]; then
+ printf "%s" "${PROFILES[i]}"
return 0
fi
done
@@ -105,10 +105,13 @@ create_profile()
local flags key msg security
PROFILE="$INTERFACE-$1"
[[ -f "$PROFILE_DIR/$PROFILE" ]] && PROFILE+=".wifi-menu"
- flags="$(grep -m 1 $'\t'"$1\$" "$NETWORKS" | cut -f 2)"
- security="$(expr match "$flags" ".*\(WPA\|WEP\)")"
- : ${security:="NONE"}
- if [[ "$flags" =~ "PSK"|"WEP" ]]; then
+ flags=$(grep -m 1 $'\t'"$1\$" "$NETWORKS" | cut -f 2)
+ if [[ "$flags" =~ WPA|WEP ]]; then
+ security=${BASH_REMATCH[0],,}
+ else
+ security=none
+ fi
+ if [[ "$flags" =~ PSK|WEP ]]; then
msg="Enter $security security key for\n'$1'"
if [[ "$OBSCURE" ]]; then
key=$(wpa_passphrase "$1" "$(dialog --insecure --passwordbox \
@@ -120,13 +123,13 @@ create_profile()
key="KEY='$(dialog --inputbox "$msg" 10 40 --stdout)'"
RETURN=$?
fi
- [[ $RETURN -eq 0 ]] || return $RETURN
+ (( RETURN == 0 )) || return $RETURN
fi
cat << EOF > "$PROFILE_DIR/$PROFILE" || return 4
CONNECTION='wireless'
DESCRIPTION='Automatically generated profile by wifi-menu'
INTERFACE='$INTERFACE'
-SECURITY='${security,,}'
+SECURITY='$security'
ESSID='$1'
IP='dhcp'
$key
@@ -147,12 +150,12 @@ connect_to_ssid()
else
PROFILE=$(create_profile "$1")
RETURN=$?
- [[ $RETURN -eq 0 ]] || return $RETURN
+ (( RETURN == 0 )) || return $RETURN
SPAWNED_PROFILE=1
clear
fi
if ! profile_up "$PROFILE"; then
- if [[ "$SPAWNED_PROFILE" ]]; then
+ if (( SPAWNED_PROFILE )); then
msg=" CONNECTING FAILED
Do you want to keep the generated profile ('$PROFILE')?"
@@ -191,7 +194,7 @@ if [[ $(id -u) -ne 0 ]]; then
exit_stderr "This script needs to be run with root privileges"
fi
-INTERFACE="${1-$WIRELESS_INTERFACE}"
+INTERFACE=${1-$WIRELESS_INTERFACE}
if [[ -z "$INTERFACE" ]]; then
report_err "Missing interface specification"
usage
@@ -200,16 +203,16 @@ fi
cd / # We do not want to spawn anything that can block unmounting
is_interface "$INTERFACE" || exit_fail "No such interface: $INTERFACE"
-if [[ -z "$(ip link show up dev $INTERFACE)" ]]; then
+if [[ -z "$(ip link show up dev "$INTERFACE" 2> /dev/null)" ]]; then
[[ -f "$IFACE_DIR/$INTERFACE" ]] && . "$IFACE_DIR/$INTERFACE"
bring_interface up "$INTERFACE" || exit_fail "Interface unavailable"
SPAWNED_INTERFACE=1
fi
report_try "Scanning for networks"
-CONNECTION=$(expr substr \
- "$(wpa_cli -p "$WPA_CTRL_PATH" -i "$INTERFACE" status \
- 2> /dev/null | grep "^ssid=")" 6 32)
+CONNECTION=$(wpa_cli -p "$WPA_CTRL_PATH" -i "$INTERFACE" status 2> /dev/null \
+ | grep "^ssid=")
+CONNECTION=${CONNECTION#ssid=}
init_profiles "$INTERFACE"
NETWORKS=$(wpa_supplicant_scan_info "$INTERFACE" 3,4,5)
[[ $? -eq 0 ]] && init_entries "$NETWORKS"
@@ -227,7 +230,7 @@ Flags description:
CHOICE=$(dialog --column-separator : --menu "$MSG" 24 50 12 \
"${ENTRIES[@]}" --stdout)
RETURN=$?
- if [[ $RETURN -eq 0 ]]; then
+ if (( RETURN == 0 )); then
connect_to_ssid "$CHOICE"
RETURN=$?
fi
@@ -256,5 +259,5 @@ case $RETURN in
;;
esac
[[ -f "$NETWORKS" ]] && rm -f "$NETWORKS"
-[[ $RETURN -ne 0 && "$SPAWNED_INTERFACE" ]] && bring_interface down "$INTERFACE"
+(( RETURN && SPAWNED_INTERFACE )) && bring_interface down "$INTERFACE"
exit $RETURN