943,635 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
May 14th, 2004
0

writing a script

Expand Post »
I need to write the code for a script to count and display # of files and directories and then dispaly the # of each as well as the script name as output. I also have to sow a command to execut the script which I know already
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hotlantas69 is offline Offline
7 posts
since May 2004
May 14th, 2004
0

Re: writing a script

Hi,

Did you have any sample code to start with? What shell are you writing for? I sense that this is a homework problem... and while we like to help, we also encourage growth and self-learning.

Christian
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
May 14th, 2004
0

Re: writing a script

Quote originally posted by kc0arf ...
Hi,

Did you have any sample code to start with? What shell are you writing for? I sense that this is a homework problem... and while we like to help, we also encourage growth and self-learning.

Christian
Hi,
Wasn't sure what they meant by code. I have so far
ls | tee frizzle(script name) wc -l -d ./
Wasn't sure if I write that into a vi or do it drom the command prompt
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hotlantas69 is offline Offline
7 posts
since May 2004
May 15th, 2004
0

Re: writing a script

The following script shows show I would do it, this is with a script instead of the commandline, of course in reality I would use perl but this is a basic idea on how to use bash to do it.

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2.  
  3. directory=0
  4. file=0
  5. total=0
  6.  
  7. for a in `ls`
  8. do
  9. if test -d $a; then
  10. directory=$(($directory+1))
  11. else
  12. file=$(($file+1))
  13. fi
  14.  
  15. total=$(($total+1))
  16. echo $a
  17.  
  18. done
  19.  
  20. echo Total directories: $directory
  21. echo Total files: $file
  22. echo Total: $total

Of course if this is in fact a homework assignment the included code wouldn't get you a good grade it is intended just to show basically how it is done.

It can be pasted into a file and then run with: sh scriptname.sh

HTH

Ben
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004
May 21st, 2004
0

Re: writing a script

IMHO you can keep it much simpler than liliafan's script, by using 'the right tool for the right job' as they say

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. echo Total: $(ls | wc -l)
  3. echo Total files: $(ls -F | grep -v /$ | wc -l)
  4. echo Total dirs: $(ls -F | grep /$ | wc -l)

(the '-F' option for ls adds symbols after certain types of files: a * after executables, a @ after symlinks, and a / after dirs)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ludootje is offline Offline
16 posts
since May 2004
May 21st, 2004
0

Re: writing a script

You are right that would be a simpler method of doing it, but since it sounded a lot like a homework assignment I was hoping to just give an indication of how it could be done, whilst indicating that there was better ways to do it, rather than giving the answer in the simplest possible way :o)
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004
May 22nd, 2004
0

Re: writing a script

Well by now I suppose (s)he had to hand in the assignment already (if it was homework in the first place), so now the only people benefitting (speeling?) from it would be other readers (or so I hope).

Your script was very useful to me btw, didn't know you could do this in bash:
Shell Scripting Syntax (Toggle Plain Text)
  1. if test -d $a; then
I thought you always needed
Shell Scripting Syntax (Toggle Plain Text)
  1. if [ -d $a ]; then

The math (i.e. the directory=$(($directory+1)) thing) is new to me too, I thought you always had to pipe those things thru 'bc'.

You got me curious with your remark about Perl though... how would this be done in Perl? Would it be easier than bash?
I suppose that'd be pretty much impossible, since bash was designed specifically for these things, but I'm not exactly a Perl expert

edit: added some code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ludootje is offline Offline
16 posts
since May 2004
May 22nd, 2004
0

Re: writing a script

Ludootje - It can be done in perl very easily, and since primarily I am a perl programmer that would be my tool of choice.
Shell Scripting Syntax (Toggle Plain Text)
  1. opendir(TMP, "/adirectory");
  2. while ($f=readdir(TMP)) {
  3. if (-d $f) {
  4. $directory++;
  5. } else {
  6. $file++;
  7. }
  8. $total++;
  9. }
  10. closedir(TMP);
Obviously just counting files this is a lot of work but in my experience if you are counting files and directories in real world situations you are going to do something with this output, which is where perl comes in handy.

In regards to the bash statements, I couldn't remember the syntax for "test"ing so I used the man pages and on the system I was on at the time (openBSD 3.2) that is the method described. The arithmatic functions in bash suck bad since if the syntax isn't 100% correct it will fail and often not even say that it has failed, I only used it because I was trying to use internal shell as much as possible and avoid external commands where I could. In terms of speed and real world usage for just counting your method is faster and more efficient.

Ben
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004
May 22nd, 2004
0

Re: writing a script

Thanks *a lot* for posting that Perl example, Ben. I've been wondering for a while how hard system programming would be, and in Perl it seems fairly easy - I'm very much surprised that's all that's needed.
Easy to understand, too. I guess I should give Perl/Python another try...
Now if only it didn't take so long before being able to write something actually *useful*, I wouldn't give up so easily
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ludootje is offline Offline
16 posts
since May 2004
May 22nd, 2004
0

Re: writing a script

Quote originally posted by Ludootje ...
I guess I should give Perl/Python another try...
Now if only it didn't take so long before being able to write something actually *useful*, I wouldn't give up so easily
I don't know about Perl, I'm assuming it'd be the same way, but with Python, it's SUPER easy, fun, in fact, to get up and going, and make useful programs with it. Python's syntax is such that you have to write decently clean looking code, as indentation is actually a way of dividing things up. This is unlike Perl, or even C, where you could just about omit white space to your heart's content, and make it look like a huge mess.

If you're interested, you should pick up the O'Reilly book, Learning Python. It's very short, and it gives you a good foundation that you can use and extend to checking out a book like Python Standard Library, which provides useful examples for programming with the various modules that come in the Python Distribution.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Your thoughts regarding AdA
Next Thread in Shell Scripting Forum Timeline: Bash shell screipt help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC