BASH脚本-不是预期的输出?

时间:2018-11-11 02:01:13

标签: bash curl echo counter

嗨,我这里有两个文本文件,boy.txt和girl.txt

boy.txt中的值是:

12345678
87654321

girl.txt中的值是:

35f1f125-5d33-44a4-82fc-26734843c804
5abd0360-c4be-4e69-8085-340755f87290

我必须在curl命令中使用这两个值,这是我的命令:

readarray boy < boy.txt
readarray girl < girl.txt

count=0
for i in ${girl[@]}
do
result[${boy[${count}]}]=${i}
count=$((count+1))

for output1 in ${!result[@]}; do
output2=${result[$output1]};


echo "curl -X PUT 
--header \"Content-Type: application/json\" 
--header \"Accept: application/json\" 
--header \"Authorization: Bearer gAAAAJ1EY28EHByJCcgT-sWhzid- cxW1uQrfO1QJG52D4n9GYj9YWd4KUGBSxO2LXZ48eEbQivSAlM6bplNE82o84UFnkwiVJ0V1q4DM9VAHw6jU4UvkdhhSrlupC6gKl_RHEgf2cluwvAN11My7eFflA-JnBrINp02d146CaJrR_r52hAEAAIAAAAAXmL1IDoHPz3S5deta0ygANUUoEFBsuKHaeDo3NXVUptxxDXR00yaNmILqd1psJckYhrBhh7wdQNdNYaSsqgwsF8Nuw_hhUAbg27hhGaVLmbeK3eS3mtVlq1D73Vg3KSk-24DmR0o-jT2u88k4MLfkqxuipEsLAEcSoe4zAPE1jcch2GGvlNWTHN98gMxw-svmvu6hizZ1fe-pWNtMGwa9u_hVimOr8A-7HcidA4Sj_jBprulGUapifc-Dd_qZFj1FM1HedHvnu8NKzp_uRr8z_Myk0IO6RyqjPPufLdafV3N4hmEqVex8P8jr4SkZWT0f-4i1epfZcRJmz6ognOCfaxa91c3qFNpG3IVY881g9aCPdLOxmU5FqKDPgHIZLvp8wWeTWdi_YQaAXMUh-yZQXBLhBimG8SR2iRj6ZTvu_L_Ytmc0QePkaijA6CB8wY2SbHlOGL2JUcEXKoNjC9dSSbkHqWx0tXMR2XCCdlhHPC1vGYcCgV-XQ_bfqgN7yn8\" 
-d \"\\"\"referenceId:"$output2"\\"\"\" \"https://website.com:443/testing/api/devices/"$output1"/parentPath\" " > test.sh

done
done

chmod+x test.sh

./test.sh

上面命令的输出是:

问题是仅回显了一个curl命令,而不是两个

curl -X PUT 
--header "Content-Type: application/json" 
--header "Accept: application/json" 
--header "Authorization: Bearer gAAAAJ1EY28EHByJCcgT-sWhzid-cxW1uQrfO1QJG52D4n9GYj9YWd4KUGBSxO2LXZ48eEbQivSAlM6bplNE82o84UFnkwiVJ0V1q4DM9VAHw6jU4UvkdhhSrlupC6gKl_RHEgf2cluwvAN11My7eFflA-JnBrINp02d146CaJrR_r52hAEAAIAAAAAXmL1IDoHPz3S5deta0ygANUUoEFBsuKHaeDo3NXVUptxxDXR00yaNmILqd1psJckYhrBhh7wdQNdNYaSsqgwsF8Nuw_hhUAbg27hhGaVLmbeK3eS3mtVlq1D73Vg3KSk-24DmR0o-jT2u88k4MLfkqxuipEsLAEcSoe4zAPE1jcch2GGvlNWTHN98gMxw-svmvu6hizZ1fe-pWNtMGwa9u_hVimOr8A-7HcidA4Sj_jBprulGUapifc-Dd_qZFj1FM1HedHvnu8NKzp_uRr8z_Myk0IO6RyqjPPufLdafV3N4hmEqVex8P8jr4SkZWT0f-4i1epfZcRJmz6ognOCfaxa91c3qFNpG3IVY881g9aCPdLOxmU5FqKDPgHIZLvp8wWeTWdi_YQaAXMUh-yZQXBLhBimG8SR2iRj6ZTvu_L_Ytmc0QePkaijA6CB8wY2SbHlOGL2JUcEXKoNjC9dSSbkHqWx0tXMR2XCCdlhHPC1vGYcCgV-XQ_bfqgN7yn8" 
-d "\"referenceId:35f1f125-5d33-44a4-82fc-26734843c804\"" "https://webite.com:443/testing/api/devices/12345678/parentPath"

