Syntax error in for loop

Hi,
I have below step to go through for loop. However I’m getting below synatx error.

Code:

steps:
  arti_lib_deploy:
    stage: build image
    type: freestyle
    title: "Deploy Libs to Artifactory"
    image: 'hub.artifactory.gcp.xxxx/curlimages/curl:latest'
    commands:
      - LIB_FOLDERS=["lib1","lib2"]
      - >
        for LIB_FOLDER in "${LIB_FOLDERS[@]}"; do
         echo "FolderName- ${LIB_FOLDER}"
         curl -X GET -kv https://xxxx.service.test/entitlements/effPermissions?permissionId= "${LIB_FOLDER}"
        done

Error:

Executing command: LIB_FOLDERS=["lib1","lib2"]
[2023-08-06T10:54:14.700Z] ------------------------------
Executing command: for LIB_FOLDER in "${LIB_FOLDERS[@]}"; do
echo "FolderName-${LIB_FOLDER }"
done

[2023-08-06T10:54:14.701Z] /bin/sh: syntax error: bad substitution
[2023-08-06T10:54:15.036Z] Reading environment variable exporting file contents.[2023-08-06T10:54:15.052Z] Reading environment variable exporting file contents.[2023-08-06T10:54:16.224Z] [SYSTEM]
Message
Failed to run freestyle step: Deploy Libs to Artifactory
Caused by
Container for step title: Deploy Libs to Artifactory, step type: freestyle, operation: Freestylestep. 
Failed with exit code: 2
Documentation Link https://codefresh.io/docs/docs/codefresh-yaml/steps/freestyle/
Exit code
2
Name
NonZeroExitCodeError

I found the solution:

    commands:
      - LIB_FOLDERS="lib1 lib2"
      - >
        for LIB_FOLDER in $LIB_FOLDERS; 
         do
         echo "FolderName- $LIB_FOLDER"
         curl -X GET -kv https://xxxx.service.test/entitlements/effPermissions?permissionId= "$LIB_FOLDER";
        done

Hello

I am glad you found a solution.

For this kind of work, I recommend you simply create a script in your git repo (called my_loop.sh) and have all the bash commands there.

Then in Codefresh just call ./my_loop.sh $LIB_FOLDERS

This will also make local debugging much simpler.

1 Like