Disk space check

Hi,

I have a question regarding finding free space on the disk of a solaris machine.

Many mount points are available in my machine. Right now i am using
df -b option to get the free disk space available.

I have an assignment to check free space on the disk.
I pass the directory as a parameter to my shell script. My shell script should read the parameter and check wether there is a sufficient space in the directory.

my inputs can be any of the follwing

/opt/Agent
/var/tmp
/var/opt/Agent
/Agent

I execued my command like this.

sh checkspace.sh /var/tmp

when the above command is executed i am getting correct result because /var/tmp exists on the machine.

if i specify sh checkspace.sh /Agent
the program halts with an error saying

block device not found.

this is because the directory is not existing.

can any one please give a solution to find free disk space when inputs /Agent and /opt/Agent are specified . the code should be same for both inputs.(should also remember /Agent is not an existing directory)

Thanks in advance
RaghuDeep Amilineni

/Agent is not an existing directory??
so....:eek:
should your scipt not validate this before it checks the disk space usage?

can you show script source?

what result do you whant to get from the script? ignore, or error message? or you may whant to see the amount of used space in the folder that not is not a mount point?

Assuming <inputfile> is the name of a file that lists the directories you are interested in (one per line):

while read TESTDIR; do
   if [ -d ${TESTDIR} ]; then
       df -b ${TESTDIR}
   else
      echo "ERROR: ${TESTDIR} does not exist (or at least is not a directory)!"
   fi
done < <inputfile>

Note 1. If you run:
$ df -hl
Lists /var as a separate filesystem but not /var/tmp then a df -b of /var/tmp is really a df of /var.

Note 2. Looks like you have a filesystem or directory called /opt/Agent, your df is failing because it is of /Agent?

HTH

Tony Fuller

Thanks Tony

/opt/Agent is not a file system.

As I said user can enter /opt/Agent ot /Agent

I can check space for /Agent under /(root), and for /opt/Agent under /opt

this will solve my probelm..but our shell script doesnt know what input user will provide.
there should be a common code to handle both /Agent and /opt/Agent

please help in this.

Why didn't you post this under the shell scripting category? Maybe there are more experts in there to advise you faster with "source codes" :slight_smile:

raghu.amilineni,
Sorry did not catch your requirement properly the first time!

The coding is a lot simpler if checkspace.sh script does not limit the user to particular directories as follows (ensure df is included in the PATH) :

#!/bin/ksh

PATH=/bin:/usr/bin

TESTDIR=$1

if [ -z "${TESTDIR}" ]; then
echo "Usage: $0 directoryname"
exit 99
fi

if [ -d ${TESTDIR} ]; then
df -b ${TESTDIR}
else
echo "ERROR: ${TESTDIR} does not exist (or at least is not a directory)!"
fi

If you really want to limit what checkspace.sh will check then try this:

#!/bin/ksh

PATH=/bin:/usr/bin

TESTDIR=$1

if [ -z "${TESTDIR}" ]; then
echo "Usage: $0 directoryname"
exit 99
fi

case ${TESTDIR} in
/opt/Agent|/var/tmp|/var/opt/Agent|/Agent)
if [ -d ${TESTDIR} ]; then
df -b ${TESTDIR}
else
echo "ERROR: ${TESTDIR} does not exist (or at least is not a directory)!"
fi
;;

*\)
	echo "You have specified a directory not in the list:

of:
/opt/Agent
/var/tmp
/var/opt/Agent
/Agent

aborting"
exit 98
;;
esac