Bash for/if/else

From Notes

Jump to: navigation, search
#!/bin/bash

TARGET="host1 host2 host3 host4"

for T in ${TARGET}
do
  echo "Starting ${T}"
  RES=$(echo ${T} | grep -c 24)
  if [ ${RES} -gt 0 ]
  then
    echo "Not syncing ${T}"
  else
    echo "Syncing ${T}"
  fi
done


You can also use elif (else if):

if [ -f /etc/openldap/ldap.conf ]
then
  echo "file exists"
elif [ -d /etc/openldap/slapd.d ]
then
  echo "directory exists"
fi
Personal tools