我在 ANT 中定义了一个宏,用于检查远程 linux 机器上是否存在目录:
<macrodef name="checkIfDirExists">
<attribute name="host" />
<attribute name="username" />
<attribute name="password" />
<attribute name="dir" />
<sequential>
<runcommand executable="[ -d @{dir} ]" host="@{host}" username="@{username}" password="@{password}"/>
</sequential>
</macrodef>
runcommand
只是 sshexec
任务的包装宏,用于验证一些额外的东西,但基本上它只是一个 sshexec
。
现在,如果我运行它,它的工作方式是,如果目录存在,则继续构建,但如果目录不存在,则构建失败,因为 [ -d @{dir} ]
返回值为 1。
我希望能够检查返回值,以便将其放入 conditional
标记中,例如,如果目录存在,则跳过,如果不使用 mkdir 创建它。
这可能吗?
最佳答案
这完全是暗箱操作,不知道 Ant 会不会让你这么做。但是,如果它在远程主机上调用 bash,它应该可以工作。
<macrodef name="checkIfDirExists">
<attribute name="host" />
<attribute name="username" />
<attribute name="password" />
<attribute name="dir" />
<sequential>
<runcommand executable="[ -d @{dir} ] || mkdir @{dir}" host="@{host}" username="@{username}" password="@{password}"/>
</sequential>
</macrodef>
这样如果目录存在就会短路并返回成功。如果它不存在,它将调用 mkdir。如果 mkdir 失败,则 ant 也会失败。
关于linux - 如何从 ssh 任务 ant 中获取返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6215418/