来自ant的sshexec中bash脚本中的变量 [英] Variables in bash script in sshexec from ant
问题描述
我在 sshexec 任务中遇到变量问题.我的 build.xml ant 脚本如下所示:
I have a problem with variables in sshexec task. My build.xml ant script looks like:
<?xml version="1.0" encoding="UTF-8"?>
<project name="sshexecproject" basedir="." default="build">
<target name="build">
<sshexec
host="192.168.2.106"
username="root"
password="xxx"
commandResource="${basedir}/to_run.sh"
trust="true"
verbose="true"
failonerror="true"
/>
</target>
在 to_run 脚本中,我有两个变量:
In the to_run script I have two variables:
#!/bin/bash
name="Pink Panther"
export name2="Panther Pink"
echo "Name: "
echo $name
echo "Name with export: "
echo $name2
如果我在终端上运行脚本,我会得到以下输出:
If I run the script on terminal i get the following output:
$ ./to_run.sh
Name:
Pink Panther
Name with export:
Panther Pink
我们可以看到一切正常.但是如果我从 ant 启动 build.xml 脚本,我会得到以下输出:
We can see that all works fine. But if I start the build.xml script from ant i get the following output:
...
[sshexec] Authentication succeeded (password).
[sshexec] cmd : #!/bin/bash
[sshexec] cmd :
[sshexec] cmd : name="Pink Panther"
[sshexec] cmd : export name2="Panther Pink"
[sshexec] cmd :
[sshexec] cmd : echo "Name: "
[sshexec] Name:
[sshexec] cmd : echo $name
[sshexec]
[sshexec] cmd : echo "Name with export: "
[sshexec] Name with export:
[sshexec] cmd : echo $name2
[sshexec]
[sshexec] Disconnecting from ...
我们可以看到在远程服务器上这个脚本创建了一个空的回声.变量 name 和 name2 未填充.为什么?
We can see that on the remote server this script create an empty echo. The variable name and name2 is not filled. Why?
推荐答案
更改这一行:
commandResource="${basedir}/to_run.sh"
到
command="${basedir}/to_run.sh"
结果如下:
[sshexec] Authentication succeeded (password).
[sshexec] cmd : /data/tmp/anttest/to_run.sh
[sshexec] Name:
[sshexec] Pink Panther
[sshexec] Name with export:
[sshexec] Panther Pink
commandResource
获取包含命令列表的资源文件,并使用 bash -c $LINE
单独执行每一行,因此定义的任何变量仅在同一行上有效.command
在同一个 shell 中执行整个脚本.
commandResource
takes a resource file with a list of commands and executes each line individually with bash -c $LINE
so any variables defined are only valid on the same line. command
executes the whole script in the same shell.
这篇关于来自ant的sshexec中bash脚本中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!