通过 SSH 为 awk 命令转义大括号 [英] Escaping curly brace for Awk commands over SSH

查看:89
本文介绍了通过 SSH 为 awk 命令转义大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下命令,但出现错误:

I tried the following command and got an error:

$  ssh -q rmthost "bash -c 'cat /var/tmp/5|awk '''{print \$1}''' >/var/tmp/redirect 2>&1'"
Missing }

(注意:bash -c ' 是必需的,因为 rmthost 上帐户的登录 shell 是 csh)

(Note: the "bash -c ' is needed since the login shell for the account on rmthost is csh)

禁用大括号扩展不起作用:

disabling brace expansion doesn't work:

$ set +B

我也试过:

$  ssh -q rmthost "bash -c 'cat /var/tmp/5|awk ''''{'print \$1'}''''>/var/tmp/redirect 2>&1'"
awk: syntax error near line 1
awk: illegal statement near line 1

中间脚本文件可以,但我更喜欢单行,因为我有 10 多个这样的类似命令需要在不同的时间点运行.

intermediate script files will do but I prefer one-liners since I have 10+ such similar commands need to be run at different point in time.

在这种情况下有没有办法摆脱大括号?

Is there a way to escape the braces in such case?

谢谢.

推荐答案

我玩了一会儿......我认为你真正想做的是简化".我的本地 shell 是 bash,而我的遥控器是 tcsh...这是我想出的功能(根据您的文件名).

I played with this a little while... And I think what you really want to do is "simplify." My local shell is bash, and my remote is tcsh... here is what I came up with as functional (in accordance with your filenames).

ssh -q rmthost "awk '{ print \$1 }' /var/tmp/5"

Shell 转义非常棘手,您真的必须考虑为什么"要执行此类操作(例如,为了利用中间脚本文件).

Shell escaping is tricky enough that you really have to think about "why" you want to do something like this (in deference, say, to leveraging an intermediate script file).

也许使用 cut 更好?

Maybe even better is to use cut?

ssh -q rmthost 'cut -f1 /var/tmp/5'

如果绝对需要外壳封装和重定向...(注意&符号上的转义.)

If shell encapsulation and redirection is absolutely required... (Note the escape on ampersand.)

ssh -q rmthost "sh -c \"awk '{ print $1 }' /var/tmp/5\" \&> /var/tmp/redirect"

这篇关于通过 SSH 为 awk 命令转义大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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