LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH command substitution that starts with a pipe | (https://www.linuxquestions.org/questions/programming-9/bash-command-substitution-that-starts-with-a-pipe-%7C-531612/)

Kristofer 02-23-2007 04:19 AM

BASH command substitution that starts with a pipe |
 
I'm having trouble substituting a command that starts with a pipe |

Typing the command directly works fine: echo "some output" 2>&1 | tee -a afile

But instead of having to add 2>&1 | tee -a afile to each and every echo statement I want to substitute it for a variable, $TEE, using it like this: echo "some output" $TEE

I have tried quite a couple of ways but can't seem to get it to work,

TEE=2>&1 | tee -a afile <- probably doesn't even execute the command at all when I put $TEE after the echo command as described above.


Using eval and TEE="2>&1 | tee -a afile" <- doesn't seem to care about the preceding echo part
echo "..." eval $TEE


MYVAR=`cmd` executes the command and stores the output in the variable, = not what I'm looking for.
TEE=`2>&1 | tee -a afile` <- executes the command at the wrong time?, and therefore probably doesn't include the echo "some output", thereby making the command execution useless.

Any ideas?

/Kristofer

theYinYeti 02-23-2007 04:36 AM

This should work:
Code:

function echo() {
  /bin/echo "$@" 2>&1 | tee -a aFile
}

echo "hello !"

Yves.

[edit:]It's obviously better to use "/bin/echo" instead of "echo" in the echo function... You'll have to use the exact path though.[/edit]

matthewg42 02-23-2007 04:41 AM

You should quote the TEE assignment:
Code:

TEE="2>&1 | tee -a afile"
When you do this:
Code:

$ echo 'J. R. Bob Dobbs' $TEE
...the shell will expand the $TEE variable after constructing the pipeline (which in this case is just a single command), and so the output is:
Code:

J. R. Bob Dobbs 2>&1 | tee -a afile
What you need to do is expand the variable first, then evaluate the command:
Code:

eval "echo 'J. R. Bob Dobbs' $TEE"
Note that I changed your double quotes to single ones so that I could wrap the command in double quotes, which permit expansion of the $TEE, while still quoting what should be echoed. You could omit the outer, double quotes, but then you would have problems if you wished to echo something which contained multiple spaces.

Kristofer 02-26-2007 06:40 AM

Thx
 
Big thx to booth of you guys. After having tried booth solutions I decided to go for the one using the function since it clutters the original code much less and thereby also making it easier to switch between the echo writing to file and screen and the original writing only to screen.

/Kristofer

cfaj 02-27-2007 05:52 PM

Quote:

Originally Posted by Kristofer
I'm having trouble substituting a command that starts with a pipe |

Typing the command directly works fine: echo "some output" 2>&1 | tee -a afile

But instead of having to add 2>&1 | tee -a afile to each and every echo statement I want to substitute it for a variable, $TEE, using it like this: echo "some output" $TEE

I have tried quite a couple of ways but can't seem to get it to work,

TEE=2>&1 | tee -a afile <- probably doesn't even execute the command at all when I put $TEE after the echo command as described above.


Using eval and TEE="2>&1 | tee -a afile" <- doesn't seem to care about the preceding echo part
echo "..." eval $TEE


MYVAR=`cmd` executes the command and stores the output in the variable, = not what I'm looking for.
TEE=`2>&1 | tee -a afile` <- executes the command at the wrong time?, and therefore probably doesn't include the echo "some output", thereby making the command execution useless.

Any ideas?

/Kristofer


Code:

eval "echo QWERTY $TEE"


All times are GMT -5. The time now is 02:15 AM.