LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-23-2007, 04:19 AM   #1
Kristofer
LQ Newbie
 
Registered: Nov 2005
Location: Sweden
Distribution: Slackware
Posts: 20

Rep: Reputation: 0
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
 
Old 02-23-2007, 04:36 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
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]

Last edited by theYinYeti; 03-05-2007 at 11:29 AM.
 
Old 02-23-2007, 04:41 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
 
Old 02-26-2007, 06:40 AM   #4
Kristofer
LQ Newbie
 
Registered: Nov 2005
Location: Sweden
Distribution: Slackware
Posts: 20

Original Poster
Rep: Reputation: 0
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
 
Old 02-27-2007, 05:52 PM   #5
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
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"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Command Substitution dakensta Programming 5 11-30-2006 03:10 PM
Varible substitution in Bash script RonV Linux - Newbie 2 06-04-2006 12:11 PM
Bash variable substitution daYz Programming 3 04-14-2006 01:16 PM
Bash Process Substitution joshholt Programming 4 10-11-2005 03:15 AM
bash assignments of commands substitution p0rtius Programming 6 07-03-2005 06:41 PM


All times are GMT -5. The time now is 10:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration