[Yum-devel] [PATCH 07/10] Change "[" builtins in yum-cron to use bash "[[ ]]" compound commands.

Matthew Miller mattdm at mattdm.org
Fri Jul 29 02:00:12 UTC 2011


This is for consistency, and because the script is bash-specific anyway.
---
 yum-cron/yum-cron.sh |   30 ++++++++++++++----------------
 1 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/yum-cron/yum-cron.sh b/yum-cron/yum-cron.sh
index 83dc7f6..5ba3be4 100755
--- a/yum-cron/yum-cron.sh
+++ b/yum-cron/yum-cron.sh
@@ -19,7 +19,7 @@ TSLOCK=$LOCKDIR/ts.lock
 SCRIPTDIR=/usr/share/yum-cron/
 
 # If no command line options were given, exit with a usage message.
-if [ -z "$1" ]; then
+if [[ -z "$1" ]]; then
   echo "Usage: yum-cron {update|cleanup|...}"
   exit 1
 else
@@ -28,30 +28,28 @@ fi
 
 # If a command line option was given, it must match a yum script.
 YUMSCRIPT=${SCRIPTDIR}/${ACTION}.yum
-if [ ! -r $YUMSCRIPT ]; then
+if [[ ! -r $YUMSCRIPT ]]; then
   echo "Script for action \"$ACTION\" is not readable in $SCRIPTDIR."
   exit 1
 fi  
 
 # Read the settings from our config file.
-if [ -f /etc/sysconfig/yum-cron ]; then
+if [[ -f /etc/sysconfig/yum-cron ]]; then
   source /etc/sysconfig/yum-cron
 fi
 
 # If no system name is set, use the hostname.
-[ -z "$SYSTEMNAME" ]  && SYSTEMNAME=$( hostname ) 
+[[ -z "$SYSTEMNAME" ]] && SYSTEMNAME=$( hostname ) 
 
 # If DOWNLOAD_ONLY is set, then we force CHECK_ONLY too.
 # Gotta check for updates before we can possibly download them.
-if [ "$DOWNLOAD_ONLY" == "yes" ]; then
-  CHECK_ONLY=yes
-fi
+[[ "$DOWNLOAD_ONLY" == "yes" ]] && CHECK_ONLY=yes
 
 # This holds the output from the "meat" of this script, so that it can
 # be nicely mailed to the configured destination when we're done.
 YUMTMP=$(mktemp /var/run/yum-cron.XXXXXX)
 touch $YUMTMP 
-[ -x /sbin/restorecon ] && /sbin/restorecon $YUMTMP
+[[ -x /sbin/restorecon ]] && /sbin/restorecon $YUMTMP
 
 # Here is the gigantic block of lockfile logic.
 #
@@ -72,7 +70,7 @@ else
   # First, if there's no PID file in the lock directory, something bad has
   # happened.  We can't know the process name, so, clean up the old lockdir
   # and restart.
-  if [ ! -f $LOCKFILE ]; then
+  if [[ ! -f $LOCKFILE ]]; then
     rmdir $LOCKDIR 2>/dev/null
     echo "yum-cron: no lock PID, clearing and restarting myself" >&2
     exec $0 "$@"
@@ -80,7 +78,7 @@ else
   OTHERPID="$(cat "${LOCKFILE}")"
   # if cat wasn't able to read the file anymore, another instance probably is
   # about to remove the lock -- exit, we're *still* locked
-  if [ $? != 0 ]; then
+  if [[ $? != 0 ]]; then
     echo "yum-cron: lock failed, PID ${OTHERPID} is active" >&2
     exit 0
   fi
@@ -94,7 +92,7 @@ else
     # Remove lockfiles more than a day old -- they must be stale.
     find $LOCKDIR -type f -name 'pidfile' -amin +1440 -exec rm -rf $LOCKDIR \;
     # If it's still there, it *wasn't* too old. Bail!
-    if [ -f $LOCKFILE ]; then
+    if [[ -f $LOCKFILE ]]; then
       # Lock is valid and OTHERPID is active -- exit, we're locked!
       echo "yum-cron: lock failed, PID ${OTHERPID} is active" >&2
       exit 0
@@ -122,7 +120,7 @@ fi
         #   nothing special -- just do it
         # Note that in all cases, yum is updated first, and then 
         # everything else.
-        if [ "$CHECK_ONLY" == "yes" ]; then
+        if [[ "$CHECK_ONLY" == "yes" ]]; then
           # TSLOCK is used by the safe-shutdown code in the init script.
           touch $TSLOCK
           /usr/bin/yum $YUM_PARAMETER -e 0 -d 0 -y check-update 1> /dev/null 2>&1
@@ -130,13 +128,13 @@ fi
             1)   exit 1;;
             100) echo "New updates available for host $SYSTEMNAME";
                  /usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y -C check-update
-                 if [ "$DOWNLOAD_ONLY" == "yes" ]; then
+                 if [[ "$DOWNLOAD_ONLY" == "yes" ]]; then
                      /usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y --downloadonly update
                      echo "Updates downloaded. Use \"yum -C update\" manually to install them."
                  fi
                  ;;
           esac
-        elif [ "$CHECK_FIRST" == "yes" ]; then
+        elif [[ "$CHECK_FIRST" == "yes" ]]; then
           # Don't run if we can't access the repos -- if this is set, 
           # and there's a problem, we exit silently (but return an error
           # code).
@@ -162,9 +160,9 @@ fi
 
 } >> $YUMTMP 2>&1
 
-if [ ! -z "$MAILTO" ] && [ -x /bin/mail ]; then 
+if [[ ! -z "$MAILTO" && -x /bin/mail ]]; then 
 # If MAILTO is set, use mail command for prettier output.
-  [ -s "$YUMTMP" ] && mail -s "System update: $SYSTEMNAME" $MAILTO < $YUMTMP 
+  [[ -s "$YUMTMP" ]] && mail -s "System update: $SYSTEMNAME" $MAILTO < $YUMTMP 
 else 
 # The default behavior is to use cron's internal mailing of output.
   cat $YUMTMP
-- 
1.7.6



More information about the Yum-devel mailing list