in reply to Time for Some Movie Magick

for i in `ls ${image_path}*.jpg`
I wonder what the thinking is that gets people into this twisted level of code. Maybe it's just seeing it somewhere else, then cargoculting it without thinking.

So, given that this is likely the case, let me say, Don't do it this way.

Here's what you're asking your poor system to do:

  1. Shell, please fork
  2. Child shell, figure out what matches ${image_path}*.jpg
  3. Child shell, now pass that list to an ls that you forked
  4. ls, please go look up this list of names that I've already handed you. (And, by the way, if any of those are directories, expand them, even though that isn't what I want, so it'll be a bug I have to fix later.) And spit those names out one by one, with a newline.
  5. (Parent) shell, take the output of that child process, and create elements of a list, newline delimited (which breaks if any of the names contain whitespace such as newline, but that's another bug to be fixed later).
  6. Shell, start looping through that list, doing the following...
Now, let's see how we could have written it, properly:
for i in ${image_path}*.jpg
Yeah, that's it. Simple. Now look what we told the system to do:
  1. Shell, figure out what matches ${image_path}*.jpg
  2. Shell, start looping through that list, doing the following...
Not only is it faster and shorter to type, it's now also whitespace safe, newline safe, and directory safe!

So, why does the other form get cargo-culted so much? It's like the useless use of cat that I keep seeing all the time. My guess is that it's like that in a bad book or a bad FAQ, and then it gets copied and pasted from one person to the next, like a magical formula that nobody understands and yet repeats, as if the magic words will open up the door if uttered in the proper moonlight.

Just curious, I guess. Remember, this isn't personal... I'm just trying to figure out your thinking so that I can work out a counter-meme to it to help everyone at once, instead of one at a time like I keep ending up doing. Call me lazy. {grin}

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: Time for Some Movie Magick
by abitkin (Monk) on Oct 20, 2004 at 11:58 UTC
    Merlyn,
    I thank you. This is actually very constructive critisim. Hopefully, it's obvious that I don't do a lot of bash programming, and I picked the mentioned code up from other bash scripts I had laying around and guess and check. Unfortunately what I've found online very rarely explains in detail as you have, and is often times more like my code than yours.

    Once again, I thank you for keeping me honest, and for attempting to understand my thinking. I know it's scary inside my head, heck I have to live there every day.


    ==
    Kwyjibo. A big, dumb, balding North American ape. With no chin.