Reputation: 24778
Here's what I am trying to do:
time sh -c "dd if=/dev/zero of=ddfile bs=512 count=125 && sync"
When run that on shell the o/p is
125+0 records in
125+0 records out
64000 bytes (62.5KB) copied, 0.028935 seconds, 2.1MB/s
real 0m 0.08s
user 0m 0.00s
sys 0m 0.01s
I want to capture the output to a variable or to a file. How do I do that ?
Thanks!
Upvotes: 1
Views: 1892
Reputation: 2042
Bash uses >
for standard output redirection. An example would be:
./test.sh > outfile.txt
If you're interested in appending to rather than replacing outfile.txt, use >>
instead of >
. Both redirect standard output.
However, the output of the time
command in this case will be to standard error, so you must use use 2>
rather than >
.
time sh -c "dd if=/dev/zero of=ddfile bs=512 count=125 && sync" 2> outfile.txt
This almost works, but the time
command works separately from the sh
shell command, and thus only the output of the sh
command will be redirected to outfile.txt using the above command. To include the output of the time
command, we need to wrap the entire command in parenthesis, as below:
(time sh -c "dd if=/dev/zero of=ddfile bs=512 count=125 && sync") 2> outfile.txt
Upvotes: 2
Reputation: 455380
You can do:
(time sh -c "dd if=/dev/zero of=ddfile bs=512 count=125 && sync") 2> file
Since you need to redirect the output of both time
and dd
you need to enclose the entire thing in (..)
.
Both time
and dd
send their output to stderr, so using >
will not work, you need 2>
which redirects stderr.
To get the output in a variable you can do:
var=$((time sh -c "dd if=/dev/zero of=ddfile bs=512 count=125 && sync") 2>&1)
Upvotes: 2
Reputation: 25789
time sh -c "dd if=/dev/zero of=ddfile bs=512 count=125 && sync" > ./yourfile
> is the redirect command
Upvotes: 1