开发者

Redirecting the output of program which itself is an argument

开发者 https://www.devze.com 2023-02-20 02:51 出处:网络
Let me present the scenario first with the command which is not working under linux bash environment.

Let me present the scenario first with the command which is not working under linux bash environment.

$ timed-run prog1 1>/dev/null 开发者_Python百科2>out.tmp

Here in the above case I want to redirect the output of program 'prog1' to /dev/null and out.tmp file. But this command is redirecting the output (if any) of timed-run to out.tmp.

Any help will be appreciated.


From a simple example, I experience exactly the opposite.

$ time ls 1> foo 2> bar

real    0m0.002s
user    0m0.004s
sys     0m0.000s
$ more foo
<show files>
$ more bar
<empty>
$

The output of ls is redirected, and the output of time is not!


The problem here is in timed-run not in bash. If you run the same command replacing timed-run with the standard time command this works as you expect. Mainly timed run needs to run the arguments of prog1 through the shell again. If it is a shell script you can do this with the eval command. For example:

#!/bin/sh

echo here is some output
echo $*
eval $*

now run

timed-run prog1 '1>/dev/null' '2>output.tmp'


How about using sh -c 'cmd' like so:

    time -p sh -c 'ls -l xcvb 1>/dev/null 2>out.tmp'
    time -p sh -c 'exec 0</dev/null 1>/dev/null 2>out.tmp; ls -l xcvb'

    # in out.tmp:
    # ls: xcvb: No such file or directory
0

精彩评论

暂无评论...
验证码 换一张
取 消