0

I'm trying to do the following:

uname>>1.txt | echo #####>>1.txt | echo uname>>1.txt &

to get the following output:

uname
## ## ## ## ##

Linux (or whatever the uname is)

But instead all I get as output is: uname

However if I try just:

uname>>1.txt | echo uname>>1.txt &

Then I do get the following output:

uname

Linux 

Wondering if there is some limitation to this sort of piped redirection?

=======================================================================

I'll be calling this shell command from within a tcl script. Well actually there are a list of commands being executed from within the tcl script, and the outputs need to be formatted in the following way <------->

I wanted to run them in background to decrease the execution time, as the outputs of these commands are not related to each other.

I thought the commands in () would output the formatted output to 1.txt as a background process.

Would you suggest another way of doing this?

1
  • Hmm, turns out when I use hyphen instead of hash, I do get all 3 piped outputs on file. But they are not ordered properly. With the order being 2,3,1 I was assuming that the file is appended in 3,2,1 order of re-directions.
    – egorulz
    Commented Nov 6, 2012 at 10:30

1 Answer 1

2

There are a number of problems here.

  1. In general it's a bad idea to combine output redirection and pipes. Once redirected, there's nothing left to pipe.
  2. Piping to echo doesn't make a bit of sense.
  3. Use parentheses to put a suite of commands in the background.
  4. You shouldn't be putting this in the background.
  5. In general commands run from left to right, not right to left.

What you want is

(echo uname > 1.txt; echo ------ >>1.txt; uname >>1.txt)


Update (per comments and changes to the question)

You are continuing to invoke what is essentially undefined behavior with this command:

uname>>1.txt | echo uname>>1.txt &

The pipe from uname is invalid because there's nothing to pipe once you have redirected output. The pipe to echo is invalid because doesn't read from standard input. Which of the uname or echo commands prints it's output first to the file 1.txt is up for grabs here. This is apparently what you want:

bash -c 'echo uname >> 1.txt; echo ------ >> 1.txt; uname >> 1.txt'

Note the -c option to bash. This tells bash that the argument following -c is a string that contains shell commands.

4
  • Thanks for the reply. Your suggestion seems to work while running from the CLI. But it fails when I try to pass the entire string as an argument to exec.
    – egorulz
    Commented Nov 6, 2012 at 11:33
  • 1
    Passing the entire string as an argument to exec? Of course that won't work. It doesn't make sense. Perhaps you meant eval? Commented Nov 6, 2012 at 13:57
  • @egorulz - I suggest that you edit your question along the lines of I want a shell command to do <X>. I've tried <A>, <B>, and <C>, but none of them seem to work. What do I need to use? You need to fill in that <X>, <A>, etc. Tell us what you are trying to do, and tell us why you think you need to use exec. Commented Nov 6, 2012 at 13:59
  • Edited the question. Will try and provide more context while I try out some more ideas.
    – egorulz
    Commented Nov 7, 2012 at 4:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.