Jenkins:在 bash 脚本中插入特殊字符
我正在里面执行一个 bash 脚本。像这样的命令在执行时
current_loc=/tmp/$BUILDTYPE
rm -rf $current_loc/*
[ -d $current_loc/ ] || mkdir -p $current_loc
会被替换为
+ current_loc=$'/tmp/stage\r'
+ rm -rf '/tmp/stage
/*
'
+ '[' -d $'/tmp/stage\r/' ']'
+ mkdir -p $'/tmp/stage\r\r'
dollar
quoting
和 \r
正在造成严重破坏。我该如何解决这个问题?
I am executing a bash script inside. A command like this when executed
current_loc=/tmp/$BUILDTYPE
rm -rf $current_loc/*
[ -d $current_loc/ ] || mkdir -p $current_loc
Get replaced by
+ current_loc=
The dollar
quoting
and \r
is creating havoc. How do I fix this?
/tmp/stage\r'
+ rm -rf '/tmp/stage
/*
'
+ '[' -d
The dollar
quoting
and \r
is creating havoc. How do I fix this?
/tmp/stage\r/' ']'
+ mkdir -p
The dollar
quoting
and \r
is creating havoc. How do I fix this?
/tmp/stage\r\r'
The dollar
quoting
and \r
is creating havoc. How do I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将 '\r' 符号替换为
BULDTYPE=$(echo $BULDTYPE | tr -d '\r');
等命令You must replace '\r' symbol with command like
BULDTYPE=$(echo $BULDTYPE | tr -d '\r');
在 Jenkins 中运行 BASH 脚本有多种方法。方法#1 是创建一个 bash 脚本并让 Jenkins 在构建过程中执行它。但是,您也可以直接在 Jenkins 中输入 BASH 脚本。你用什么方法来做这个?
我怀疑您在 Windows 上使用记事本编写了 shell 脚本,然后在 Unix 或设置为采用 Unix 行结尾的 Cygwin 安装上执行它。您看到的
\r
位于您的 shell 脚本本身中。您可以使用程序编辑器重新编辑脚本,然后将行结尾设置为 Unix 行结尾。在 VIM 中,您可以通过以下方式执行此操作:
在命令模式下。
如果您在 Windows 上使用 Notepad++(免费下载且开源),则选择
Edit->EOL Conversion- >Unix
菜单项。 (或者您可以使用 Vim)您可以运行大多数 Unix 和 Linux 上的
dos2unix
实用程序安装有,但我无论如何都会在程序编辑器中查看脚本。不要使用非程序编辑器来编辑程序。我见过人们使用 Winpad 并忘记将脚本保存为文本。相反,他们创建 RTF 格式的 shell 脚本。 RTF 格式让 Unix BASH shell 很伤心。
There are several ways to run a BASH script in Jenkins. Way #1 is to create a bash script and have Jenkins execute it as part of the build process. However, you can also type in a BASH script directly into Jenkins. Which way are you doing this?
My suspicion is that you've written your shell script using Notepad on Windows and then are executing it on Unix or on a Cygwin installation which is set to take Unix line endings. The
\r
you're seeing are there in your shell script itself.You can reedit the script using a program editor, and then setting the line endings to Unix line endings. In VIM, you can do this by:
in the Command Mode.
If you use Notepad++ on Windows (free download and open source), you select the
Edit->EOL Conversion->Unix
menu item. (Or you can use Vim)You can run the
dos2unix
utility that most Unixes and Linux installations have, but I'd look over the script anyway in a program editor.Do not use a non-program editor to edit a program. I've seen people use Winpad and forget to save the script as Text. Instead, they create a shell script in RTF format. RTF format makes Unix BASH shell sad.