已通过ssh嵌套引用运行shell命令 [英] Running shell command that has nested quotes via ssh

查看:197
本文介绍了已通过ssh嵌套引用运行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个下面的shell命令:

I have this following shell command:

ssh user@host "df | grep /dev/ | \
awk 'BEGIN{print "DISK", "%USAGE", "STATUS"} {split($5, a, "%"); \
var="GREEN"; print $1, $5, var}' | column -t"

我需要通过ssh运行,但我因嵌套双和单引号的presence得到语法错误。

I need to run this over ssh but I get syntax error due to the presence of nested double and single quotes.

我试开始前和报价结束转义字符,但它并没有解决问题。

I tried the escape characters for before the beginning and ending of the quotes but it did not solve the problem.

但是,在运行这会给下面的输出本地系统:

However, on local system running this will give the following output:

$ df | grep /dev/ | \
awk 'BEGIN{print "DISK", "%USAGE", "STATUS"} {split($5, a, "%"); \
var="GREEN"; print $1, $5, var}' | column -t
DISK       %USAGE  STATUS
/dev/sda1  95%     GREEN

感谢

推荐答案

一个引号定界符允许省略外报价:

A quoted heredoc allows you to omit the outer quotes:

ssh user@host <<'END'
df | grep /dev/ | awk 'BEGIN{print "DISK", "%USAGE", "STATUS"} {split($5, a, "%"); var="GREEN"; print $1, $5, var}' | column -t
END

这篇关于已通过ssh嵌套引用运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