[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

eval in functions in pipelines doesn't set variables globally

From: Markus . Schwarzenberg
Subject: eval in functions in pipelines doesn't set variables globally
Date: Mon, 13 Feb 2006 16:19:42 +0100 (MET)

Configuration Information [Automatically generated, do not change]:
Machine: sparc
OS: solaris2.8
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='sparc' 
-DCONF_OSTYPE='solaris2.8' -DCONF_MACHTYPE='sparc-sun-solaris2.8' 
-DCONF_VENDOR='sun' -DLOCALEDIR='/sw/opensrc/gnu/share/locale' -DPACKAGE='bash' 
-DSHELL -DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib  
-I/sw/opensrc/gnu/include -I/sw/opensrc/include -I/sw/opensrc/include/freetype2 
-I/usr/local/ssl/include -O6
uname output: SunOS suni2 5.8 Generic_117350-24 sun4u sparc SUNW,Sun-Blade-2500
Machine Type: sparc-sun-solaris2.8

Bash Version: 3.1
Patch Level: 0
Release Status: release

        Normally, the eval builtin used in functions to set variables
        makes these variables available globally otutside the function.
        However, when the function gets input from a pipline, the variables
        are set only locally. This bug was already present in bash-3.0, maybe
        also in earlier version. The correct behavior would be very usefull
        to be able to write functions which receive stdin and put it's 
        contens - preprocessed by awk or similar - into an array. 
        Note: What I atually wanted to do was to use 
function arrayset {
  eval $( gawk -vn=$1 '{gsub(/"/,"\\\"");printf("%s[%i]=\"%s\";",n,NR,$0);}' )
        to set up an array containing complete unmodified lines of input, 
        which is not possible using read -a (setting IFS to '\n' still 
        supresses whitespace betweeen the words read in, there).

function evalfunc_stdin  # shows the bug
  cat > /dev/null
  eval $1=$2
  eval echo "inside the function $1 = ${!1}"

function evalfunc_nostdin # works correctly 
  eval $1=$2
  eval echo "inside the function $1 = ${!1}"

Test Procedure: 
a) correct behavior, set A=test using evalfunc_nostdin:

        % unset A
        % echo $A

        % evalfunc_nostdin A test
        inside the function A = test
        % echo $A
---> variable A is correctly set globally

b) the bug using evalfunc_stdin
        % unset B
        % echo $B

        % echo | evalfunc_stdin B test
        inside the function B = test
        % echo $B
---> variable B is not set in global scope!     


reply via email to

[Prev in Thread] Current Thread [Next in Thread]