0

I am new to bash script and writing the bash script. I have declared an array and i have given the values. But When i pass the array in the loop values are not getting inside the loop. Below is my code

 declare -a files=( "A1S0" "D1S0" "D2S0" "D3S0" "D4S0" "D5S0" "D6S0" )   

     command_to_get_filesizes() {


        for i in ${#files[@]}; do
         echo test
        echo "${files[@]}"
        echo files[$i]

        echo  $(date +%m)
        aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query "Contents[?contains(Key, 'files[$i]$(date +%m%d)')]" --output text | awk '{print $2, $4}' >> "$FILESIZE"
        }
  command_to_get_filesizes()

Can u pls help me on this.. 'files[$i]$(date +%m%d)' array values should come in this place

eg: 'A1S0[$i]$(date +%m%d)'
1
  • Add a shebang and then paste your script there: shellcheck.net Commented May 22, 2020 at 7:58

1 Answer 1

1

Wrong syntax. What is the # doing there? Correct would be

 for i in "${files[@]}"; do

    echo Processing file $i
    .... 
    aws s3api list-objects --bucket ui-dl-weather-ecmwf-ltf --prefix daily/ --query "Contents[?contains(Key, '$i$(date +%m%d)')]" --output text | awk '{print $2, $4}' >> "$FILESIZE"

 done
Sign up to request clarification or add additional context in comments.

3 Comments

As suggested above, i tried, but $i is not printing anything... Can u pls help me on this
$i is printing the values as 1,2,3,4 instaed of values in the array
It does not, as you can easily see by trying out this code in a bash command line: declare -a files=( "A1S0" "D1S0" "D2S0"); for i in "${files[@]}"; do echo $i; done. Make sure to really copy and paste what I wrote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.