| | |
writing a script
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2004
Posts: 7
Reputation:
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
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
Shell Scripting Syntax (Toggle Plain Text)
#!/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: 16
Reputation:
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)

Shell Scripting Syntax (Toggle Plain Text)
#!/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)
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: 16
Reputation:
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:
Shell Scripting Syntax (Toggle Plain Text)
if test -d $a; then
Shell Scripting Syntax (Toggle Plain Text)
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.
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
Shell Scripting Syntax (Toggle Plain Text)
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: 16
Reputation:
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.
•
•
•
•
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
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
![]() |
Similar Threads
- writing a script for nagios checks... (Shell Scripting)
- 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: Bash shell screipt help
Views: 15185 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for Shell Scripting






