[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why is command substitution happening?
From: |
Greg Wooledge |
Subject: |
Re: Why is command substitution happening? |
Date: |
Tue, 12 Apr 2011 10:03:51 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Mon, Apr 11, 2011 at 12:23:15PM -0600, Bill Gradwohl wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
>
> I have a tiny script to prune empty directories from a structure. The
> essential line is as follows:
>
> tree --noreport -dfi "${1}" | tac | sed 's/"/\\"/g;s/^/rmdir
> - --ignore-fail-on-non-empty "/;s/$/"/'|bash
Not good. You're piping filenames into bash and letting bash parse them
as commands. If a filename contains something like $(...) or `...` then
yes, bash would perform a command substitution.
Just do this instead:
find . -type d -exec rmdir {} \; 2>/dev/null
It's a bit of a hack; it relies on the fact that rmdir will fail on
non-empty directories, so we don't have to CHECK them. It discards
all errors, which is the hackish part. If this isn't good enough,
learn find in more depth.
http://mywiki.wooledge.org/UsingFind