•
•
•
•
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
![]() |
•
•
Join Date: May 2004
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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
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
•
•
Join Date: Apr 2004
Location: Virginia Beach
Posts: 113
Reputation:
Rep Power: 5
Solved Threads: 2
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.
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
#!/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
•
•
Join Date: May 2004
Posts: 15
Reputation:
Rep Power: 5
Solved Threads: 0
IMHO you can keep it much simpler than liliafan's script, by using 'the right tool for the right job' as they say 
(the '-F' option for ls adds symbols after certain types of files: a * after executables, a @ after symlinks, and a / after dirs)

#!/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)
•
•
Join Date: Apr 2004
Location: Virginia Beach
Posts: 113
Reputation:
Rep Power: 5
Solved Threads: 2
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
•
•
Join Date: May 2004
Posts: 15
Reputation:
Rep Power: 5
Solved Threads: 0
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:
I thought you always needed
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
).Your script was very useful to me btw, didn't know you could do this in bash:
if test -d $a; then
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.
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.
•
•
Join Date: Apr 2004
Location: Virginia Beach
Posts: 113
Reputation:
Rep Power: 5
Solved Threads: 2
Ludootje - It can be done in perl very easily, and since primarily I am a perl programmer that would be my tool of choice.
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
opendir(TMP, "/adirectory");
while ($f=readdir(TMP)) {
if (-d $f) {
$directory++;
} else {
$file++;
}
$total++;
}
closedir(TMP);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
•
•
Join Date: May 2004
Posts: 15
Reputation:
Rep Power: 5
Solved Threads: 0
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
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.
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.
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation:
Rep Power: 11
Solved Threads: 101
•
•
•
•
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
Similar Threads
- Problems writing script which checks the users group and date (VB.NET)
- Shell script for identifying FS TYPE and mounting via LOOP device (Shell Scripting)
- ASP slow-down server script (ASP)
Other Threads in the Shell Scripting Forum
- Previous Thread: Your thoughts regarding AdA
- Next Thread: how to recursive search of subdiretories in BASH script



Linear Mode