UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 19th, 2012, 03:36 PM
Kry56 Kry56 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 3 Kry56 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old April 19th, 2012, 03:55 PM
aitken325i's Avatar
aitken325i aitken325i is offline
Providing fuel for space ships
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Mar 2004
Location: nr Edinburgh, Scotland
Posts: 14,360 aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)  Folding Points: 10110 Folding Title: Novice Folder
Time spent in forums: 6 Months 1 Day 12 h 43 m 17 sec
Reputation Power: 3777
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

Reply With Quote
  #3  
Old April 19th, 2012, 04:06 PM
Kry56 Kry56 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 3 Kry56 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old April 19th, 2012, 04:31 PM
Kry56 Kry56 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 3 Kry56 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.?

Reply With Quote
  #5  
Old April 19th, 2012, 09:27 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
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

Reply With Quote
  #6  
Old April 20th, 2012, 08:51 AM
LKBrwn_DBA's Avatar
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 752 LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 3 h 5 m 12 sec
Reputation Power: 348
Cool

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...
__________________

Reply With Quote
  #7  
Old April 20th, 2012, 09:53 AM
Lucretiuss Lucretiuss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 4 Lucretiuss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old April 20th, 2012, 11:20 AM
LKBrwn_DBA's Avatar
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 752 LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 3 h 5 m 12 sec
Reputation Power: 348
Cool

Here (for starters) is count of directories:
Code:
ls -l|grep ^d|wc -l


Reply With Quote
  #9  
Old April 20th, 2012, 11:27 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
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)"

Reply With Quote
  #10  
Old April 20th, 2012, 01:16 PM
Lucretiuss Lucretiuss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 4 Lucretiuss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #11  
Old April 20th, 2012, 03:05 PM
LKBrwn_DBA's Avatar
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 752 LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 3 h 5 m 12 sec
Reputation Power: 348
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"


Reply With Quote
  #12  
Old April 20th, 2012, 04:44 PM
aitken325i's Avatar
aitken325i aitken325i is offline
Providing fuel for space ships
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Mar 2004
Location: nr Edinburgh, Scotland
Posts: 14,360 aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)aitken325i User rank is General 42nd Grade (Above 100000 Reputation Level)  Folding Points: 10110 Folding Title: Novice Folder
Time spent in forums: 6 Months 1 Day 12 h 43 m 17 sec
Reputation Power: 3777
Looks like you guys (Lucretiuss and Kry56) are getting somewhere, excellent.

Out of curiosity, what class are you guys taking ?

Reply With Quote
  #13  
Old April 20th, 2012, 05:45 PM
Lucretiuss Lucretiuss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 4 Lucretiuss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #14  
Old April 20th, 2012, 05:54 PM
Lucretiuss Lucretiuss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 4 Lucretiuss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Shell Scripting Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap