The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Operating Systems
> UNIX Help
|
Shell Scripting Help
Discuss Shell Scripting Help in the UNIX Help forum on Dev Shed. Shell Scripting Help UNIX Help forum discussing the Unix Operating System and all variants including Irix, Solarix, and AIX. Unix was designed as a true multi-user operating system.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 19th, 2012, 03:36 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 3
Time spent in forums: 1 h 6 m 42 sec
Reputation 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.
|

April 19th, 2012, 03:55 PM
|
 |
Providing fuel for space ships
|
|
Join Date: Mar 2004
Location: nr Edinburgh, Scotland
|
|
|
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
|

April 19th, 2012, 04:06 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 3
Time spent in forums: 1 h 6 m 42 sec
Reputation Power: 0
|
|
Quote: | 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
|

April 19th, 2012, 04:31 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 3
Time spent in forums: 1 h 6 m 42 sec
Reputation 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.?
|

April 19th, 2012, 09:27 PM
|
|
|
|
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
|

April 20th, 2012, 08:51 AM
|
 |
Contributing User
|
|
|
|
Quote: | 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...

__________________
|

April 20th, 2012, 09:53 AM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 37 m 30 sec
Reputation 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.
|

April 20th, 2012, 11:20 AM
|
 |
Contributing User
|
|
|
|
Here (for starters) is count of directories:
Code:
ls -l|grep ^d|wc -l

|

April 20th, 2012, 11:27 AM
|
|
|
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 ...
That will list the contents of the current directory.
That will do the same thing but will list them one per line.
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)"
|

April 20th, 2012, 01:16 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 37 m 30 sec
Reputation 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.
|

April 20th, 2012, 03:05 PM
|
 |
Contributing User
|
|
|
|
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"

|

April 20th, 2012, 04:44 PM
|
 |
Providing fuel for space ships
|
|
Join Date: Mar 2004
Location: nr Edinburgh, Scotland
|
|
|
Looks like you guys (Lucretiuss and Kry56) are getting somewhere, excellent.
Out of curiosity, what class are you guys taking ?
|

April 20th, 2012, 05:45 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 37 m 30 sec
Reputation 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!
|

April 20th, 2012, 05:54 PM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 4
Time spent in forums: 1 h 37 m 30 sec
Reputation Power: 0
|
|
Quote: | 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|