Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ant中sshexec中bash脚本中的变量_Ant - Fatal编程技术网

ant中sshexec中bash脚本中的变量

ant中sshexec中bash脚本中的变量,ant,Ant,我在sshexec任务中遇到变量问题。 我的build.xml ant脚本如下所示: <?xml version="1.0" encoding="UTF-8"?> <project name="sshexecproject" basedir="." default="build"> <target name="build"> <sshexec host="192.168.2.106" username="roo

我在sshexec任务中遇到变量问题。 我的build.xml ant脚本如下所示:

<?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.sh 
Name: 
Pink Panther
Name with export: 
Panther Pink
...
[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 ...
我们可以看到一切都很好。但如果从ant启动build.xml脚本,则会得到以下输出:

$ ./to_run.sh 
Name: 
Pink Panther
Name with export: 
Panther Pink
...
[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 ...
我们可以看到,在远程服务器上,这个脚本创建了一个空回音。变量名和名称2未填充。为什么?

更改此行:

commandResource="${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
分别执行每一行,因此定义的任何变量仅在同一行上有效<代码>命令在同一个shell中执行整个脚本

感谢您的回复,我已经更新了我的问题,因为问题仍然没有解决。经过进一步研究,
commandResource
是一个命令文件,其中每一行都作为
bash-c$line
执行,因此每个命令都有一个单独的shell和上下文。相反,您需要的是
command
,它将立即执行整个脚本。如果我将commandResource更改为command,则会得到一个“未找到文件”错误。脚本必须首先在服务器上执行,然后我才能使用它?看起来命令是在服务器端执行的,所以脚本需要首先使用scp传输到服务器,对吗?没错,命令必须是服务器上的有效命令。