Variables in bash script in sshexec from ant

故事扮演 提交于 2019-12-24 17:09:03

问题


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>

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

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 ...

We can see that on the remote server this script create an empty echo. The variable name and name2 is not filled. Why?


回答1:


Changing this line:

commandResource="${basedir}/to_run.sh" 

to

command="${basedir}/to_run.sh" 

results in the following:

[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 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.



来源:https://stackoverflow.com/questions/14063194/variables-in-bash-script-in-sshexec-from-ant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!