If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > bash script problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-08-04, 10:07
thegrave thegrave is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
bash script problem

I have a problem with a script.The first argument is the directory, the second is a number.This number is a certain amount of space that has to be verified if it is available on the partition at which the directory(first arguement) is placed.I know that I have to use df for this but how should I take the results from the third column of the output of df(used space) and compare it with number given by the second argument.The syntax is sh <program> <dir> <space> if someone hadn't already gotten it.Here's what I have written till now:

#!/bin/sh
echo " THIS SCRIPT IS CHECKING FOR AVAILABLE FREE SPACE!"

if [ $# -ge 3 ]; then
echo "Too Many Arguments!"
exit
fi

if [ -n $1 ]; then
if [ -d $1 ]; then
echo "$1 is a dir"
else
echo "$1 is not a dir"
fi
fi

if [ -n $2 ]; then
if [ $2 -gt 0 ]; then
echo "Size is correct"
else
echo "Size is incorrect"
fi
fi
df
##if
##echo "There is enough free space"
##else
##echo "There's not enough free space"

Hope someone could help!
Reply With Quote
  #2 (permalink)  
Old 06-08-04, 11:56
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
I do it like this :
My output from df -v looks like this :
SCO_cd000000 > df -vt
Mount Dir Filesystem blocks used free %used
/ /dev/root 2000002 690454 1309548 35%
/stand /dev/boot 40000 24358 15642 61%
/k1 /dev/k1 4000000 442596 3557404 12%
/k2 /dev/k2 4000000 663738 3336262 17%
/usr2 /dev/u1 6973998 1358770 5615228 20%

and this is the script:

ERR=`df -v | grep $1 | awk -v space=$2 ' BEGIN { FS=" " }
{
if ( $5 > space )
{
print "Space OK"
}
}'`
if [ -n "$ERR" ]
then
echo $1" have enough space"
else
echo $1" have not enough space"
fi
__________________
Greetings from germany
Peter F.
Reply With Quote
  #3 (permalink)  
Old 06-08-04, 14:42
thegrave thegrave is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
Thank you a lot for your help fla5do!It almost works!Here is my output from df -v
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hdb3 6955888 5623348 973492 86% /
/dev/hdb1 44280352 28834528 15445824 66% /fat1
/dev/hdb2 7142988 6437144 705844 91% /fat2
/dev/hda2 140895776 95028064 45867712 68% /fatbig
/dev/hda1 14876196 1306324 12801968 10% /second

Here's how I modified the code:
ERR=`df -v | grep $1 | awk -v space=$2 ' BEGIN { FS=" " }
{
if ( $4 > space)
{
print "Space OK"
}
}'`
if [ -n "$ERR" ]; then
echo $1" have enough free space"
else
echo $1" have not enough free space"
But it always returns that there's no free space regardless the input.Do you have any idea where's the mistake?
Reply With Quote
  #4 (permalink)  
Old 06-08-04, 17:06
thegrave thegrave is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
The problem transformed in awk one.Here's the deal: I haven't imposed the fact that I have to check at which partition exactly is the directory.I mean the following: let's say you have several hdd mounted at /, /hdd1, /hdd2, etc.The directory for example is /hdd2/home/whatever.I can't find the way for extracting only the valuable info.That's because both / and /hdd2 are included in /hdd2/home/whatever.So I have to find with awk where exactly is the direcorty at and after that make the checking for avaiable space.So I am at this stage now:
cd $1
dir=`pwd -P`
ERR=`df -v | awk -v space=$2 -v DIR=$dir ' BEGIN { FS=" " }
Reply With Quote
  #5 (permalink)  
Old 06-08-04, 17:39
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Hi,
I test it with your output.

#Filesystem 1K-blocks Used Available Use% Mounted on
#/dev/hdb3 6955888 5623348 973492 86% /
#/dev/hdb1 44280352 28834528 15445824 66% /fat1
#/dev/hdb2 7142988 6437144 705844 91% /fat2
#/dev/hda2 140895776 95028064 45867712 68% /fatbig
#/dev/hda1 14876196 1306324 12801968 10% /second


I can not identify any error. The change from $5 to $4 is OK.

Try it with following commands again.

The command is :

<script> hdb3 1000000 //// result : hdb3 have not enough free space

<script> hdb3 100000 //// result :hdb3 have enough free space
__________________
Greetings from germany
Peter F.
Reply With Quote
  #6 (permalink)  
Old 06-08-04, 17:54
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Quote:
Originally Posted by thegrave
The problem transformed in awk one.Here's the deal: I haven't imposed the fact that I have to check at which partition exactly is the directory.I mean the following: let's say you have several hdd mounted at /, /hdd1, /hdd2, etc.The directory for example is /hdd2/home/whatever.I can't find the way for extracting only the valuable info.That's because both / and /hdd2 are included in /hdd2/home/whatever.So I have to find with awk where exactly is the direcorty at and after that make the checking for avaiable space.So I am at this stage now:
cd $1
dir=`pwd -P`
ERR=`df -v | awk -v space=$2 -v DIR=$dir ' BEGIN { FS=" " }
The output from "df" command is the same for every User and every aktive Direktory. Surely : only mounted directorys and filesystems are shown in "df" command
__________________
Greetings from germany
Peter F.

Last edited by fla5do; 06-09-04 at 02:17.
Reply With Quote
  #7 (permalink)  
Old 06-08-04, 18:12
thegrave thegrave is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
Yes dude, what you've written works fine but the problem as I said in my previuos post is that if I write <script> Maildir 1234 for instance and as I'm not root on the mashine my home folder is / but the physical is /home/me for instance.That's why I get the physical folder and then I have to check at which partition this physical folder is, got it?I have to do this with awk, it's really simple but I'm having some troubles with the syntax.I just have to compare the strings for instance /fat1/home/me/Maildir with the ones in the sixth column and to find out at which partition it is.See what the problem is? "/" is included in /fat1/home/me/Maildir and "/fat1" as well.I need to write a simple algorithm for getting the true result but dunno how!
Reply With Quote
  #8 (permalink)  
Old 06-08-04, 18:23
thegrave thegrave is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
Quote:
Originally Posted by fla5do
Blödsinn !!!
The output from "df" command is the same for every User and every aktive Direktory. Surely : only mounted directorys and filesystems are shown in "df" command
You didn't understand me dude, I don't argue about the df cmd but about the absolute path for the directory.The problem was there but I solved it as you see.Now it's just the last thing - to check where the directory is mounted and compare with the corresponding record.
Reply With Quote
  #9 (permalink)  
Old 06-09-04, 05:43
iaguigon iaguigon is offline
Registered User
 
Join Date: May 2004
Location: Barcelona, Spain
Posts: 54
Assumig DIR holds input directory and MOUNTED holds a mount point

${DIR#$MOUNTED} should return:

a.- a relative path from MOUNTED to DIR, if it exists (DIR below MOUNTED)
b.- DIR

Of course, / will always return a relative path. The script should go through all mounted drives and take one of them, or else take /.

I'm not sure if this will work inside awk, but it works fine in the shell script.
Reply With Quote
  #10 (permalink)  
Old 06-09-04, 12:12
thegrave thegrave is offline
Registered User
 
Join Date: Jun 2004
Posts: 6
Man, I'm just way too stupid.The only thing I have to do is go to the corresponding dir and make "df ." there.Try it and you'll see it returns only the current partition
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On