![无法在 pssh 命令中接受用户输入命令](https://linux22.com/image/1163154/%E6%97%A0%E6%B3%95%E5%9C%A8%20pssh%20%E5%91%BD%E4%BB%A4%E4%B8%AD%E6%8E%A5%E5%8F%97%E7%94%A8%E6%88%B7%E8%BE%93%E5%85%A5%E5%91%BD%E4%BB%A4.png)
我正在尝试从用户输入中获取操作系统命令并导出并在函数中使用,如下所示
export command5=date
export command4=uname
其在以下命令中工作
pssh -h /tmp/cus6 -i "$command5;$command4"
[1] 04:17:06 [SUCCESS] Server1
Mon Jan 28 03:17:06 UTC 2019
Linux
[2] 04:17:06 [SUCCESS] Server2
Mon Jan 28 03:17:06 UTC 2019
Linux
但在尝试将命令放在一行时不起作用
pssh -h /tmp/cus6 -i 'echo $(echo ), $(command5), $(command4)'
[1] 04:26:45 [SUCCESS] Server1
, ,
Stderr: bash: command5: command not found
bash: command4: command not found
[2] 04:26:45 [SUCCESS] Server2
, ,
Stderr: bash: command5: command not found
bash: command4: command not found
但同样的方法可以保留命令而不是命令 5 和命令 4
pssh -h /tmp/cus6 -i 'echo $(echo ), $(date), $(uname)'
[1] 04:30:01 [SUCCESS] Server1
, Mon Jan 28 03:30:01 UTC 2019, Linux
[2] 04:30:01 [SUCCESS] Server2
, Mon Jan 28 03:30:01 UTC 2019, Linux
答案1
为了当地的要扩展的变量,您需要将外面的单引号替换为双引号。
然后,为了将命令替换传递给偏僻的shell,您将需要引用(或转义)它。
前任。
$ cmd='uname -r'
$ parallel-ssh -h hostsfile -i "echo \$($cmd)"
[1] 22:38:52 [SUCCESS] vm
4.4.0-141-generic
[2] 22:38:52 [SUCCESS] localhost
4.15.0-43-generic