Bash 脚本在本地运行但在 CI 中返回语法错误
Bash script working locally but returning syntax error in CI
在我的 gitlab CI 我是 运行 下面的简单脚本 (.gitlab-ci.yml):
STR=$(cat $FILE)
if grep -q "substring" <<< "$STR"; then echo "ok"; fi
不幸的是,这给了我错误
/bin/sh: eval: line 100: syntax error: unexpected redirection
运行 与脚本相同的命令在本地按预期运行:
#!/bin/sh
FILE="./file.txt"
STR=$(cat $FILE)
if grep -q "substring" <<< "$STR"; then
echo "ok"
fi
文件内容为:
This has a substring somewhere
/bin/sh
不是 bash
,<<<
是一个 bash 扩展,并非每个 shell 都可用。安装 bash,将 shebang 更改为 /bin/bash
并确保脚本是 bash 下的 运行 或使用 posix 兼容语法 printf "%s\n" "$str" | grep...
注意:大写变量按照惯例保留用于导出变量,如 IFS
COLUMNS
PWD
UID
EUID
LINES
等. 在脚本中使用小写变量。
在我的 gitlab CI 我是 运行 下面的简单脚本 (.gitlab-ci.yml):
STR=$(cat $FILE)
if grep -q "substring" <<< "$STR"; then echo "ok"; fi
不幸的是,这给了我错误
/bin/sh: eval: line 100: syntax error: unexpected redirection
运行 与脚本相同的命令在本地按预期运行:
#!/bin/sh
FILE="./file.txt"
STR=$(cat $FILE)
if grep -q "substring" <<< "$STR"; then
echo "ok"
fi
文件内容为:
This has a substring somewhere
/bin/sh
不是 bash
,<<<
是一个 bash 扩展,并非每个 shell 都可用。安装 bash,将 shebang 更改为 /bin/bash
并确保脚本是 bash 下的 运行 或使用 posix 兼容语法 printf "%s\n" "$str" | grep...
注意:大写变量按照惯例保留用于导出变量,如 IFS
COLUMNS
PWD
UID
EUID
LINES
等. 在脚本中使用小写变量。