#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Exclamation Unix script trouble and question


    Hi I am new to unix and need some help, the main reason I am here is because i need basic unix knowledge
    The question says :

    "If the command procedure contains the command echo $ {d-. } what will it bring?"

    Also I need to Write a script to check in the directory specified by the first argument, if there is enough free space specified by the second argument. Provide a comprehensive examination of the arguments."

    This is my script so far, can anyone help me finish this script (with the parsing code) I really need it as soon as possible, and the more I work the worst it seems to get at this point

    Code:
    if [ -d "$1" ]; then
      FREE=$(df -k "$1" | <some parsing to get free space field>)
      [ $FREE -le $2 ] && echo Not enough space
    else
      echo Directory does not exist
    here is another code i tried for the same project please tell me what you think and if you see something wrong please help

    Code:
    for dspace in `df -k /filesystem |tail -1 |awk '{print $1}'`
    do
    if [ $dspace -le "80" ]
    then 
    echo $dspace % available 
    else 
    df -k /filesystem > /tmp/fsspace 
    cat /usr/local/bin/fswarn.txt /tmp/fsspace > /usr/local/bin/fswarning.txt 
    /usr/bin/mailx -s "FS exceeded 80% and is at $dspace % FULL needs attention" mymail@mydomain.tld < /usr/local/bin/export
    warning.txt
    fi
    Thanks to everyone
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    857
    Rep Power
    388

    Cool


    Originally Posted by krolike
    Hi I am new to unix and need some help, the main reason I am here is because i need basic unix knowledge
    ... Etc ...
    Thanks to everyone
    1) $ {d-. } brings nothing else other than the value of variable "d"

    2) Assuming parameter '1' is a mount point and parameter '2' is a percent "free", then try this:
    Code:
    if [ -d "$1" ]; then
      FREE=$(df -k $1 | |awk 'NR==2{print 100-substr($5,1,length($5)-1);}')
      [ $FREE -le $2 ] && echo "Not enough space, ${FREE}% left"
    else
      echo "Directory $1 does not exist"
    fi
    3) Learn Unix! ...
    Last edited by LKBrwn_DBA; February 4th, 2011 at 01:57 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0
    thanks alot for the answer.
    I tried your code and here are some stuff I noticed:

    NR==2 isn't perfect. Some df's can output on three lines.

    On Linux, using -P (the so-called "POSIX" option) would show the output on two lines.

    i.e.

    Code:
    # ssh root@oraserv1
    / # cd /oracle
    $ df .
    Filesystem           1K-blocks      Used Available Use% Mounted on
    /dev/mapper/ora001vg-ora001lv
                           8252856   4752396   3081236  61% /oracle
    
    /oracle # df -P .
    Filesystem         1024-blocks      Used Available Capacity Mounted on
    /dev/mapper/ora001vg-ora001lv   8252856   4752396   3081236      61% /oracle
    tail is good. sed can also get the last line:

    Code:
    /oracle # df . | sed '$!d'
                           8252856   4752396   3081236  61% /oracle
    And with awk, there's no need to remove the %. It'll do that for you when it know's it's a number.

    Code:
    $ echo 22% | awk '{print 100-$1}'  
    78
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    857
    Rep Power
    388

    Cool


    Originally Posted by krolike
    thanks alot for the answer.
    I tried your code and here are some stuff I noticed:

    NR==2 isn't perfect. Some df's can output on three lines.

    ... etc ...
    From the command line the 'df' command may out put two lines, BUT if you redirect to pipe or text file, it will output ONE line.

IMN logo majestic logo threadwatch logo seochat tools logo