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 ${daemo...@]}; then
-                               stop_daemon $daemon
-                 fi
-               done
-       fi
-       # Shutdown daemons in reverse order
-       let i=${#daemo...@]}-1
-       while ((i >= 0)); do
-               if [[ ${DAEMONS[$i]:0:1} != '!' ]]; then
-                       ck_daemon ${daemons[$...@} || stop_daemon 
${daemons[$...@}
-               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" "${daemo...@]}" || stop_daemon "$daemon"
+    done
+       
+    # Shutdown daemons in reverse order
+    for ((i=${#daemo...@]}-1; i>=0; i--)); do
+       [[ ${DAEMONS[$i]:0:1} = '!' ]] && continue
+       ck_daemon ${daemons[$...@} || stop_daemon ${daemons[$...@}
+    done
 
        # Terminate all processes
        stat_busy "Sending SIGTERM To Processes"
-- 
1.7.1

Reply via email to