Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0

    Exclamation Help with File Monitoring Script


    I am getting errors when I try to run the script I just made, any suggestiongs would be helpful. Please be gentle, Im still a newbie and learning on the go at an entry level position.


    #!/usr/bin/ksh
    # PURPOSE: This script is going to view the top 10 largest home directory Folders, then go in each directory and list top 10 largest files.
    #
    #Author: Emmanuel Iroanya Jr
    #Date: March 8,2012
    #
    ##### DEFINE FILES AND VARIABLES HERE ####
    WORKFILE="/tmp/homedf.work" # Holds filesystem data
    >$WORKFILE # Initialize to empty
    OUTFILE="/tmp/homedf.outfile" # Output display file
    >$OUTFILE # Initialize to empty
    THISHOST=`hostname` # Hostname of this machine
    USER=`ls /home | sort -n -r | head -n 10` # Define User
    counter=0 # Sets Counter to 0
    ######## START OF MAIN #############

    cd /home
    du -sk /home | sort -n -r | head -n 10 > $WORKFILE

    while read $USERS

    do
    du -a $USER | sort -n -r | head -n 10 > $OUTFILE
    counter=counter+1
    done

    if [[ -s "$WORKFILE" && "$OUTFILE"]]
    then
    cat "$WORKFILE" && "$OUTFILE"
    print
    mailx -s "Home Folder Size Files"<email address here> < "$WORKFILE" && "$OUTFILE"

    fi
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    /usr/bin/ksh: syntax error: `newline or ;' unexpected <---- First error I recieved.



    I'm horrible at searching through my own code. ANother pair of eyes would help alot. thanks
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    Not sure if it is th eissue or not, but ...
    You have $USERS in the read command, then reference $USER inside the loop
    Not sure counter=counter+1 is valid. try: counter=$(( counter + 1 ))
    if [[ -s "$WORKFILE" && "$OUTFILE"]] add a space after the last ". Also I suspect that the condition you have is not what you want.
    cat "$WORKFILE" && "$OUTFILE" <-- I suspect that is not doing what you think it is

    For your condition your probably want:
    if [[ -s "$WORKFILE" && -s "$OUTFILE" ]]
    Testing that both files exist and are not empty

    Instead of cat "$WORKFILE" && "$OUTFILE" I suspect what you want is
    cat "$WORKFILE" "$OUTFILE"
    to output the contents of both files, one after the other
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    Thanks alot, let me make these changes and see what I get.

    Appreciate it alot.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    No worries.
    For the sake of completeness your line: cat "$WORKFILE" && "$OUTFILE" is actually valid. Kind of! What it does is run the command to the left of the &&, evaluate the return code, and if 0 (i.e., it worked) it will run the command on the right of the &&. In your case that was just a file name, so unless that happened to be the name of an executable it'd just complain.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    One Last error when I post the edited code I get this error as well



    ksh: syntax error: `-a' unexpected


    I am assuming its coming from the line:



    du -a $USER | sort -n -r | head -n 10 > $OUTFILE

    Edited Code:

    #!/usr/bin/ksh
    # PURPOSE: This script is going to view the top 10 largest home directory Folders, then go in each directory and list top 10 largest files.
    #
    #Author: Emmanuel Iroanya Jr
    #Date: March 8,2012
    #
    ##### DEFINE FILES AND VARIABLES HERE ####

    WORKFILE="/tmp/homedf.work" # Holds filesystem data
    >$WORKFILE # Initialize to empty
    OUTFILE="/tmp/homedf.outfile" # Output display file
    >$OUTFILE # Initialize to empty
    THISHOST=`hostname` # Hostname of this machine
    USER=`du -sk /home/* | sort -n -r | awk '{print $2}' | head -10` # Defines User
    counter=0 # Sets Counter to 0
    ######## START OF MAIN #############

    cd /home
    du -sk /home | sort -n -r | head -n 10 > $WORKFILE

    ls /home | sort -n -r | head -n 10
    while read $USER
    do
    du -a $USER | sort -n -r | head -n 10 > $OUTFILE
    let counter=$counter+1
    echo $counter
    done

    if [[ -s "$WORKFILE" -a -s "$OUTFILE" ]]
    then
    cat "$WORKFILE" "$OUTFILE"
    print
    mailx -s "Home Folder Size Files"(email address) < "$WORKFILE" && "$OUTFILE"
    fi
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    That is coming from your if test ... change the -a back to &&
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    Originally Posted by SimonJM
    That is coming from your if test ... change the -a back to &&

    Thanks alot!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    Happy to help - hopefully all is now working as planned?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    before i left the office i ran the script to see if it worked... it is going through the home directory but it is only listing the last 10 users in the directory and it isnt changing into directories and listing the top 10 files...
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    Strange you should day that, I took another look over your script and realised you had some logic flaws there.

    I'd not use $USER as a variable, that tends to be set by the shell for you and should contain your username.
    Code:
    USERS=`du -sk /home/* | sort -n -r | head -10 | awk '{print $2}'` # Defines User
    I also re-jigged the order of the pipes, so that the awk to extract the home directory only acts on the 10 required entries, not them all (a very minor effeciency tweak!)

    Is the intent to find those users and files, and then mail someone with that data?
    If so, then with and inside your loop you have two issues:
    Code:
    while read $USER
    That won't actually do what you think, I do not believe - I'd have thought it would just sit there and wait for input from stdin, try:
    Code:
    for $USRDIR in $USERS
    Code:
    du -a $USER | sort -n -r | head -n 10 > $OUTFILE
    $USER will contain the user name, thus you will need to prepend /home/ to it. The > $OUTFILE is over-writing the data with each iteration fop the loop, so you need you use the append redirect option, which is >>.
    Code:
    du -a $USRDIR | sort -n -r | head -n 10 >> $OUTFILE
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    just checked this now, i will make changes and let you know the outcome.

    $USER will contain the user name, thus you will need to prepend /home/ to it.
    <---- i am a tad bit confused, isnt the home directory already where the usernames will be picked from in the variable defined?
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    Originally Posted by SimonJM
    Strange you should day that, I took another look over your script and realised you had some logic flaws there.

    I'd not use $USER as a variable, that tends to be set by the shell for you and should contain your username.
    Code:
    USERS=`du -sk /home/* | sort -n -r | head -10 | awk '{print $2}'` # Defines User
    I also re-jigged the order of the pipes, so that the awk to extract the home directory only acts on the 10 required entries, not them all (a very minor effeciency tweak!)

    Is the intent to find those users and files, and then mail someone with that data?
    If so, then with and inside your loop you have two issues:
    Code:
    while read $USER
    That won't actually do what you think, I do not believe - I'd have thought it would just sit there and wait for input from stdin, try:
    Code:
    for $USRDIR in $USERS
    Code:
    du -a $USER | sort -n -r | head -n 10 > $OUTFILE
    $USER will contain the user name, thus you will need to prepend /home/ to it. The > $OUTFILE is over-writing the data with each iteration fop the loop, so you need you use the append redirect option, which is >>.
    Code:
    du -a $USRDIR | sort -n -r | head -n 10 >> $OUTFILE
    After changes

    What I am getting not in an e-mail:

    xmladm
    williamb
    tranadm
    trainadm
    tonyd
    tombe
    toddm
    tiat
    testuser
    testadm
    /usr/bin/ksh: $USRDIR: is not an identifier


    What I am expecting in an e-mail:

    1484373 krishnaa
    160446 joe
    148463 kimh
    112804 michaelr
    89522 craign
    79895 solomond
    78694 brandonp
    63859 charlesc
    63769 miker
    39251 michael

    krishnaa:
    2968746 .
    2956268 ./bin
    262688 ./bin/EVServices
    256352 ./bin/Bureau
    154288 ./bin/CharacteristicCitizens
    137712 ./bin/CharacteristicReg
    134048 ./bin/Initiator
    132480 ./bin/CalculationSTD
    129568 ./bin/CharacteristicCmp
    127024 ./bin/VerificationService

    etc...


    CODE:

    #!/usr/bin/ksh
    #PURPOSE: This script is going to view the top 10 largest home directory Folders, then go in each directory and list top 10 largest files.
    #
    #Author: Emmanuel Iroanya Jr
    #Date: March 8,2012
    #
    ##### DEFINE FILES AND VARIABLES HERE ####

    WORKFILE="/tmp/homedf.work" # Holds filesystem data
    >$WORKFILE # Initialize to empty
    OUTFILE="/tmp/homedf.outfile" # Output display file
    >$OUTFILE # Initialize to empty
    THISHOST=`hostname` # Hostname of this machine
    USER=`du -sk /home/* | sort -n -r | head -10 | awk '{print $2}'` # Defines User
    counter=0 # Sets Counter to 0
    ######## START OF MAIN #############

    cd /home
    du -sk | sort -n -r | head -n 10 > $WORKFILE

    ls /home | sort -n -r | head -n 10
    for $USRDIR in $USERS
    do
    du -a $USRDIR | sort -n -r | head -n 10 >> $OUTFILE
    let counter=$counter+1
    echo $counter
    done

    if [[ -s "$WORKFILE" && -s "$OUTFILE" ]]
    then
    cat "$WORKFILE" "$OUTFILE"
    print
    mailx -s "Home Folder Size Files" exxxxxxl_ixxxxxa@xxxxxx.com < "$WORKFILE" && "$OUTFILE"
    fi
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    You need to change the variable in the assignment too:
    Code:
    USERS=`du -sk /home/* | sort -n -r | head -10 | awk '{print $2}'` # Defines User
    That will then allow the loop to function and, hopefully, you to get the output you want!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    changing user to users now.

    What about the USRDIR variable. do i just set it to
    Code:
     /home/*
    and let the code determine which user directory to look in?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo