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

    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Shell Scripting Help


    This is an assignment for a class I'm taking

    You need to write a shell script that calculates the following information for the contents of a given directory:

    1. The total number of directories that are in the given directory.
    2. The total number of files in the given directory.
    3. The number of items (files / directories) in the current directory that are readable.
    4. The number of items (files / directories) in the current directory that are writable.
    5. The number of items (files / directories) in the current directory that are executable.

    It also has to check for the correct number of arguments (one argument) and check if the command line argument is actually a directory.
  2. #2
  3. Providing fuel for space ships
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2004
    Location
    nr Edinburgh, Scotland
    Posts
    14,382
    Rep Power
    3848
    Hi Kry56 and welcome to Dev Shed.

    OK - now, can you show us what you have so far ? Don't mind helping you, but not doing it for you!
    The No Ma'am commandments:

    1.) It is O.K. to call hooters 'knockers' and sometimes snack trays
    2.) It is wrong to be French
    3.) It is O.K. to put all bad people in a giant meat grinder
    4.) Lawyers, see rule 3
    5.) It is O.K. to drive a gas guzzler if it helps you get babes
    6.) Everyone should car pool but me
    7.) Bring back the word 'stewardesses'
    8.) Synchronized swimming is not a sport
    9.) Mud wrestling is a sport
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by aitken325i
    Hi Kry56 and welcome to Dev Shed.

    OK - now, can you show us what you have so far ? Don't mind helping you, but not doing it for you!
    That's the thing, I have no idea where to start
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0
    So far I've found that using the find command I can output how many files are in a directory, but how can I alter the command to echo "Number of files: #" as well as the number of readables etc.?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,447
    Rep Power
    1751
    If you are going to use the find command for all of these you may want to check out the -perm operand.
    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. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    850
    Rep Power
    387

    Cool


    Originally Posted by Kry56
    So far I've found that using the find command I can output how many files are in a directory, but how can I alter the command to echo "Number of files: #" as well as the number of readables etc.?
    Also check out the 'ls', 'grep' and 'cut' commands, they may be of some help...
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0
    I'm on the same assignment and have a question about counts.

    He gives us this example:
    count=`wc -w <$1`
    # the value of count is assigned the number of words in file $1

    But we need to count directories and files in a directory (non recursively), so this example doesn't really help other than tell me I need to use `'s. I've tried count=`ls -d` (as a logical jump from `ls -1` returning all files, but this returns nothing (I already set the cd to $1 so I wouldn't need to do <$1, correct?). Also, how do I output the value of count? $count returns all the names of the files, but #count does nothing. ("$count" returns all the files one line each and "#count" still returns nothing).

    Thanks.

    EDIT: Ok I found a lot of mistakes on my own thanks to this: (google ls commands unix...being blocked from posting links..) if anybody else needs help.
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    850
    Rep Power
    387

    Cool


    Here (for starters) is count of directories:
    Code:
    ls -l|grep ^d|wc -l
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,447
    Rep Power
    1751
    Don't forget that if you are actually on the *nix machine you will have man pages available.

    Let's walk through some basic examples ...

    Code:
    ls
    That will list the contents of the current directory.
    Code:
    ls -1
    That will do the same thing but will list them one per line.
    Code:
    ls -1p
    That will list directoru contents, one per line, but will append a / character to the name of any directory.
    Can you see where we are going with this? If we count the number of words (the wc -w command) we might get the right numbers - but consider files and directories that have spaces in them: suddenly we will get a wrong answer. Now, with the details being one per line we can use the wc -l command to count the number of lines - if we wish.
    So, we could use the following command to list the directory contents, extract only the lines ending in / and count them:
    Code:
    ls -1p | grep "\$" | wc -l
    A better way would be to combine the filtering and counting, which we can do with grep:
    Code:
    ls -1p | grep -c "\$"
    If we want to count files we would need to look for lines that do not end in a \ - effectively inverting the filter:
    Code:
    ls -1p | grep -vc "\$"
    So, we now have a number being returned. Just assign that to a variable:
    Code:
    dir_count=$(ls -1p | grep -c "\$")
    file_count=$(ls -1p | grep -vc "\$")
    echo "We found $dir_count directories and $file_count files within the current directory $(pwd)"
    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
    Apr 2012
    Posts
    4
    Rep Power
    0
    Ok, I managed to make it output the number of files and directories, and now I have to check if the files are readable, writable, or executable. I'm thinking about doing something like

    Code:
    excount=0
    for filenames in -x
    do    
         excount=`expr $excount + 1`
    done
         (do this twice more, but for 2 other counts for reading and writing)
    echo "The number of executable files: " $excount
         (do this twice more, again for reading and writing)
    2nd attempt:

    Code:
    excount=0
    if [ -x filename ]
    then
         excout=`expr $excount + 1`
    elif [ for read and write...]
    fi
    echo "The number of executable files: " $excount
        (twice more for reading and writing)
    2nd attempt returns 0 for all numbers, which is wrong.

    EDIT: Trimmed it down a little, still doesn't count correctly.
  20. #11
  21. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    850
    Rep Power
    387

    Cool


    This is one way to do it:
    Code:
    r0=0; w0=0; x0=0;
    for f in `ls -l|grep -v ^d|grep -v ^total`
    do
      r=`echo $f|cut -c2`
      w=`echo $f|cut -c3`
      x=`echo $f|cut -c4`
      [ "$r" = 'r' ]&& ((r0+=1))
      [ "$w" = 'w' ]&& ((w0+=1))
      [ "$x" = 'x' ]&& ((x0+=1))
    done
    echo "Readable: $r0, Writable: $w0, Executable: $x0"
  22. #12
  23. Providing fuel for space ships
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2004
    Location
    nr Edinburgh, Scotland
    Posts
    14,382
    Rep Power
    3848
    Looks like you guys (Lucretiuss and Kry56) are getting somewhere, excellent.

    Out of curiosity, what class are you guys taking ?
    The No Ma'am commandments:

    1.) It is O.K. to call hooters 'knockers' and sometimes snack trays
    2.) It is wrong to be French
    3.) It is O.K. to put all bad people in a giant meat grinder
    4.) Lawyers, see rule 3
    5.) It is O.K. to drive a gas guzzler if it helps you get babes
    6.) Everyone should car pool but me
    7.) Bring back the word 'stewardesses'
    8.) Synchronized swimming is not a sport
    9.) Mud wrestling is a sport
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0
    Intro to Unix. A lot of non CS kids take it though as it's a 1 credit hour class at 3000 level and above. I'm surprised how helpful you guys are, typically when I ask for c++ help I get "Well, your problem is in 500linesofcode.cpp".

    Thanks for the help!
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by LKBrwn_DBA
    This is one way to do it:
    Code:
    r0=0; w0=0; x0=0;
    for f in `ls -l|grep -v ^d|grep -v ^total`
    do
      r=`echo $f|cut -c2`
      w=`echo $f|cut -c3`
      x=`echo $f|cut -c4`
      [ "$r" = 'r' ]&& ((r0+=1))
      [ "$w" = 'w' ]&& ((w0+=1))
      [ "$x" = 'x' ]&& ((x0+=1))
    done
    echo "Readable: $r0, Writable: $w0, Executable: $x0"
    Took me a few minutes but to figure this out:

    Code:
    r=`echo $f | cut -c2`
    Quite clever...didn't think of that.

    What do the ampersands do in:

    Code:
    [ "$r" = 'r' ]&& ((r0+=1))
    Is that just shorthand for if [ "$r" = 'r'], then r0+=1 ?

    EDIT: I figured out it was just shorthand, finished the assignment. Thanks again for the help guys.

IMN logo majestic logo threadwatch logo seochat tools logo