0

I have a command processing function that runs its arguments and outputs the results to a file. The outputs of those commands must now be PROCESSED just before going to the file.

I have only one requirement: echo "var is: $var" must show the outcome which will be used further down in the code.

THIS BREAKS MY REQUIREMENT: "${@:1}" | perl -pe 's/\**//g' >> './test.log'

I imagine something like this:

"${@:1}" 1>> perl -pe 's/\**//g' >> './test.log'

but it does not work.

I have simplified my code down to the following:

process() {
    "${@:1}" 1>>'./test.log'
}

process eval 'var=$(echo -e "*****ONE *****WORD"); echo -e "$var";'
echo "var is: $var"

Currently this function outputs:

 screen :"var is: *****ONE *****WORD"

 logfile:"*****ONE *****WORD"

.

Required result:

 screen :"var is: *****ONE *****WORD" 

 logfile:"ONE WORD"

How can I achieve this?

PS. muru asked me why I use eval. I answered, because there is no other way to populate the variable without duplicating code. Unfortunately our discussion under his answer was deleted.

conanDrum
  • 457

1 Answers1

2

Since you're using eval here to set the variable for some weird reason, it must be run in the current shell, which is not the case with pipes. So you probably need process substitution:

process () {
    "${@}" > >(perl ... >> test.log)
}

For example:

$ process () {
 "${@}" > >(perl -pe 's/\**//g' >> test.log)
}
$ process eval 'var=$(echo -e "*****ONE *****WORD"); echo -e "$var";'
$ echo "var is: $var"
var is: *****ONE *****WORD
$ cat test.log
ONE WORD
muru
  • 72,889