0

I am struck with the requirements for a sprint now and unable to crack it and request you to help me with a bit complex requirement.

  1. I have a loop that outputs a list of JSON files.

for i in my_list; do echo $i; done This list may vary between 15 and 30 based on the environment.

the $i output looks something like this "apple.json, boy.json", etc.

  1. I now want the output of $i in the below curl command and keep on append to --form "actions[][file_path]=JSON_FILES/$SYNC_ENV/$i.json" \ and --form "actions[][content]=<$i.json" \.

Kindly note there are no files to append and all this is dynamic by assigning to a variable eg: my_ultimate_dynamic_command

         ${CURLPOST} --form "branch=default-branch" \
                        --form "commit_message=[ Syncing ]" \
                        --form "actions[][action]=update" \
                        --form "actions[][file_path]=JSON_FILES/$SYNC_ENV/$i.json" \
                        --form "actions[][content]=<$i.json" \
                        -H "PRIVATE-TOKEN:$TOKEN" "https://gitlab.myorg.com/api/v4/projects/$prid/repository/commits"

  1. expected output after the loop ends and echo of $my_ultimate_dynamic_command as below...
         ${CURLPOST} --form "branch=default-branch" \
                        --form "commit_message=[ Syncing ]" \
                        --form "actions[][action]=update" \
                        --form "actions[][file_path]=JSON_FILES/$SYNC_ENV/apple.json" \
                        --form "actions[][content]=<apple.json" \
                        --form "actions[][action]=update" \
                        --form "actions[][file_path]=JSON_FILES/$SYNC_ENV/boy.json" \
                        --form "actions[][content]=<boy.json" \
                         ...and so on until $i is empty
                        -H "PRIVATE-TOKEN:$TOKEN" "https://gitlab.myorg.com/api/v4/projects/$prid/repository/commits"

  1. Finally, the dynamically generated variable should be executed as eval $my_ultimate_dynamic_command

Appreciate it if someone could help me with this.

Venu Reddy
  • 39
  • 8
  • 1
    Storing commands in variables tends not to work except in very simple cases (see [BashFAQ #50: "I'm trying to put a command in a variable, but the complex cases always fail!"](http://mywiki.wooledge.org/BashFAQ/050)), and `eval` just makes it even more confusing. Generally, the best solutions are 1) don't try to store it at all, just execute it directly, or 2) store it in an array rather than a plain variable (see [this question](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia)). – Gordon Davisson May 30 '22 at 18:05

1 Answers1

2

Try this

#!/bin/bash
SYNC_ENV=????
TOKEN=????

declare -a CURLPOST
CURLPOST+=("some options")
CURLPOST+=("other options")
CURLPOST+=("--form" "\"branch=default-branch\"")
CURLPOST+=("--form" "\"commit_message=[ Syncing ]\"")


my_list="apple boy"
for i in ${my_list}; 
do 
    echo $i; 
    CURLPOST+=("--form" "\"actions[][action]=update\"")
    CURLPOST+=("--form" "\"actions[][file_path]=JSON_FILES/$SYNC_ENV/${i}\"")
    CURLPOST+=("--form" "\"actions[][content]=\<${i}\"")                         
done

CURLPOST+=("-H" "\"PRIVATE-TOKEN:$TOKEN\"" "\"https://gitlab.myorg.com/api/v4/projects/$prid/repository/commits\"")

# see the content of your array
declare -p CURLPOST

# exec curl with options 
curl "${CURLPOST[@]}"
ufopilot
  • 3,269
  • 2
  • 10
  • 12
  • 1
    Nice approach, but `\"actions[]...` looks strange. (Forgot to enclose with double quotes?) – tshiono May 30 '22 at 23:14
  • I am testing the above solution and below is the issue `declare -A CURLARGS` `CURLARGS+=("--form" "\"branch\=sync\"")` bash: CURLARGS: "--form": must use subscript when assigning associative array bash: CURLARGS: "\"branch\=sync\"": must use subscript when assigning associative array – Venu Reddy Jun 08 '22 at 09:59
  • @VenuReddy use `declare -a `for indexed array instead of `declare -A ` for associative array – ufopilot Jun 08 '22 at 12:17