[arch-general] [PATCH 19/48] Simplify daemon-killing loops in rc.single.

Victor Lowther victor.lowther at gmail.com
Wed Jun 30 17:47:43 EDT 2010


Parsing the output of ls is Bad, especially when globbing works just as well
and does not get confused by odd characters in filenames.

bash has arithemetic for loops. Use them instead of while loops for iterating
over arrays.
---
 rc.single |   29 ++++++++++++-----------------
 1 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/rc.single b/rc.single
index aa58b4c..aa27be0 100755
--- a/rc.single
+++ b/rc.single
@@ -9,23 +9,18 @@
 run_hook single_start
 
 if [[ $PREVLEVEL != N ]]; then
-
-	# Find daemons NOT in the DAEMONS array. Shut these down first
-	if [[ -d /var/run/daemons ]]; then
-		for daemon in $(/bin/ls -1t /var/run/daemons); do
-		  if ! in_array $daemon ${DAEMONS[@]}; then
-				stop_daemon $daemon
-		  fi
-		done
-	fi
-	# Shutdown daemons in reverse order
-	let i=${#DAEMONS[@]}-1
-	while ((i >= 0)); do
-		if [[ ${DAEMONS[$i]:0:1} != '!' ]]; then
-			ck_daemon ${DAEMONS[$i]#@} || stop_daemon ${DAEMONS[$i]#@}
-		fi
-		let i=i-1
-	done
+    # Find daemons NOT in the DAEMONS array. Shut these down first
+    for daemon in /var/run/daemons/*; do
+        [[ -f $daemon ]] || continue
+        daemon=${daemon##*/}
+	in_array "$daemon" "${DAEMONS[@]}" || stop_daemon "$daemon"
+    done
+	
+    # Shutdown daemons in reverse order
+    for ((i=${#DAEMONS[@]}-1; i>=0; i--)); do
+	[[ ${DAEMONS[$i]:0:1} = '!' ]] && continue
+	ck_daemon ${DAEMONS[$i]#@} || stop_daemon ${DAEMONS[$i]#@}
+    done
 
 	# Terminate all processes
 	stat_busy "Sending SIGTERM To Processes"
-- 
1.7.1



More information about the arch-general mailing list