[TAG] Two cent tip -- proposal

Dr. Parthasarathy S drpartha at gmail.com
Thu Feb 4 08:17:22 MSK 2010


YES I agree. Your script is better than mine. If you authorise me, I will
resubmit my stuff using your script (after I add a comment line
acknowledging your contribution). Or I can just use the script for my
internal usage and not submit for publication.

I thank you for your frank opinion and CONSTRUCTIVE criticism. That is how
we all learn.


partha

******************




On 4 February 2010 08:34, Ben Okopnik <ben at linuxgazette.net> wrote:

> Hi, Partha -
>
> On Tue, Feb 02, 2010 at 09:57:02AM +0530, Dr. Parthasarathy S wrote:
> >
> > I hope you will find the enclosed submission worthwhile for LG. Please
> let me
> > know as soon as it gets published, or if it is not worth publishing in
> LG.
> > Thank you.
>
> Pretty much anything sent to TAG, other than flames and spam, is fodder
> for discussion and publication; that's what we're all about. Now, as to
> the script itself -
>
> ```
> ###########start-howmuch-script
> # Tells you how many files, subdirectories and content bytes in a
> # directory
> # Usage :: how much <directory-path-and-name>
>
> # check if there is no command line argument
> if [ $# -eq 0 ]
> then
> echo "You forgot the directory to be accounted for !"
> echo "Usage :: howmuch <directoryname with path>"
> exit
> fi
>
> echo "***start-howmuch***"
> pwd > ~/howmuch.rep
> pwd
> echo -n "Disk usage of directory ::" > ~/howmuch.rep
> echo $1 >> ~/howmuch.rep
> echo -n "made on ::" >> ~/howmuch.rep
> du -s $1 > ~/howmuch1
> tree $1 > ~/howmuch2
> date >> ~/howmuch.rep
> tail ~/howmuch1 >> ~/howmuch.rep
> tail --lines=1 ~/howmuch2 >> ~/howmuch.rep
> cat ~/howmuch.rep
> # cleanup
> rm ~/howmuch1
> rm ~/howmuch2
> #Optional -- you can delete howmuch.rep if you want
> #rm ~/howmuch.rep
>
> echo "***end-howmuch***"
> # ****
>
> ########end-howmuch-script
> '''
>
> One of the standard practices in shell scripting is to stay away from
> temporary files unless they're necessary (e.g., if you need to use a
> program that _only_ takes files as input.) What would happen, for
> example, if you already had a file called 'howmuch.rep' in that
> directory? For example, if you had run this script for, say, the 'foo'
> directory yesterday, forgot about it, and wanted to get the results for
> the 'bar' directory today? The first file would be gone - and you
> wouldn't know anything about it until you wanted the data.
>
> This is why the standard practice is to construct every program as a
> filter - that is, arrange it so that it takes data in, transforms it,
> and outputs it to STDOUT. What this mostly means with regard to coding
> is using pipes instead of temporary files. For example (I'm going to
> make an explicit effort to replicate your script's output here):
>
> ```
> #!/bin/bash
> # Created by Ben Okopnik on Wed Feb  3 21:57:05 EST 2010
>
> [ -d "$1" ] || { printf "Usage: ${0##*/} <directory>\n"; exit; }
>
> pwd
> echo -e "Disk usage of directory ::$1\nmade on ::`date`"
> du -sk "$1"
> # You could use 'tree "$1"|sed -n "$p"' - or stick with the standard
> toolkit
> ls -lR "$1"|awk '/^\//{d++};/^-/{f++}END{print d-1" directories, "f"
> files"}'
> '''
>
> This will do essentially the same thing as your script, but without any
> temp files. The output can be saved to a specified file simply by
> redirecting it, or it can be further filtered/modified by piping it to
> another program.
>
>
> --
> * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
>
>


-- 
---------------------------------------------------------------------------------------------
Dr. S. Parthasarathy                    |   mailto:drpartha at gmail.com
Algologic Research & Solutions    |
78 Sancharpuri Colony                 |
Bowenpally  P.O                          |   Phone: + 91 - 40 - 2775 1650
Secunderabad 500 011 - INDIA     |
WWW-URL: http://algolog.tripod.com/nupartha.htm
---------------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxgazette.net/mailman/private/tag/attachments/20100204/305e00e8/attachment-0001.htm>




More information about the TAG mailing list