简体   繁体   English

(BASH) 将循环变量传递给将存储在变量中的命令

[英](BASH) Passing a loop variable into command that will be stored in a variable

I am trying to iterate through a line-by-line list of file ID strings (testIDs.txt) and pass them to an AWS command.我正在尝试逐行遍历文件 ID 字符串 (testIDs.txt) 列表并将它们传递给 AWS 命令。 The command should utilize the loop variable, and the output of the command should be stored in the "folder" variable.该命令应使用循环变量,命令的输出应存储在“文件夹”变量中。 Whenever I run the script, I get blank spaces as output in the terminal.每当我运行脚本时,我都会在终端中得到空格作为输出。

#!bin/bash

while read p; do
    folder=$(aws s3 ls s3://a-bucket/ --recursive | grep "${p}" | cut -c 32-)
    echo "${folder}"
done < testIDs.txt    

The output of the AWS command should be two strings, and I have checked that this is true by running the AWS line separately in the terminal and using a string instead of ${p} . AWS 命令的输出应该是两个字符串,我已经通过在终端中单独运行 AWS 行并使用字符串而不是${p}来检查这是真的。

Note: Right now, I simply want to print folder , but later I will pass folder to another loop.注意:现在,我只想打印folder ,但稍后我会将folder传递给另一个循环。

You may want to just use two for loops, (1) read contents of the bucket to temp (optional/could search it directly), (2) Loop through IDs and (3) loop through each line of the bucket and look for the ID.您可能只想使用两个 for 循环,(1) 将存储桶的内容读取到临时文件(可选/可以直接搜索),(2) 遍历 ID 和 (3) 遍历存储桶的每一行并查找ID。

Example:例子:

Read the folder structure to local temp, then for every ID look at the line of the file check for the testIDs and print them to a results file.将文件夹结构读取到本地临时文件,然后针对每个 ID 查看文件行,检查 testID 并将它们打印到结果文件。

#!bin/bash
TMP="/tmp/s3-log-${RANDOM}.dat"
RESULTS="/tmp/s3-log-results.txt"
ID_FILE="testIDs.txt"

> "${RESULTS}"
aws s3 ls s3://a-bucket/ --recursive > "${TMP}"
while read p; do
    while IFS= read -r line ;do
        if ! [[ $line = *"${p}"* ]]; then
            echo "id: ${p} => $(echo ${line} | cut -c 32-)"  | tee -a "${RESULTS}"
        fi
    done < "${TMP}"        
done < "${ID_FILE}"

This is probably what you're looking for;这可能就是您要找的; use the while loop if you don't know how many values you need to loop through:如果您不知道需要循环多少个值,请使用 while 循环:

while read -r -d $'\t' resourceName; do
    echo "$resourceName"
done <<< "$(aws transfer list-servers --query='Servers[*].ServerId' --output text)"

In this case it's the -d $'\t' that influences the Bash word-splitting on the list of output AWS resources.在本例中,是-d $'\t'影响输出 AWS 资源列表上的 Bash 分词

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 BQ 存储过程中将 URI 作为变量传递 - How to pass URIs as variable in BQ stored procedure 在 ng-repeat 中使用变量并将其传递给 controller - Using a variable in ng-repeat and passing it to a controller Airflow 中 BigQueryInsertJobOperator 中的变量传递抛出错误 - Variable passing throwing error in BigQueryInsertJobOperator in Airflow ReferenceError: Can't find variable: i (for循环问题) - ReferenceError: Can't find variable: i (for-loop problem) 如何将 CodeBuild 环境变量放入命令参数中? - How can I place a CodeBuild envrionment variable inside a command parameter? 直接将变量内容传递给需要文件名的命令输入 - Directly pass variable content to command input expecting a filename 如何在 gcp sql 查询命令中使用 sh 变量 - How to use a sh variable in a gcp sql query command 如何在 gcloud ssh 的 --command 选项中使用变量 - How can I use a variable inside a --command option in gcloud ssh -bash:aws:找不到命令 - -bash: aws: command not found AWS Cloudformation - 将 CommaDelimitedList 参数转换为字符串以将其作为环境变量传递给 Lamda Function - AWS Cloudformation - Convert CommaDelimitedList Parameter to String for passing it as environment variable to Lamda Function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM