[wplug] bash for i?

Vance Kochenderfer vkochend at nyx.net
Thu Sep 4 14:27:27 EDT 2008


Steve Polyack <korvus at comcast.net> wrote:
> You're missing the statement that tells bash/sh you are done with the 
> loop.  Try it this way:
> 
> for i in 'dpkg -l|grep xserver-xorg-video'; do 'dpkg --purge $i'; done

Also the quoting is incorrect.  As written, this will set the
parameter i to the literal text 'dpkg -l|grep xserver-xorg-video',
not the output of a command.  It will then attempt to run a
command literally called 'dpkg --purge $i' because the single
quotes make a single unsplittable word and prevent expansion of $i.

Here's an example of what I mean about word splitting:
  bash$ echo foo
  foo
  bash$ 'echo foo'
  bash: echo foo: command not found

To perform command substitution, use backticks.  I think this is
closer to what you're looking to do:

  for i in `dpkg -l|grep xserver-xorg-video`; do dpkg --purge $i; done

It'd be worthwhile to review the Advanced Bash Scripting Guide
<http://ldp.wplug.org/LDP/abs/html/index.html>; it is a really
useful resource for learning shell behavior.

Vance Kochenderfer        |  "Get me out of these ropes and into a
vkochend at nyx.net          |   good belt of Scotch"    -Nick Danger


More information about the wplug mailing list