此问题已在此处有答案:
Difference between sh and Bash(11个回答)
12天前关闭
下面我来介绍一下for loop
。但是我得到下面的synatx错误。
代码:
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
错误代码:
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
Sh命令:
commands:
- LIB_FOLDERS="lib1 lib2"
- for LIB_FOLDER in $LIB_FOLDERS;
do
echo "FolderName- $LIB_FOLDER"
done
错误代码:
Executing command: LIB_FOLDERS="lib1 lib2"
------------------------------
/bin/sh: syntax error: unexpected end of file (expecting "done")
[2023-08-06T23:48:22.532Z] Reading environment variable exporting file contents.
[2023-08-06T23:48:22.543Z] Reading environment variable exporting file contents.
2条答案
按热度按时间ttcibm8c1#
我不熟悉Codefresh,但你的问题让我对它如何与shell交互有了一些模糊的概念。
您的原始代码在其中一个命令中包含
"${LIB_FOLDERS[@]}"
。这是特定于LIB_FOLDERS
的语法(它扩展到LIB_FOLDERS
数组的所有元素),但错误消息表明Codefresh使用/bin/sh
。显然,/bin/sh
在您的系统上不是bash(通常不是);也许是灰或破折号。一个解决方案是避免bash特定的命令。另一个问题是如何说服Codefresh使用bash而不是
/bin/sh
。或者您也可以编写一个外部bash脚本,作为命令调用。你的第二次尝试,在一些评论之后是这样的:
其给出:
这意味着前面有
-
的每个命令都单独传递给/bin/sh
。一种解决方案是将循环写在一行中,例如:同样,如果这变得太复杂,最好将命令放在外部脚本中。特别要注意的是,
lib1
和lib2
恰好是单个单词,所以将它们组合成一个字符串,然后再拆分。如果这些文件夹名称中有空格,则必须执行更复杂的操作。eni9jsuy2#
相同的代码在Codefresh中使用多行,如下所示: