用循环附加字符串到变量,将格式完好无损地保持在bash中

发布于 2025-02-03 10:41:50 字数 1909 浏览 5 评论 0原文

我现在对冲刺的要求感到震惊,无法破解它,并要求您帮助我有点复杂的要求。

  1. 我有一个循环,可以输出JSON文件列表。

in my_list中的i;做回声$ i;完成 根据环境,此列表可能在15至30之间变化。

$ i输出看起来像这样“ Apple.jsonboy.json”等。

  1. 我现在想要的输出$ i在下面的curl命令中,并在上保持“ action [] [file_path] = json_files/$ sync_env/$ i.json” \- -form“ action [] [content] =< $ i.json” \

请注意,没有文件可以附加,并且通过分配给变量,所有这些都是动态的 例如:my_ultimation_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. 如下...
         ${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. 循环结束后的预期输出和$ my_ultimate_dynamic_command /代码>

如果有人可以帮助我,请感谢它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2025-02-10 10:41:50

尝试一下

#!/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[@]}"

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[@]}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文