SSH运行sh脚本,该脚本在本地但不能远程运行

时间:2019-12-17 14:04:03

标签: bash ssh

我正在尝试运行一个脚本,该脚本将被其他软件调用以运行一些参数以获取目标值。

脚本run.sh如下:

#!/bin/bash
set -e 
ssh id@somehost '
/path/to/folder/solver arg1 arg2 arg3
res=$(</path/to/folder/res_data.txt)
echo "Final Result:"
echo "1 $res"
'

运行此文件将得到以下结果:

$ sh run.sh
OpenNN Exception: NeuralNetwork class.
void load(const std::string&) method.
Cannot load XML file ../data/neural_network.xml.

Final Result:
1 -285361 3.22136
Connection to somehost closed.

上面的最终结果来自先前的输出

如果我运行类似的脚本但没有ssh

set -e 
/path/to/folder/solver arg1 arg2 arg3
res=$(</path/to/folder/res_data.txt)
echo "Final Result:"
echo "1 $res"

结果

$ sh run.sh 7 26 100
Final Result:
1 -285361 3.22136
$ sh run.sh 7 26 150
Final Result:
1 -421429 5.16397

有人知道如何解决此问题吗?

1 个答案:

答案 0 :(得分:0)

基于上面的评论,我得到了错误的解决方案,

#!/bin/bash
set -e 
ssh id@somehost '
cd /path/to/folder/
./solver '$1' '$2' '$3'
res=$(<./res_data.txt)
echo "Final Result:"
echo "1 $res"
'

我很简单,只需添加cd /path/to/folder/并从似乎可以使用的文件夹中运行脚本,此外,我还解决了参数问题而不是./solver $1 $2 $3的问题,因为有了{{1} }作为传递输入参数以在求解器上运行的方式。

以下输出来自上面的更正文件

./solver '$arg1' '$arg2' '$arg3'