如何在命令(ssh)中正确编写命令?
how to correctly write command inside commands (ssh)?
这是我的命令
只是用returns一个数字
的表达式杀死
kill $(ps -ef | grep '[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector' | cut -f8 -d' ') &> /dev/null
这里是我平时用的ssh
bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.XXX "cd NightTest"'
我试着把两者结合起来
bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 "kill $(ps -ef | grep '[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector' | cut -f8 -d' ') &> /dev/null"'
它不起作用,我的猜测是它与 '' 混淆了。
尝试过的选项
转义 kill 命令中的大部分 ' :
bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 "kill $(ps -ef | grep \'[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector\' | cut -f8 -d\' ') &> /dev/null"'
也不行,我试了很多其他的都不行。
有什么想法吗?
已添加注释
我的系统不支持 pkill 命令
如果您使用 pkill
终止所需的进程会更容易。一次搜索一次查杀
pkill -f 'matchbox-panel --titlebar --start-applets showdesktop,windowselector'
然后您可以将其放入 SSH 调用中:
bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 pkill -f "matchbox-panel --titlebar --start-applets showdesktop,windowselector"'
bash -c
的目的是什么?如果能去掉那个就更简单了
timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 pkill -f 'matchbox-panel --titlebar --start-applets showdesktop,windowselector'
删除 bash -c
符号。假设您不重写命令以使用 pkill
,那么您需要这样的东西:
timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 \
'kill $(ps -ef | grep "[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector" | cut -f8 -d" ") &> /dev/null'
请注意,我在要执行的命令中使用了双引号。幸运的是,命令中没有使用单引号或双引号的问题。
带有'embedded single quotes' 的版本不起作用,因为您不能在单引号字符串中嵌入单引号。如果必须,您可以编写 '…'\''…'
以在两个省略号之间获得单引号。第一个 '
终止当前的单引号字符串(即使前面的字符是反斜杠;单引号字符串中没有转义); \'
生成单引号;中间组中的第三个 '
再次恢复单引号字符串。
因此,在您的尝试中:
bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 "kill $(ps -ef | grep \'[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector\' | cut -f8 -d\' ') &> /dev/null"'
Bash 命令看到:
timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 "kill $(ps -ef | grep \[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector\ | cut -f8 -d\ ) &> /dev/null"
这意味着 grep
在你想要一个的地方得到多个参数,等等。它完全打破了意义。
嵌套引用约定很难——真的很难。
这是我的命令
只是用returns一个数字
的表达式杀死kill $(ps -ef | grep '[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector' | cut -f8 -d' ') &> /dev/null
这里是我平时用的ssh
bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.XXX "cd NightTest"'
我试着把两者结合起来
bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 "kill $(ps -ef | grep '[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector' | cut -f8 -d' ') &> /dev/null"'
它不起作用,我的猜测是它与 '' 混淆了。
尝试过的选项
转义 kill 命令中的大部分 ' :
bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 "kill $(ps -ef | grep \'[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector\' | cut -f8 -d\' ') &> /dev/null"'
也不行,我试了很多其他的都不行。
有什么想法吗?
已添加注释
我的系统不支持 pkill 命令
如果您使用 pkill
终止所需的进程会更容易。一次搜索一次查杀
pkill -f 'matchbox-panel --titlebar --start-applets showdesktop,windowselector'
然后您可以将其放入 SSH 调用中:
bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 pkill -f "matchbox-panel --titlebar --start-applets showdesktop,windowselector"'
bash -c
的目的是什么?如果能去掉那个就更简单了
timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 pkill -f 'matchbox-panel --titlebar --start-applets showdesktop,windowselector'
删除 bash -c
符号。假设您不重写命令以使用 pkill
,那么您需要这样的东西:
timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 \
'kill $(ps -ef | grep "[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector" | cut -f8 -d" ") &> /dev/null'
请注意,我在要执行的命令中使用了双引号。幸运的是,命令中没有使用单引号或双引号的问题。
带有'embedded single quotes' 的版本不起作用,因为您不能在单引号字符串中嵌入单引号。如果必须,您可以编写 '…'\''…'
以在两个省略号之间获得单引号。第一个 '
终止当前的单引号字符串(即使前面的字符是反斜杠;单引号字符串中没有转义); \'
生成单引号;中间组中的第三个 '
再次恢复单引号字符串。
因此,在您的尝试中:
bash -c 'timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 "kill $(ps -ef | grep \'[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector\' | cut -f8 -d\' ') &> /dev/null"'
Bash 命令看到:
timeout 120s ssh -o StrictHostKeyChecking=no root@192.168.155.148 "kill $(ps -ef | grep \[m]atchbox-panel --titlebar --start-applets showdesktop,windowselector\ | cut -f8 -d\ ) &> /dev/null"
这意味着 grep
在你想要一个的地方得到多个参数,等等。它完全打破了意义。
嵌套引用约定很难——真的很难。