变量bash脚本从蚂蚁sshexec [英] 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
我们可以看到,所有工作正常。但是,如果我从蚂蚁启动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 ...
我们可以看到,在远程服务器上运行此脚本创建一个空的回声。变量的名称和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 $行
单独执行每行这样定义的任何变量仅在同一行有效。 命令
执行中相同的外壳,整个剧本。
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.
这篇关于变量bash脚本从蚂蚁sshexec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!