Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2004
    Posts
    6

    Unanswered: 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!

  2. #2
    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.

  3. #3
    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?

  4. #4
    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=" " }

  5. #5
    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.

  6. #6
    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
    Last edited by fla5do; 06-09-04 at 03:17.
    Greetings from germany
    Peter F.

  7. #7
    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!

  8. #8
    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.

  9. #9
    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.

  10. #10
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •