Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the bash PIPESTATUS array of an eval'd command?

Tags:

bash

I have this code:

error(){
    time=$( date +"%T %F" )
    echo "Start : ${time} : ${1}" 1>&2

    result=$( eval "${1}" )
    if [ `echo "${PIPESTATUS[@]}" | tr -s ' ' + | bc` -ne 0 ]; then 
        echo "command ${1} return ERROR" 1>&2
        exit
    else
        if [ "${2}" != "silent" ]; then
          echo "${result}"
        fi
    fi
}

I start testing command:

error "ifconfig | wc -l" "silent"
Start : 14:41:53 2014-02-19 : ifconfig | wc -l

error "ifconfiggg | wc -l" "silent"
Start : 14:43:13 2014-02-19 : ifconfiggg | wc -l
./install-streamserver.sh: line 42: ifconfiggg: command not found

But, I expect a different result. Example:

error "ifconfig" "silent"
Start : 14:44:52 2014-02-19 : ifconfig

Start : 14:45:40 2014-02-19 : ifconfiggg
./install-streamserver.sh: line 42: ifconfiggg: command not found
command ifconfiggg return ERROR  (<<<<<<<<<<<< This message)

I don't have it, because when bash runs a command with eval, as in

 eval "ifconfiggg | wc -l"

the $PIPESTATUS[@] array just contains "0" instead of the expected "1 0".

How can I fix this?

like image 924
user3327614 Avatar asked Oct 20 '22 14:10

user3327614


1 Answers

The eval starts a new shell context which has a separate PIPESTATUS[] array. The lifetime of this context ends when the eval ends. You can communicate this array to the parent context through assigning to a variable, say, PIPE as follows:

$ eval 'ifconfiggg | wc -l; PIPE=${PIPESTATUS[@]}'
bash: ifconfiggg: command not found
0
$ echo $PIPE
127 0

Note the single quotes to prevent ${PIPESTATUS[@]} from expanding too early.

Wrapping this in yet another result=$(...) does not work, since this creates yet another shell context. I suggest instead something along the lines of

eval "${1}; "'PIPE=${PIPESTATUS[@]}' >result.out 2>result.err
# do something with $PIPE here
# do something with result.out or result.err here

Note the use of both double quotes followed by single quotes.

like image 145
Jens Avatar answered Oct 31 '22 22:10

Jens