2

I want to execute the following command:

echo $RANDOM 2>&1 >> test.txt 

in this screen session:

screen -S test -X eval 'stuff "echo $RANDOM 2>&1 >> test.txt\015"'

However, instead of executing, it echoes the following:

echo  2>&1 >> test.txt

What am I doing wrong?

I want the echo command to not just to save its ouptut to test.txt, but to actually show that output to the screen as well.

2 Answers 2

1

You can do like this:

screen -S test -X exec bash -c 'echo RANDOM=$RANDOM 2>&1 >> test.txt'

If you want to see the output and save at the same time, you can use tee:

screen -S test -X exec bash -c 'echo RANDOM=$RANDOM 2>&1 | tee -a test.txt'
5
  • What's sh -c ? Dec 29, 2013 at 13:29
  • It will be run in cronjob, so I dont think that tee is a good idea Dec 29, 2013 at 13:30
  • Run specified command in sh shell. The tee there is within the shell command (part of the '...'), the output will be visible only inside the screen session.
    – janos
    Dec 29, 2013 at 13:32
  • This will work if and only if /bin/sh is Bash. The RANDOM variable does not exist in Dash.
    – Dennis
    Dec 29, 2013 at 13:38
  • @Dennis true, changed it bash
    – janos
    Feb 23, 2014 at 14:41
0
$ screen -S test -X stuff 'echo \$RANDOM >> /tmp/test 2>&1\n'

Or if you want to duplicate the output and manage it without tee by some reason

$ screen -S shell -X stuff 'echo \$RANDOM 2>&1 | while read; do printf "%s\\n" "\$REPLY"; printf "%s\\n" "\$REPLY" >> /tmp/test; done\n'

I assume you want redirect to file both stdin and stdout and your 2>&1 >> test.txt was an accidentally mistake.

3
  • What it does? It pastes echo $RANDOM > /tmp/test 2>&1 and executes it, but test.txt was not updated and got no output in actuall screen Dec 29, 2013 at 13:38
  • @Ultra Surely it doesn’t update ./test.txt – it updates /tmp/test. :) But what output you’ve expected if it was redirected to file by you. You may use tee, if you want to duplicate output. Dec 29, 2013 at 13:47
  • @Ultra It was not obvious for me from your question that you want to duplicate output. And you’ve mentioned you would like to avoid tee – I do not see any reason for that, but of course it possible with GNU Bash. So I’ve updated my answer. Dec 29, 2013 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .