执行使用 jq 和 bash 创建的看似有效的 AWS CLI 命令时如何避免“无效参数类型”错误?

问题描述 投票:0回答:1

我有一个 s3 路径的文件,

sources.lst
。内容:

[
  "s3://bucket-name/path/to/file0.tif",
  "s3://bucket-name/path/to/file1.tif",
]

我正在尝试迭代这些 s3 路径并下载文件。我正在尝试使用

jq
和 bash while 循环。我的
download.sh
:

#!/bin/bash

set -o errexit

while read -r source
do
    echo "Attempting to execute:"
    echo "aws s3 cp "${source}" ."

    aws s3 cp "${source}" .
done < <(cat sources.lst | jq '.[]')

执行

bash download.sh
给我:

Attempting to execute:
aws s3 cp "s3://bucket-name/path/to/file0.tif" .

usage: aws s3 cp <LocalPath> <S3Uri> or <S3Uri> <LocalPath> or <S3Uri> <S3Uri>
Error: Invalid argument type

这很奇怪,因为如果我复制导致错误的行,即

aws s3 cp "s3://bucket-name/path/to/file0.tif" .

并在我运行的终端中执行

bash download.sh
,下载成功。

如何使用bash下载

sources.lst
中指定的文件?

我使用的是Mac,

bash --version
GNU bash, version 5.2.26(1)-release (aarch64-apple-darwin22.6.0)
aws --version
aws-cli/2.13.1 Python/3.11.4 Darwin/23.4.0 exe/x86_64 prompt/off

amazon-web-services bash amazon-s3 jq aws-cli
1个回答
0
投票

使用 jq 的

--raw-output
(或
-r
)标志将 JSON 字符串解码为原始文本(本质上是解析转义字符并删除双引号)。如果没有它,存储每个结果的 Bash 变量仍将包含编码。 (但是将其复制到终端可能会意外地起作用,因为 shell 在读取命令时会消耗这些引号。)

while read -r source
do aws s3 cp "${source}" .
done < <(jq -r '.[]' sources.lst)
© www.soinside.com 2019 - 2024. All rights reserved.