检索从ssh运行到变量的bash命令的结果

时间:2017-05-23 18:59:11

标签: linux bash ssh

当我在dsired机器上运行它时,以下命令运行良好:

app_name=*some_proccess_name*
pid=`pgrep $app_name | tail -n 1`

但是当我按照以下方式运行它时,从使用ssh的远程PC开始运行:

pid=$(ssh $USER_NAME@$HOST_NAME "echo `pgrep $app_name | tail -n 1`")

之后pid的值只是空白。我不确定是什么错(只是为了澄清我已经尝试了几个在目标pc上运行的进程名称 - 这不是问题)。

P.S当我在没有回声的情况下运行命令时,就像那样,我只是卡在远程电脑里面而且必须使用exit才能解开并返回我的本地电脑:

pid=$(ssh tester@mir1 "`pgrep indicator-apple | tail -n 1`")

1 个答案:

答案 0 :(得分:3)

少即是多

pid=$(ssh tester@mir1 pgrep indicator-apple \| tail -n 1)