Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ssh-pkill-bash命令中正确地转义双引号?_Bash_Ssh_Escaping_Heredoc_Quoting - Fatal编程技术网

如何在ssh-pkill-bash命令中正确地转义双引号?

如何在ssh-pkill-bash命令中正确地转义双引号?,bash,ssh,escaping,heredoc,quoting,Bash,Ssh,Escaping,Heredoc,Quoting,一切都很完美 $ ssh root@123.123.123.123 123.123.123.123# pkill -f "stalled process name"; commands_to_restart; some_more_commands; many many lines of output demonstrating success 123.123.123.123# exit; 没有输出,什么也不做 $ ssh root@123.123.123.123 "pkill -f "\""s

一切都很完美

$ ssh root@123.123.123.123
123.123.123.123# pkill -f "stalled process name"; commands_to_restart; some_more_commands;
many many lines of output demonstrating success
123.123.123.123# exit;
没有输出,什么也不做

$ ssh root@123.123.123.123 "pkill -f "\""stalled process name"\"";"\
> "commands_to_restart; some_more_commands;";
所以。。。报价转义的两个阶段按预期进行

如何使用ssh/bash获得一层引号转义? 由于引用在两个层次上都能完美地工作,我觉得它与引用关系不大,而与
ssh
s处理终端的某些方面有关。然而,据我所知,这些命令只对标准输出和无输入执行简单而常规的IO。

试试:

$ ssh root@123.123.123.123 "echo "\""pkill -f "\"\\\"\""stalled process name"\"\\\"\""; "\
> "commands_to_restart; some_more_commands;"\"";";
pkill -f "stalled process name"; commands_to_restart; some_more_commands;
使用单引号中的命令,无需转义内部双引号

如果您喜欢只使用双引号,则内部双引号的单转义应该是令人满意的:

ssh root@123.123.123.123 'pkill -f "stalled process name"; commands_to_restart; some_more_commands'

您最好使用heredoc进行以下操作:

ssh root@123.123.123.123 "pkill -f \"stalled process name\"; commands_to_restart; some_more_commands"

sshroot@123.123.123.123bash-s本身与终端处理无关。需要理解的是,ssh将其所有参数连接到一个字符串中,并通过线路发送该字符串。因此,进入这些参数之间边界的语法引用被完全丢弃——它被本地shell在形成ssh参数列表时使用,但是ssh随后丢弃了这些信息:本地shell丢弃本地语法引号,远程shell将参数列表中传递的文字引号视为语法。这是一种设计错误,但它是一种设计错误,只能通过新的wire协议修复(在导线上传递多个字符串而不是一个字符串),因此它在将来被修复的可能性…是不太可能的。@CharlesDuffy事实证明引用是完美的!花了几个小时阅读了有关shell/ssh引用转义过程的所有内容,然后才意识到问题是pkill是对
sh-c'…pkill-f“其他进程”的自引用.“
进程是由ssh启动的。事实上,您需要以不同于本地命令的方式引用是不完美的,而且如果ssh没有被错误设计,我上面所说的将是不必要的。接受是因为这不会导致
bash-c pkill\040-f\040“stalled\040process\040name”“commands”to“restart”;一些“more”commands;
显示在服务器上的命令行参数上,并导致pkill终止其自己的shell环境,这是我遇到的实际问题。不接受,因为引用不是问题。这将导致ssh运行
bash-c pkill\040-f\040“stalled\040process\040name”;命令重新启动;一些更多的命令;
,因此pkill将杀死自己的shell环境并阻止执行
命令重新启动
ssh root@123.123.123.123 bash -s << 'EOF'
    pkill -f "stalled process name"
    commands_to_restart
    some_more_commands
EOF