[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
exported functions gain quotes and whitespace, without bound
From: |
linguist |
Subject: |
exported functions gain quotes and whitespace, without bound |
Date: |
26 Jun 2002 08:34:50 -0000 |
Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='i686'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu'
-DCONF_VENDOR='pc' -DSHELL -DHAVE_CONFIG_H -I. -I../src -I../src/include
-I../src/lib -g -O2
uname output: Linux monster 2.4.17-SMPs #10 SMP Sun May 12 08:47:19 EDT 2002
i686 unknown
Machine Type: i686-pc-linux-gnu
Bash Version: 2.05a
Patch Level: 0
Release Status: release
Description:
There is a case where defining a function, and then eval'ing the
results of
declare -f <funcname> will not reproduce the function properly. The
case
is caused by using ((arithmetic)) in the condition of an if or while
statement.
I had a look at the source, and noted a comment on line 3897 of y.tab.c,
which said that for an ARITH_FOR_COMMAND, it would remove the extra
quotes.
There does not appear to be any such allowance made in the handling of
if
and while.
Note that the equivelent action seems to occur when one exports the
function
and starts a subshell, so one does not have to do anything subversive
like
evaling the output of declare -f in order to have the problem.
I notice now that there are also problems with simpler constructs:
for example, foo() { (($#)) && echo; }. And oddly, eval $(declare -f
foo)
fails entirely, due to the lack of a <cr> or ; in the output. Exporting
the function works, but subshells do have the extra quotes and
whitespace.
I guess I'll include the questions all this raised: is eval $(declare
-f foo)
expected to work in the general case? Is there a batter way to achieve
this result?
Regards,
Rich Paul
Repeat-By:
---- cut here ----
#!/bin/bash
TEXT='
foo ()
{
if (($#)); then
echo true;
fi
}
declare -f foo
eval $(declare -f foo)
declare -f foo
eval $(declare -f foo)
declare -f foo
eval $(declare -f foo)
declare -f foo
'
echo The following text:
echo "$TEXT"
echo When piped through the shell, produces:
bash <(echo "$TEXT")
---- cut here ----
output:
---- cut here ----
The following text:
foo ()
{
if (($#)); then
echo true;
fi
}
declare -f foo
eval $(declare -f foo)
declare -f foo
eval $(declare -f foo)
declare -f foo
eval $(declare -f foo)
declare -f foo
When piped through the shell, produces:
foo ()
{
if (( "$#" )); then
echo true;
fi
}
foo ()
{
if (( " "$#" " )); then
echo true;
fi
}
foo ()
{
if (( " " "$#" " " )); then
echo true;
fi
}
foo ()
{
if (( " " " "$#" " " " )); then
echo true;
fi
}
---- cut here ----
Fix:
The fix for the problem appears to involve removing the excess quotes,
either
at initial parse or when
- exported functions gain quotes and whitespace, without bound,
linguist <=