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
hotlantas69 0 Newbie Poster
kc0arf 68 Posting Virtuoso Team Colleague
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
hotlantas69 0 Newbie Poster
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
liliafan 53 Junior Poster
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
Ludootje 0 Newbie Poster
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)
liliafan 53 Junior Poster
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)
Ludootje 0 Newbie Poster
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
liliafan 53 Junior Poster
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
Ludootje 0 Newbie Poster
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 :)
alc6379 120 Cookie... That's it Team Colleague
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.
Ludootje 0 Newbie Poster
I'm very interested... I know I need to pick up these languages some day anyway. I already started learning Python a while back, but I quit at some point. I just lack the motivation to read those tutorials until the end :/
(it takes pretty long for me to write a book or tutorial or whatever in English (it's not my native language as I'm sure you'll have guessed by now))
In the beginning, all you can are stupid programs, not useful at all. With bash, at least I get to do (semi-)useful things right from the start:)
I'll probably buy Learning Python someday soon though (on the other hand, I've been telling myself that for a while heh).
liliafan 53 Junior Poster
Python has been looking interesting to me for a few years, i even went as far as buying an oreilly book on it, but so far it hasn't progressed up my list of languages to learn which as it stands at this point is:
ksh
ruby
Ocaml
python
java
Java is another language I have been thinking about learning but I really don't want to, it goes against my grain C/C++, perl, however, I have a horrible feeling someone is going to offer me a project in the near future using java and I will have to turn it down, being a freelance coder sucks sometimes the clients gets to have to much input on the language to be used.
This is besides the point, acl6379, how do you find python compares to other scripted languages, perl, ruby, etc, I have only ever heard the opinions of perl and python fanatics who always say their language is best I have yet to hear an objective opinion.
Ben
Ludootje 0 Newbie Poster
This is besides the point, acl6379, how do you find python compares to other scripted languages, perl, ruby, etc, I have only ever heard the opinions of perl and python fanatics who always say their language is best I have yet to hear an objective opinion.
Ben
I doubt it'd be easy to find an unbiased view about Python<->something, as most people are generally fond of one the languages they're comparing. However, here's a pretty good read on Python vs. Perl by ESR (Eric S. Raymond, a pretty known F/OSS evangelist): http://www.linuxjournal.com/article.php?sid=3882
It's pretty long, but definitely worth a read IMHO.
alc6379 120 Cookie... That's it Team Colleague
definitely, I couldn't give an unbiased comparison of the two.
Firstly, I LOVE Python. Secondly, I haven't really done enough with Perl or Ruby to make an educated opinion about it. I do know that you can do everything in Perl that you can do in Python-- it's just done differently.
Perl looks like somebody merged C with bash scripting, at least to me it looks like that. Python doesn't feel syntactically like any other language I've fooled with. Its indentation is part of the syntax-- nested statements require indentation. This makes code way easier to read, and it reinforces good coding habits. Other than that, I wouldn't be much help to you there...
liliafan 53 Junior Poster
Ludootje - Excellent article, it certainly has got me thinking.
acl6379 - Your statement combined with reading the article from Ludootje, has made up my mind. I am gonna play with python, this is no small statement perl is my first language I have been using it for 6-7 years and I always hated the look of python, but perhaps it has a few positive features, especially considering the timing, I just wrote a project in perl with >5000 lines of code and it broke after 3 months because the interpreter couldn't handle the load, perhaps python will be more successful.
Ben
Ludootje 0 Newbie Poster
I just wrote a project in perl with >5000 lines of code and it broke after 3 months because the interpreter couldn't handle the load, perhaps python will be more successful.
Don't know if Python would be any better for such a project - but wouldn't a compiled language (C maybe) be better for something so big?
Don't know a lot about the sizes of projects as I'm not a professional programmer (wouldn't even consider myself a programmer), but I always that for bigger projects a compiled language was recommended instead of a scripted... Is this wrong?
liliafan 53 Junior Poster
Ludootje - Yes generally compiled languages are better for the bigger projects, the project I mentioned does in fact prove that point (I am in the process of recoding it in C++), however, sometimes a project starts being a lot smaller than it ends up, with perl you end up with spagetti code that is almost impossible to read, very difficult to reference and often buggy and breakable, however, python sounds like it may fit this niche quite well, something that starts as a small project but has potential to grow into something a lot larger, it looks to me like python will grow quite well with any project.
So long story short, yes bigger projects often are more suited to compiled languages (although not always), but small projects that at first seem perfect for a scripted language can often grow, a good example of that was a bash / perl set of scripts I wrote to simply monitor a machine for load getting to high and sent out some mail if it got severe, started as a couple of hundred lines in about 3 scripts, ended up being a full distributed network monitoring program which is still being added to everyday at the moment it has something like 6000-8000 lines of code across about 50 files, across 6 midsize sun enterprise servers, in hindsight it should have been written in C/C++ for scalability but at the time, I had an hour on my hands and wrote a couple of scripts to make my life a little easier.
Ben
Ludootje 0 Newbie Poster
I see. Thanks for your explanations - I didn't think about the possibility of a small hack becoming a big project (having never been in that situation before), but I guess it's pretty logical :)
(Man it must suck if you have to rewrite 5000 lines because the interpreter can't handle it...)
Thanks!
liliafan 53 Junior Poster
Ludootje - I am not having to write that much code over :o) The wonderful thing about knowing what the end product will be is that you can write more efficient code, you can also eradicate uneeded code, and in addition some language handle things better than others, where as all the caching code in the project written in perl came to about 900 lines dealing with the different caches and different methods of access by using C++ and SDL that is reduced to about 100 lines.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.