[英](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.