LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Urgent !!! monitoring directory size using bash (https://www.linuxquestions.org/questions/programming-9/urgent-monitoring-directory-size-using-bash-208147/)

juby 07-22-2004 12:38 AM

Urgent !!! monitoring directory size using bash
 
Hi ppl,
I'm trying to monitor a directory, and whenever its size changes want to do some stuff... This is actually being done to patch a problem when i redeploy a struts application on sun one appserver. This is the code that does it ..


#Calculate the size of application's directory initially
echo -n "Creating usage stamps..."
dusage=0
newdusage=0

#Test if a dir called MY_APP* exists
if [ -d $_APP_SRV_DEPL_HOME/MY_APP* ]
then
dirName=$`ls $_APP_SRV_DEPL_HOME/ | cut -f1`
echo "Name is" $dirName
dusage=`du -s $_APP_SRV_DEPL_HOME/$dirName | cut -f1`
newdusage=`du -s $_APP_SRV_DEPL_HOME/$dirName | cut -f1`
fi
echo "done"

#Monitor change in directory size..

while [ $newdusage -eq $dusage ]
do
if [ -d $_APP_SRV_DEPL_HOME/$dirName ];
then
newdusage=`du -s $_APP_SRV_DEPL_HOME/$dirName | cut -f1`
#echo $newdusage
sleep 1
fi
done

#loop ends

but i get a error like
.............
...........
Creating usage stamps...done
./common_libs.sh: [: -eq: unary operator expected
...............................

Can u tell me whats wrong and is there any better way to do this ?? plzz
I'm not a shell programmer.. so am in bit of trouble :0

osvaldomarques 07-22-2004 01:32 AM

Hi Juby,
The error pointed by you probably is on the line
Code:

while [ $newdusage -eq $dusage ]
This line expects compare 2 numbers. If one of these variables is not numeric because any error occured before, you will have this message. I believe the following line is in error
Code:

dirName=$`ls $_APP_SRV_DEPL_HOME/ | cut -f1`
The dollar sign ("$") just after the equal sign appears to me in excess. If this is the error, you would not have a valid dirName and, by consequence would not have a proper initialization of the variables used in test ("['). By the way the "left square bracket" is a link to /usr/bin/test. To understand how it works, enter "man test".
I'm tweaking with your commands a little. It appears that it may work only if you have just one file in this directory. If one more is left in this directory you will have a mess on standard error and will measure just the size of the first file. That dollar sign will be pre-pended in the first file name, resulting in nothing to be checked by test, which is the reason of the error.
If you for sure wants to evaluate the size of the directory, you just needs to run "du -s" against the directory, forgetting the files. This code would be like
Code:

dusage=`du -s $_APP_SRV_DEPL_HOME | cut -f1`
newdusage=`du -s $_APP_SRV_DEPL_HOME | cut -f1`

Don't forget the second line happens 2 times.

juby 07-22-2004 02:24 AM

Hi,
Thanx a lot,

I corrected the '$' and followed your suggestion , but now i get an error like


Creating usage stamps..../common_libs.sh: [: -eq: unary operator expected

the actual code is posted below..


#Calculate the size of application's directory initially
echo -n "Creating usage stamps..."

#Test if a dir called DOE_TCIS_200* exists
if [ -d $_APP_SRV_DEPL_HOME/DOE_TCIS_200* ]
then
dusage=`du -s $_APP_SRV_DEPL_HOME | cut -f1`
newdusage=`du -s $_APP_SRV_DEPL_HOME | cut -f1`
fi


while [ $newdusage -eq $dusage ]
do
if [ -d $_APP_SRV_DEPL_HOME ];
then
newdusage=`du -s $_APP_SRV_DEPL_HOME | cut -f1`
sleep 1
fi
done

#loop ends

osvaldomarques 07-22-2004 02:39 AM

Try debug your script. To do it, enter
Code:

sh -x common_libs.sh ....
Replace the "...." with the parameters you normally use. Run it in a xterm session and you will see as the shell interprets and processes the script. If you need some assistance, copy and paste the execution in the next post. Please, in this case use the vB code to insert the result as normal html will mess the text. (P.S. vB code is not Visual Basic; it's a special notation in this site to show code lines better.)

juby 07-22-2004 05:47 AM

thanx
 
Hi
thanx a lot man i used the debugging system and solved it.... thanx a lot once again.
Btw it was the problem with the wildcard expansion, the wildcard expansion gave two directories.. that was the problem :)


All times are GMT -5. The time now is 09:32 AM.