LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-22-2004, 01:38 AM   #1
juby
Member
 
Registered: Dec 2002
Location: Bangalore, India
Distribution: Ubuntu 5.*
Posts: 241

Rep: Reputation: 30
Exclamation 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
 
Old 07-22-2004, 02:32 AM   #2
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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.
 
Old 07-22-2004, 03:24 AM   #3
juby
Member
 
Registered: Dec 2002
Location: Bangalore, India
Distribution: Ubuntu 5.*
Posts: 241

Original Poster
Rep: Reputation: 30
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
 
Old 07-22-2004, 03:39 AM   #4
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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.)
 
Old 07-22-2004, 06:47 AM   #5
juby
Member
 
Registered: Dec 2002
Location: Bangalore, India
Distribution: Ubuntu 5.*
Posts: 241

Original Poster
Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH : Total directory size xgreen Programming 7 03-04-2013 06:33 AM
Port Monitoring bash script twantrd Programming 15 01-29-2009 01:59 AM
monitoring directory to get the modified file names appas Programming 1 09-06-2004 05:56 AM
bash script, monitoring running PID? thebover Programming 4 08-26-2004 03:42 PM
Urgent- How to exclude the arp rquests of the monitoring NIC Bassam Linux - Networking 3 02-10-2004 12:56 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration