From: Chris F.A. Johnson on 6 Feb 2010 22:35 On 2010-02-07, Ed Morton wrote: .... > Note that in general if you find yourself writing a loop in a shell script, it's > the wrong approach. An over-broad generalization that borders on nonsensical. -- Chris F.A. Johnson, author <http://shell.cfajohnson.com/> =================================================================== Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== ===== and is released under the GNU General Public Licence =====
From: Kaz Kylheku on 6 Feb 2010 23:48 On 2010-02-07, Ed Morton <mortonspam(a)gmail.com> wrote: > Note that in general if you find yourself writing a loop in a shell script, it's > the wrong approach. On one recent project, I wrote not only a loop in a shell script, but in fact a loop which reads input from a file, lexically analyzes, parses it and interprets another language; a domain-specific language for constructing a file system image with customized contents.
From: Ed Morton on 7 Feb 2010 10:55 On 2/6/2010 9:35 PM, Chris F.A. Johnson wrote: > On 2010-02-07, Ed Morton wrote: > ... >> Note that in general if you find yourself writing a loop in a shell script, it's >> the wrong approach. > > An over-broad generalization that borders on nonsensical. > No, it's not and the point of saying it so definitively is to get the attention of people who don't understand why I'd say that so they really think about it when they find themselves writing a loop. Writing a shell loop is something you should do after you've thought about whether or not it's really the best approach as it usually isn't but for people new to shell scripting it's often the approach they're most familiar with so it's the direction they take off in rather than investigating which tools are out there that (often in pipelined combinations) already do the job. Ed.
From: Ed Morton on 7 Feb 2010 11:25 On 2/7/2010 9:55 AM, Ed Morton wrote: > On 2/6/2010 9:35 PM, Chris F.A. Johnson wrote: >> On 2010-02-07, Ed Morton wrote: >> ... >>> Note that in general if you find yourself writing a loop in a shell >>> script, it's >>> the wrong approach. >> >> An over-broad generalization that borders on nonsensical. >> > > No, it's not and the point of saying it so definitively is to get the > attention of people who don't understand why I'd say that so they really > think about it when they find themselves writing a loop. Writing a shell > loop is something you should do after you've thought about whether or > not it's really the best approach as it usually isn't but for people new > to shell scripting it's often the approach they're most familiar with so > it's the direction they take off in rather than investigating which > tools are out there that (often in pipelined combinations) already do > the job. > > Ed. FWIW, here's how I'd really write a script to calculate the min/max/ave from a set of values being input by a user when prompted: $ cat tst.sh awk 'BEGIN{ sum=min=max=ave="NAN"; printf "Enter a value: " } { sum += $0 min = ( ($0 < min) || (min == "NAN") ? $0 : min) max = ( ($0 > max) || (max == "NAN") ? $0 : max) printf "Enter a value: " } END { ave = ( NR ? sum / NR : ave ) print "\ntotal =", sum print "minimum value =", min print "average value =", ave print "maximum value =", max }' $ $ ./tst.sh Enter a value: 1 Enter a value: 2 Enter a value: total = 3 minimum value = 1 average value = 1.5 maximum value = 2 $ $ ./tst.sh Enter a value: total = NAN minimum value = NAN average value = NAN maximum value = NAN The input is terminated by the user typing control-D or reaching the end of file. Note it wasn't necessary to write a loop (shell or otherwise) and the script handles the case where the user doesn't enter any input by just printing "Not A Number" for all the variables and it handles the case where the average is not a whole number, etc., etc. It's a much better solution in every way than the shell loop. Regards, Ed.
From: Seebs on 7 Feb 2010 12:26 On 2010-02-07, Ed Morton <mortonspam(a)gmail.com> wrote: > Writing a shell loop is something you > should do after you've thought about whether or not it's really the best > approach as it usually isn't Define "usually". The circumstances in which I find myself writing loops in shell scripts are usually cases where I want to perform a series of operations on each of several files, and while existing tools can often perform a single operation on several files, trying to do them in a pipeline seems doomed to failure. -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: find directory jpg and rename Next: is there a bash equivalent of "this" ... |