User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 391,919 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,712 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser:
Views: 11227 | Replies: 18
Reply
Join Date: May 2004
Posts: 7
Reputation: hotlantas69 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hotlantas69 hotlantas69 is offline Offline
Newbie Poster

News writing a script

  #1  
May 14th, 2004
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2004
Posts: 1,514
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Rep Power: 10
Solved Threads: 48
Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: writing a script

  #2  
May 14th, 2004
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
Reply With Quote  
Join Date: May 2004
Posts: 7
Reputation: hotlantas69 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hotlantas69 hotlantas69 is offline Offline
Newbie Poster

News Re: writing a script

  #3  
May 14th, 2004
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
Reply With Quote  
Join Date: Apr 2004
Location: Virginia Beach
Posts: 113
Reputation: liliafan is on a distinguished road 
Rep Power: 5
Solved Threads: 2
liliafan's Avatar
liliafan liliafan is offline Offline
Junior Poster

Re: writing a script

  #4  
May 15th, 2004
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.

#!/bin/bash

directory=0
file=0
total=0

for a in `ls`
do
   if test -d $a; then
      directory=$(($directory+1))
   else
      file=$(($file+1))
   fi

   total=$(($total+1))
   echo $a

done

echo Total directories: $directory
echo Total files: $file
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
Application development, webhosting, and much more: www.webcentric-hosting.com
Reply With Quote  
Join Date: May 2004
Posts: 15
Reputation: Ludootje is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Ludootje Ludootje is offline Offline
Newbie Poster

Re: writing a script

  #5  
May 21st, 2004
IMHO you can keep it much simpler than liliafan's script, by using 'the right tool for the right job' as they say

#!/bin/bash
echo Total: $(ls | wc -l)
echo Total files: $(ls -F | grep -v /$ | wc -l)
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)
Reply With Quote  
Join Date: Apr 2004
Location: Virginia Beach
Posts: 113
Reputation: liliafan is on a distinguished road 
Rep Power: 5
Solved Threads: 2
liliafan's Avatar
liliafan liliafan is offline Offline
Junior Poster

Re: writing a script

  #6  
May 21st, 2004
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)
Application development, webhosting, and much more: www.webcentric-hosting.com
Reply With Quote  
Join Date: May 2004
Posts: 15
Reputation: Ludootje is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Ludootje Ludootje is offline Offline
Newbie Poster

Re: writing a script

  #7  
May 22nd, 2004
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:
if test -d $a; then
I thought you always needed
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
We are proud to say that no tests have been made on animals to provide you this post.
Also, we can assure you that if it would be necessary to do tests, we wouldn't use penguins for it.
Disclaimer: the author of this post is NOT responsible for any moral and/or physical damage this post could cause to you.
Reply With Quote  
Join Date: Apr 2004
Location: Virginia Beach
Posts: 113
Reputation: liliafan is on a distinguished road 
Rep Power: 5
Solved Threads: 2
liliafan's Avatar
liliafan liliafan is offline Offline
Junior Poster

Re: writing a script

  #8  
May 22nd, 2004
Ludootje - It can be done in perl very easily, and since primarily I am a perl programmer that would be my tool of choice.
opendir(TMP, "/adirectory");
while ($f=readdir(TMP)) {
   if (-d $f) {
      $directory++;
   } else {
      $file++;
   }
   $total++;
}
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
Application development, webhosting, and much more: www.webcentric-hosting.com
Reply With Quote  
Join Date: May 2004
Posts: 15
Reputation: Ludootje is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Ludootje Ludootje is offline Offline
Newbie Poster

Re: writing a script

  #9  
May 22nd, 2004
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
We are proud to say that no tests have been made on animals to provide you this post.
Also, we can assure you that if it would be necessary to do tests, we wouldn't use penguins for it.
Disclaimer: the author of this post is NOT responsible for any moral and/or physical damage this post could cause to you.
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 101
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: writing a script

  #10  
May 22nd, 2004
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.
Alex Cavnar, aka alc6379
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Shell Scripting Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 8:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC