大网数据广告活动
服务器运维问答

Why can't I capture AWS EC2 CLI Output in Bash?

· 52CE 我爱测

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…

问题原文

原帖链接 · JoshZ · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · Will · CC BY-SA 3.0

The > operator only redirects stdout ("standard output"), or "file descriptor 1". Error messages are usually printed on a different file descriptor, 2, stderr, ("standard error"). On your terminal screen, you are seeing both stdout and stderr.

The > operator is really more just a shortcut for 1>, and again, only redirects stdout. The 2> operator is similar to 1> but it instead of redirecting stdout, it redirects stderr.

user@host$ echo hello world >/dev/null
user@host$ echo hello world 1>/dev/null
user@host$ echo hello world 2>/dev/null
hello world
user@host$

So, to redirect both stdout and stderr to the same file, use >file 2>&1.

user@host$ echo hi 2>/dev/null 1>&2
user@host$

This says, "redirect the echo's stderr to /dev/null, and redirect stdout to stderr.

user@host$ curl --invalid-option-show-me-errors >/dev/null
curl: option --invalid-option-show-me-errors: is unknown
try 'curl --help' or 'curl --manual' for more information

user@host$ curl --invalid-option-show-me-errors 2>/dev/null
user@host$ 
user@host$ curl --invalid-option-show-me-errors >/dev/null 2>&1
user@host$ 

In modern Bash, you can also use &> to redirect both streams to the same file:

user@host$ curl --invalid-option-show-me-errors &>/dev/null
user@host$ 

So for you, specifically, use:

aws ec2 delete-snapshot --snapshot-id vid --output json >test 2>&1

Or

aws ec2 delete-snapshot --snapshot-id vid --output json &>test

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-05-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。