Re: shell script and variable problem
On Fri, Dec 01, 2006 at 06:51:05PM -0900, Ken Irving wrote:
> > The problem is that my test
> > if [ -n $exlist -a -f $exlist ]
> > does not work. For some reason, this always evaluate to true, and then
> > the tar command fails. Why is this?
> Not sure, but it'll work if you enclose the variable in quotes.
> > My understanding is -n will check if $exlist is empty or not, and if it
> > is, it will fail. And even if it doesn't, -f $exlist should fail since
> > it wouldn't be able to find that file.
> My guess is that the variable expands to nothing, and then the -n
> test is applied to the next token it sees, perhaps the -a?
> I've run into the same issue recently, and would like to somehow become
> less baffled by the ins and outs of shell (bash) scripting. Presumably
> there's a statement in bash(1) that would clarify what's going on.
This may be off the mark, but browsing the bash source I see this
comment in subst.c:
/* Partially and wholly quoted strings which expand to the empty
string are retained as an empty arguments. Unquoted strings
which expand to the empty string are discarded. The single
exception is the case of expanding "$@" when there are no
positional parameters. In that case, we discard the expansion. */
This sounds like it matches the symptoms anyway, though it still
seems odd to me.
Ken Irving, firstname.lastname@example.org