[arch-projects] [PATCH 5/7] standardize syntax in [[ and ((

Tom Gundersen teg at jklm.no
Sat Mar 26 18:15:47 EDT 2011


On Saturday 26 March 2011 16:32:01 Dave Reisner wrote:
> * checks for variable existance should not use -n or quotes
> * avoid using -eq, this is what arithmetic expansion is for
> * always quote the LHS of a [[
> * always quote proper arrays expanded with @
> 
> Signed-off-by: Dave Reisner <d at falconindy.com>
> ---
>  network     |   40 ++++++++++++++++++++--------------------
>  rc.shutdown |    6 +++---
>  rc.single   |    2 +-
>  rc.sysinit  |   18 +++++++++---------
>  4 files changed, 33 insertions(+), 33 deletions(-)
> 
> diff --git a/network b/network
> index 5a261b4..27df68a 100755
> --- a/network
> +++ b/network
> @@ -69,7 +69,7 @@ ifdown() {
>  }
> 
>  iflist() {
> -	for ifline in ${INTERFACES[@]}; do
> +	for ifline in "${INTERFACES[@]}"; do
>  		if [[ $ifline = ${ifline#!} ]]; then
>  			printf " $ifline:\t"
>  		else
> @@ -110,8 +110,8 @@ rtdown() {
>  }
> 
>  rtlist() {
> -	for rtline in ${ROUTES[@]}; do
> -		if [[ $rtline = ${rtline#!} ]]; then
> +	for rtline in "${ROUTES[@]}"; do
> +		if [[ "$rtline" = ${rtline#!} ]]; then
>  			printf " $rtline:\t"
>  		else
>  			printf "$rtline:\t"
> @@ -121,8 +121,8 @@ rtlist() {
>  }
> 
>  bond_up() {
> -	for ifline in ${BOND_INTERFACES[@]}; do
> -		if [[ $ifline = ${ifline#!} ]]; then
> +	for ifline in "${BOND_INTERFACES[@]}"; do
> +		if [[ "$ifline" = ${ifline#!} ]]; then
>  			bondcfg="bond_$ifline"
>  			if [[ ${!bondcfg} ]]; then
>  				/sbin/ifenslave $ifline ${!bondcfg} || error=1
> @@ -132,8 +132,8 @@ bond_up() {
>  }
> 
>  bond_down() {
> -	for ifline in ${BOND_INTERFACES[@]}; do
> -		if [[ $ifline = ${ifline#!} ]]; then
> +	for ifline in "${BOND_INTERFACES[@]}"; do
> +		if [[ "$ifline" = ${ifline#!} ]]; then
>  			bondcfg="bond_$ifline"
>  			/sbin/ifenslave -d $ifline ${!bondcfg} || error=1
>  		fi
> @@ -141,8 +141,8 @@ bond_down() {
>  }
> 
>  bridge_up() {
> -	for br in ${BRIDGE_INTERFACES[@]}; do
> -		if [[ $br = ${br#!} ]]; then
> +	for br in "${BRIDGE_INTERFACES[@]}"; do
> +		if [[ "$br" = ${br#!} ]]; then
>  			# if the bridge already exists, remove it
>  			if [[ $(/sbin/ifconfig $br 2>/dev/null) ]]; then
>  				/sbin/ifconfig $br down
> @@ -151,9 +151,9 @@ bridge_up() {
>  			/usr/sbin/brctl addbr $br
>  			brifs="bridge_$br"
>  			for brif in ${!brifs}; do
> -				if [[ $brif = ${brif#!} ]]; then
> +				if [[ "$brif" = ${brif#!} ]]; then
>  					for ifline in ${BOND_INTERFACES[@]}; do
> -						if [[ $brif = $ifline && $ifline = ${ifline#!} ]]; then
> +						if [[ "$brif" = $ifline && "$ifline" = ${ifline#!} ]]; 
then
>  							ifup $ifline
>  							bondcfg="bond_$ifline"
>  							/sbin/ifenslave $ifline ${!bondcfg} || error=1
> @@ -169,8 +169,8 @@ bridge_up() {
>  }
> 
>  bridge_down() {
> -	for br in ${BRIDGE_INTERFACES[@]}; do
> -		if [[ $br = ${br#!} ]]; then
> +	for br in "${BRIDGE_INTERFACES[@]}"; do
> +		if [[ "$br" = ${br#!} ]]; then
>  			/usr/sbin/brctl delbr $br
>  		fi
>  	done
> @@ -189,8 +189,8 @@ case "$1" in
>  		# bring up bridge interfaces
>  		bridge_up
>  		# bring up ethernet interfaces
> -		for ifline in ${INTERFACES[@]}; do
> -			if [[ $ifline = ${ifline#!} ]]; then
> +		for ifline in "${INTERFACES[@]}"; do
> +			if [[ "$ifline" = ${ifline#!} ]]; then
>  				ifup $ifline || error=1
>  			fi
>  		done
> @@ -198,7 +198,7 @@ case "$1" in
>  		bond_up
>  		# bring up routes
>  		for rtline in "${ROUTES[@]}"; do
> -			if [ "$rtline" = "${rtline#!}" ]; then
> +			if [[ "$rtline" = ${rtline#!} ]]; then
>  				rtup $rtline || error=1
>  			fi
>  		done
> @@ -215,7 +215,7 @@ case "$1" in
>  		#	exit
>  		#fi
> 
> -		if [[ $NETWORK_PERSIST =~ yes|YES && $RUNLEVEL == [06] ]]; then
> +		if [[ "$NETWORK_PERSIST" =~ yes|YES && "$RUNLEVEL" = [06] ]]; then
>  			status "Skipping Network Shutdown" true
>  			exit 0
>  		fi
> @@ -224,20 +224,20 @@ case "$1" in
>  		rm_daemon network
>  		error=0
>  		for rtline in "${ROUTES[@]}"; do
> -			if [[ $rtline = ${rtline#!} ]]; then
> +			if [[ "$rtline" = ${rtline#!} ]]; then
>  				rtdown $rtline || error=1
>  			fi
>  		done
>  		# bring down bond interfaces
>  		bond_down
>  		for ifline in ${INTERFACES[@]}; do
> -			if [[ $ifline = ${ifline#!} ]]; then
> +			if [[ "$ifline" = ${ifline#!} ]]; then
>  				ifdown $ifline || error=1
>  			fi
>  		done
>  		# bring down bridge interfaces
>  		bridge_down
> -		if ((error == 0)); then
> +		if (( error == 0 )); then
>  			stat_done
>  		else
>  			stat_fail
> diff --git a/rc.shutdown b/rc.shutdown
> index 499459f..9a04014 100755
> --- a/rc.shutdown
> +++ b/rc.shutdown
> @@ -61,7 +61,7 @@ stat_busy "Deactivating Swap"
>  stat_done
> 
>  # stop monitoring of lvm2 groups before unmounting filesystems
> -if [[ $USELVM =~ yes|YES && -x /sbin/lvm && -d /sys/block ]]; then
> +if [[ "$USELVM" =~ yes|YES && -x /sbin/lvm && -d /sys/block ]]; then
>  	stat_busy "Deactivating monitoring of LVM2 groups"
>  	/sbin/vgchange --monitor n >/dev/null 2>&1
>  	stat_done
> @@ -86,7 +86,7 @@ if [[ -f /etc/crypttab && -n $CS ]] && /bin/grep -q ^[^#]
> /etc/crypttab; then stat_done
>  fi
> 
> -if [[ $USELVM =~ yes|YES && -x /sbin/lvm && -d /sys/block ]]; then
> +if [[ "$USELVM" =~ yes|YES && -x /sbin/lvm && -d /sys/block ]]; then
>  	stat_busy "Deactivating LVM2 groups"
>  	/sbin/vgchange --sysinit -a n >/dev/null 2>&1
>  	stat_done
> @@ -100,7 +100,7 @@ run_hook shutdown_poweroff
> 
>  # Power off or reboot
>  printsep
> -if [[ $RUNLEVEL = 0 ]]; then
> +if (( RUNLEVEL == 0 )); then
>  	printhl "${C_H2}POWER OFF"
>  	/sbin/poweroff -d -f -h -i
>  else
> diff --git a/rc.single b/rc.single
> index aab2b58..de50a47 100755
> --- a/rc.single
> +++ b/rc.single
> @@ -46,7 +46,7 @@ fi
> 
>  run_hook single_end
> 
> -if [[ $RUNLEVEL = 1 ]]; then
> +if (( RUNLEVEL == 1 )); then
>  	printsep
>  	printhl "Entering single-user mode..."
>  	# make sure /dev/initctl is in place
> diff --git a/rc.sysinit b/rc.sysinit
> index 3d0eb02..b08d927 100755
> --- a/rc.sysinit
> +++ b/rc.sysinit
> @@ -33,7 +33,7 @@ fi
> 
>  # anything more serious than KERN_WARNING goes to the console
>  # 'verbose' cmdline parameter enables more messages
> -if [[ -n "$verbose" ]]; then
> +if [[ $verbose ]]; then
>  	/bin/dmesg -n 8
>  else
>  	/bin/dmesg -n 3
> @@ -162,11 +162,11 @@ if [[ -f /etc/crypttab && -n $CS ]] && /bin/grep -q
> ^[^#] /etc/crypttab; then # This sanity check _should_ be sufficient, but
> it might not. # This may cause dataloss if it is not used carefully.
>  						/sbin/blkid -p "$2" &>/dev/null
> -						if [[ $? -eq 2 ]]; then
> +						if (( $? == 2 )); then
>  							_overwriteokay=1
>  						fi
>  					fi
> -					if [[ $_overwriteokay -eq 0 ]]; then
> +					if (( _overwriteokay == 0 )); then
>  						false
>  					elif $CS -d /dev/urandom $4 $open "$a" "$b" >/dev/null; 
then
>  						stat_append "creating swapspace.."
> @@ -204,7 +204,7 @@ if [[ -f /etc/crypttab && -n $CS ]] && /bin/grep -q
> ^[^#] /etc/crypttab; then *)
>  					echo "$3" | $CS $4 $open "$a" "$b" >/dev/null;;
>  			esac
> -			if (($? != 0)); then
> +			if (( $? != 0 )); then
>  				failed=1
>  				stat_append "failed "
>  			else
> @@ -218,7 +218,7 @@ if [[ -f /etc/crypttab && -n $CS ]] && /bin/grep -q
> ^[^#] /etc/crypttab; then else
>  		stat_fail
>  	fi
> -	if [[ ${crypto_unlocked} -eq 1 ]]; then
> +	if (( crypto_unlocked == 1 )); then
>  		# Maybe someone has LVM on an encrypted block device
>  		activate_vgs
>  	fi
> @@ -246,11 +246,11 @@ if [[ -x /sbin/fsck ]]; then
>  	run_hook sysinit_prefsck
>  	/sbin/fsck -A -T -C$FSCK_FD -a -t "$NETFS,noopts=_netdev" $FORCEFSCK
> >$FSCK_OUT 2>$FSCK_ERR fsckret=$?
> -	if ((fsckret > 1)); then
> +	if (( fsckret > 1 )); then
>  		stat_fail
>  	fi
>  	run_hook sysinit_postfsck
> -	if (( ( fsckret & 2) == 2)); then
> +	if (( (fsckret & 2) == 2 )); then
>  		echo
>  		echo "********************** REBOOT REQUIRED *********************"
>  		echo "*                                                          *"
> @@ -260,7 +260,7 @@ if [[ -x /sbin/fsck ]]; then
>  		echo
>  		/bin/sleep 15
>  		fsck_reboot
> -	elif ((fsckret > 1 && fsckret != 32)); then
> +	elif (( fsckret > 1 && fsckret != 32 )); then
>  		echo
>  		echo "*****************  FILESYSTEM CHECK FAILED  ****************"
>  		echo "*                                                          *"
> @@ -280,7 +280,7 @@ fi
> 
>  stat_busy "Mounting Local Filesystems"
>  /bin/mount -n -o remount,rw /
> -if [ -x /bin/findmnt -a -e /proc/self/mountinfo ]; then
> +if [[ -x /bin/findmnt && -e /proc/self/mountinfo ]]; then
>  	/bin/findmnt -rnu -o SOURCE,TARGET,FSTYPE,OPTIONS >| /etc/mtab
>  else
>  	cat /proc/mounts >| /etc/mtab

Looks fine to me, and I trust your judgement of what bashisms are the best 
ones...

-t


More information about the arch-projects mailing list