I am trying to capture the output of an aws ec2 delete-snapshot
in a Bash script command but I cannot get anything to capture the output. I have tried result=$(command)
, result=`command`
etc. but when I try to echo $result
there is nothing there.
Here is some example output.
root@host:~# aws ec2 delete-snapshot --snapshot-id vid --output json>test
A client error (InvalidParameterValue) occurred when calling the DeleteSnapshot operation: Value (vid) for parameter snapshotId is invalid. Expected: 'snap-...'.
root@host:~# aws ec2 delete-snapshot --snapshot-id vid>test
A client error (InvalidParameterValue) occurred when calling the DeleteSnapshot operation: Value (vid) for parameter snapshotId is invalid. Expected: 'snap-...'.
root@host:~# cat test
root@host:~# testing=$(aws ec2 delete-snapshot --snapshot-id vid)
A client error (InvalidParameterValue) occurred when calling the DeleteSnapshot operation: Value (vid) for parameter snapshotId is invalid. Expected: 'snap-...'.
root@host:~# echo $testing
root@host:~#
I need to automate creation and deletion of snapshots, but I can't capture the output.
Has anyone else ran into this issue?
Best Answer
The
>
operator only redirectsstdout
("standard output"), or "file descriptor1
". Error messages are usually printed on a different file descriptor,2
,stderr
, ("standard error"). On your terminal screen, you are seeing bothstdout
andstderr
.The
>
operator is really more just a shortcut for1>
, and again, only redirectsstdout
. The2>
operator is similar to1>
but it instead of redirectingstdout
, it redirectsstderr
.So, to redirect both
stdout
andstderr
to the same file, use>file 2>&1
.This says, "redirect the echo's
stderr
to/dev/null
, and redirectstdout
to stderr.In modern Bash, you can also use
&>
to redirect both streams to the same file:So for you, specifically, use:
Or