am trying to get the files from S3 bucket,starting with certain prefix. To do so am using aws cli command in the bash script.
Below is my code
#!/bin/bash
FILESIZE=$(mktemp)
declare -a files=( "A1S0" "D1S0" "D2S0" "D3S0" "D4S0" "D5S0" "D6S0" )
for n in "${!files[@]}"; do
printf '%8d %s\n' "${n}" ${files[n]}
echo aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query "Contents[?contains(Key, '${files[n]}$(date +%m%d)')]"
done
In my code there is a problem when expanding variables inside the quotes, $(date +%m%d)**Date variable is expanding but i need to avoid single quotes **'A1S00526' . How can i do that Pls help me
Output:
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, 'A1S00526')]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, 'D1S00526')]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, 'D2S00526')]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, 'D3S00526')]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, 'D4S00526')]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, 'D5S00526')]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, 'D6S00526')]
Expected output:
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, A1S00526)]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, D1S00526)]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, D2S00526)]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, D3S00526)]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, D4S00526)]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, D5S00526)]
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query Contents[?contains(Key, D6S00526)]
Can someone pls help me to expand the variables inside the quotes
When i run code as below, please find the attached output
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query \'"Contents[?contains(Key, ${files[n]}$(date +%m%d))]"\'
This is what you need to do:
#!/bin/bash
FILESIZE=$(mktemp)
declare -a files=("A1S0" "D1S0" "D2S0" "D3S0" "D4S0" "D5S0" "D6S0")
for n in "${!files[@]}"; do
#printf '%8d %s\n' "${n}" ${files[n]}
echo aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query \'"Contents[?contains(Key, ${files[n]}$(date +%m%d))]"\'
done
The output will be:
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query 'Contents[?contains(Key, A1S00526)]'
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query 'Contents[?contains(Key, D1S00526)]'
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query 'Contents[?contains(Key, D2S00526)]'
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query 'Contents[?contains(Key, D3S00526)]'
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query 'Contents[?contains(Key, D4S00526)]'
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query 'Contents[?contains(Key, D5S00526)]'
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query 'Contents[?contains(Key, D6S00526)]'
If you write you echo
as
echo aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query "Contents[?contains(Key, ${files[n]}$(date +%m%d))]"
you will get exactly the output you expected. If you actually want to run the command, and it is important that the quotes are passed to aws
, it becomes
for n in "${!files[@]}"; do
printf '%8d %s\n' "${n}" ${files[n]}
aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query "Contents[?contains(Key, '${files[n]}$(date +%m%d)')]"
done
In short: You want the quotes, you put them in. You don't want them, you leave them out.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.