我的预期输出:

在.sh文件中回显命令

curl -X PUT 
--header "Content-Type: application/json" 
--header "Accept: application/json" 
--header "Authorization: Bearer gAAAAJ1EY28EHByJCcgT-sWhzid-cxW1uQrfO1QJG52D4n9GYj9YWd4KUGBSxO2LXZ48eEbQivSAlM6bplNE82o84UFnkwiVJ0V1q4DM9VAHw6jU4UvkdhhSrlupC6gKl_RHEgf2cluwvAN11My7eFflA-JnBrINp02d146CaJrR_r52hAEAAIAAAAAXmL1IDoHPz3S5deta0ygANUUoEFBsuKHaeDo3NXVUptxxDXR00yaNmILqd1psJckYhrBhh7wdQNdNYaSsqgwsF8Nuw_hhUAbg27hhGaVLmbeK3eS3mtVlq1D73Vg3KSk-24DmR0o-jT2u88k4MLfkqxuipEsLAEcSoe4zAPE1jcch2GGvlNWTHN98gMxw-svmvu6hizZ1fe-pWNtMGwa9u_hVimOr8A-7HcidA4Sj_jBprulGUapifc-Dd_qZFj1FM1HedHvnu8NKzp_uRr8z_Myk0IO6RyqjPPufLdafV3N4hmEqVex8P8jr4SkZWT0f-4i1epfZcRJmz6ognOCfaxa91c3qFNpG3IVY881g9aCPdLOxmU5FqKDPgHIZLvp8wWeTWdi_YQaAXMUh-yZQXBLhBimG8SR2iRj6ZTvu_L_Ytmc0QePkaijA6CB8wY2SbHlOGL2JUcEXKoNjC9dSSbkHqWx0tXMR2XCCdlhHPC1vGYcCgV-XQ_bfqgN7yn8" 
-d "\"referenceId:35f1f125-5d33-44a4-82fc-26734843c804\"" "https://webite.com:443/testing/api/devices/12345678/parentPath"


curl -X PUT 
--header "Content-Type: application/json" 
--header "Accept: application/json" 
--header "Authorization: Bearer gAAAAJ1EY28EHByJCcgT-sWhzid-cxW1uQrfO1QJG52D4n9GYj9YWd4KUGBSxO2LXZ48eEbQivSAlM6bplNE82o84UFnkwiVJ0V1q4DM9VAHw6jU4UvkdhhSrlupC6gKl_RHEgf2cluwvAN11My7eFflA-JnBrINp02d146CaJrR_r52hAEAAIAAAAAXmL1IDoHPz3S5deta0ygANUUoEFBsuKHaeDo3NXVUptxxDXR00yaNmILqd1psJckYhrBhh7wdQNdNYaSsqgwsF8Nuw_hhUAbg27hhGaVLmbeK3eS3mtVlq1D73Vg3KSk-24DmR0o-jT2u88k4MLfkqxuipEsLAEcSoe4zAPE1jcch2GGvlNWTHN98gMxw-svmvu6hizZ1fe-pWNtMGwa9u_hVimOr8A-7HcidA4Sj_jBprulGUapifc-Dd_qZFj1FM1HedHvnu8NKzp_uRr8z_Myk0IO6RyqjPPufLdafV3N4hmEqVex8P8jr4SkZWT0f-4i1epfZcRJmz6ognOCfaxa91c3qFNpG3IVY881g9aCPdLOxmU5FqKDPgHIZLvp8wWeTWdi_YQaAXMUh-yZQXBLhBimG8SR2iRj6ZTvu_L_Ytmc0QePkaijA6CB8wY2SbHlOGL2JUcEXKoNjC9dSSbkHqWx0tXMR2XCCdlhHPC1vGYcCgV-XQ_bfqgN7yn8" 
-d "\"referenceId:5abd0360-c4be-4e69-8085-340755f87290\"" "https://webite.com:443/testing/api/devices/87654321/parentPath"

或者,如果您有一种方法(正确的语法),以便我可以直接卷曲,请多多感激。

1 个答案:

答案 0 :(得分:1)

要附加而不是覆盖时,请使用>>代替>

..."$output1"/parentPath\" " > test.sh

应该是

..."$output1"/parentPath\" " >> test.sh

编辑:我认为您可以使用一些比您正在做的事情简单的事情:

#!/bin/bash

while read -r boy && read -r girl <&3; do
    # I omitted parts of the curl command with ...
    echo "curl -X PUT ... referenceId:$girl ...api/devices/$boy/" >> test.sh

done < boy.txt 3<girl.txt