Simplest Enterprise Continuous Integration Solutions

Saturday, November 12, 2011

Enterprise Linux - get /tmp size

#!/bin/sh
# This script uses to get the available /tmp size from /bin/df -h in GB

# Option 1:

# Normal case: use /bin/awk to get the available /tmp size from /bin/df -h
# if [[ `/bin/df -h /tmp | grep tmp` =~ "tmp" ]]; then
#    AVAILABLE_DISKSPACE=`/bin/df -h /tmp | /bin/grep tmp | /bin/awk '{print$4}'`
# else
#    AVAILABLE_DISKSPACE=`/bin/df -h /tmp | /bin/grep ^[[:space:]] | /bin/awk '{print$3}'`
# fi

# Option 2:

# No single quotes allowed case: use /bin/sed to get the available /tmp size from /bin/df -h
if [[ `/bin/df -h /tmp | grep tmp` =~ "tmp" ]]; then
   AVAILABLE_DISKSPACE=`/bin/df -h /tmp | /bin/grep tmp | /bin/sed -e "s/^\S*\s*\S*\s*\S*\s*\(\S*\)\s*\S*\s*\S*$/\1/g"`
else
   AVAILABLE_DISKSPACE=`/bin/df -h /tmp | /bin/grep ^[[:space:]] | /bin/sed -e "s/^\s*\S*\s*\S*\s*\(\S*\)\s*\S*\s*\S*$/\1/g"`
fi
if [[ ${AVAILABLE_DISKSPACE} =~ ".*K$" ]]; then
   TMP_SIZE_IN_GB=${AVAILABLE_DISKSPACE%K}
   TMP_SIZE_IN_GB=(${TMP_SIZE_IN_GB} / 1048576)
elif [[ ${AVAILABLE_DISKSPACE} =~ ".*M$" ]]; then
   TMP_SIZE_IN_GB=${AVAILABLE_DISKSPACE%M}
   TMP_SIZE_IN_GB=(${TMP_SIZE_IN_GB} / 1024)
elif [[ ${AVAILABLE_DISKSPACE} =~ ".*G$" ]]; then
   TMP_SIZE_IN_GB=${AVAILABLE_DISKSPACE%G}
elif [[ ${AVAILABLE_DISKSPACE} =~ ".*T$" ]]; then
   TMP_SIZE_IN_GB=${AVAILABLE_DISKSPACE%T}
   TMP_SIZE_IN_GB=(${TMP_SIZE_IN_GB} * 1024)
fi
echo "Available /tmp disk space (GB): ${TMP_SIZE_IN_GB}"