bash Jenkins 无法访问 shell 别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18019335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jenkins can't access shell alias
提问by Daniel Brady
I have configured a Jenkins job to source a bash script that sources anotherbash script which adds an alias to the .bashrc
of its user and sources the .bashrc
itself, and then original script tries to use that alias (set by the second). However, it cannot seem to find the alias it has just created. I am not using any scripting plugins aside from using a "Send files or execute commands over SSH" build step to source the script.
我已经配置了一个 Jenkins 作业来获取一个 bash 脚本,该脚本获取另一个bash 脚本,该脚本.bashrc
向其用户添加别名并获取其.bashrc
自身,然后原始脚本尝试使用该别名(由第二个设置)。但是,它似乎无法找到它刚刚创建的别名。除了使用“通过 SSH 发送文件或执行命令”构建步骤来获取脚本之外,我没有使用任何脚本插件。
The job does this:
工作是这样的:
source ./test_script.sh
test_script.sh
looks like this:
test_script.sh
看起来像这样:
echo "In test_script.sh"
echo $USER
echo $HOME
source ./setup_env.sh
echo "\nBack in test_script.sh"
alias foo
foo
And finally, setup_env.sh
looks like this:
最后,setup_env.sh
看起来像这样:
echo "\nIn setup_env.sh"
echo "alias foo=\"echo foobar\"" >> $HOME/.bashrc
source $HOME/.bashrc 2>/dev/null
cat $HOME/.bashrc
The output I receive from the Jenkins job looks like this:
我从 Jenkins 作业收到的输出如下所示:
In test_script.sh
my_user
/home/my_user
\nIn setup_env.sh
...all of my bashrc...
alias foo="echo foo"
\nBack in test_script.sh
alias foo='echo foo'
./test_script.sh: line 7: foo: command not found
I don't understand why this is happening, when I can happily run it myself on the command-line and watch it succeed. Why can't Jenkins use the new alias, when it can obviously find it (as demonstrated by the output of the alias foo
command)?
我不明白为什么会发生这种情况,当我可以愉快地在命令行上自己运行它并看到它成功时。为什么 Jenkins 不能使用新别名,因为它显然可以找到它(如alias foo
命令的输出所示)?
采纳答案by Gonen
The \n
that is showing in the output of your echo
commands
suggest this is not running under bash
, as you may be expecting.
该\n
是你的输出,显示echo
命令
提示,这是没有运行下bash
,你可能期望。
Please check the setting of your jenkinsuser
(or any other user that you run Jenkinswith) -
especially the setting of the default shell.
请检查您的jenkins用户
(或您运行Jenkins 的任何其他用户)
的设置——尤其是默认 shell的设置。
To test this, add one of those lines at the beginning of your script:
要对此进行测试,请在脚本的开头添加以下行之一:
env
or
或者
env | grep -i shell
Should also consider making sure your scripts run under the correct shell,
by adding the "shebang" line as the first line in each script.
In the case of 'bash', for example, you should add the following first line:
还应该考虑
通过在每个脚本的第一行添加“ shebang”行来确保您的脚本在正确的 shell 下运行。
例如,在 'bash' 的情况下,您应该添加以下第一行:
#!/bin/bash
(it is not a comment, despite what the auto-syntax-highlighter may think...)
(这不是评论,尽管自动语法高亮可能会这么想......)
回答by Galen Long
For anyone else who's having this problem, you may need to set the expand_aliases
shell option, which seems to be off by default with Jenkins:
对于遇到此问题的任何其他人,您可能需要设置expand_aliases
shell 选项,默认情况下,Jenkins 似乎已关闭该选项:
shopt expand_aliases # check if it's on
shopt -s expand_aliases # set expand_aliases option to true
shopt expand_aliases # it should be on now
# test with a simple alias, should print 1
alias x="python -c 'print 1'"
x