'grep' 无法通过 ssh 识别

'grep' not recognized over ssh

我正在尝试像这样远程关闭我的应用程序:

ssh pi@192.168.0.227 "kill $(ps aux | grep '[M]yApp' | awk '{print }')"

失败提示:

grep : The term 'grep' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

如果我先通过 SSH 登录然后执行命令,它可以工作,但我需要它是单行的。我已经将 /etc/ssh/sshd_config 变量 PermitUserEnvironment 设置为 yes,并尝试使用 grep (/bin/grep) 的完整路径,甚至删除了管道周围的空格(这些都是答案类似于我的问题)但没有任何东西可以让我通过命令。我错过了什么?

字符串在传递给其他主机之前由您的本地 shell 扩展。由于它是双引号字符串,$() 中的命令会在您的本地主机上运行 将此类命令传递到远程主机的最简单方法是使用“引号” here document:

ssh pi@192.168.0.227 <<'EOF'
kill $(ps aux | grep '[M]yApp' | awk '{print }')"
EOF

类似:How have both local and remote variable inside an SSH command