Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.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
Bash ssh中的Escape awk命令_Bash_Ssh_Awk - Fatal编程技术网

Bash ssh中的Escape awk命令

Bash ssh中的Escape awk命令,bash,ssh,awk,Bash,Ssh,Awk,我有以下命令: su user1 -c "ssh user1@192.168.1.2 awk '$5\==1{print\$3}' filename.log" | uniq -c 运行该命令会出现以下错误: awk: ==1{print awk: ^ syntax error awk: cmd. line:1: ==1{print awk: cmd. line:1: ^ unexpected newline or end of string 我试过几种逃跑的方法,但都没有成

我有以下命令:

su user1 -c "ssh user1@192.168.1.2 awk '$5\==1{print\$3}' filename.log" | uniq -c
运行该命令会出现以下错误:

awk: ==1{print
awk: ^ syntax error
awk: cmd. line:1: ==1{print
awk: cmd. line:1:          ^ unexpected newline or end of string
我试过几种逃跑的方法,但都没有成功。任何建议都将不胜感激。谢谢。

我愿意:

su user1 -c "ssh user1@192.168.1.2 \"awk '\\\$5==1{print \\\$3}' filename.log\"" | uniq -c
                                   ^^                                    ^^
基本上,首先,如果您是
user1
,您需要想象如何执行该命令:

ssh user1@192.168.1.2 "awk '\\\$5==1{print \\\$3}' filename.log"

然后将其引入到su user1…转义引号中。

转义正确的方法相当棘手。你可以这样写:

su user1 -c 'ssh user1@192.168.1.2 awk \"\\\$5 == 1 {print \\\$3}\" filename.log' | uniq -c
su user1 -c "ssh user1@192.168.1.2 awk \'\\\$5 == 1 {print \\\$3}\' filename.log" | uniq -c
或者像这样:

su user1 -c 'ssh user1@192.168.1.2 awk \"\\\$5 == 1 {print \\\$3}\" filename.log' | uniq -c
su user1 -c "ssh user1@192.168.1.2 awk \'\\\$5 == 1 {print \\\$3}\' filename.log" | uniq -c

也就是说,您需要在带引号的字符串中转义引号和
$
符号。由于
$
符号总是需要转义,因此在这种情况下需要加倍转义,这就是为什么
\\\\

不需要对awk内部字段说明符使用
\\\\$
的原因。您完全正确,@EtanReisner,我已经测试过,但没有注意到它是错误的。刚刚更新,谢谢!很高兴读到这一点,@ace!既然你是新来的,如果你的问题已经解决了,请别忘了把答案标为“接受”。您可以点击答案旁边的复选标记,将答案从空心切换为绿色。看看你是否有任何疑问!