[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: variables set on command line
From: |
Eric Blake |
Subject: |
Re: variables set on command line |
Date: |
Wed, 24 Aug 2011 10:19:06 -0600 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110621 Fedora/3.1.11-1.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.11 |
On 08/24/2011 10:07 AM, Sam Steingold wrote:
* Eric Blake<roynxr@erqung.pbz> [2011-08-24 09:31:45 -0600]:
f(){ echo a=$a b=$b c=$c ; }
f
a= b= c=
a=a b=b f
a=a b=b c=
f
a=a b=b c=
Which is indeed correct under the rules for POSIX
This sucks big time.
Such is life when dealing with shell portability.
So if I want to bind a variable for an eval invocation and do this:
eval "`./libtool --tag=CC --config | grep '^archive_cmds='`"
CC='${CC}' libobjs='$libs' deplibs='${CLFLAGS}' compiler_flags='${CFLAGS}' \
soname='$dll' lib='$lib' output_objdir='$dyndir' \
eval XCC_CREATESHARED=\"${archive_cmds}\"
and I want CC to have an old value after the second eval, I need to save
it and restore it by hand, like this:
CC_save=$CC
CC='${CC}' libobjs='$libs' deplibs='${CLFLAGS}' compiler_flags='${CFLAGS}' \
soname='$dll' lib='$lib' output_objdir='$dyndir' \
eval XCC_CREATESHARED=\"${archive_cmds}\"
CC=$CC_save
however, this does not distinguish between unset CC and CC=''.
(is there a way to distinguish these two situations?)
Yes - autoconf does this all the time, using an idiom roughly like this:
CC_set=${CC+set}
CC_save=$CC
do stuff that modifies $CC
if test "$CC_set" = set; then
CC=$CC_save
else
unset CC
fi
Also, you can use command to suppress the ability of built-ins like eval
(but not function calls) to affect the current environment:
$ unset foo
$ foo=bar eval :
$ echo $foo
bar
$ unset foo
$ foo=bar command eval :
$ echo $foo
$
--
Eric Blake eblake@redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org