使用管道,引号等命令的Bash变量 [英] Bash variable from command with pipes, quotes, etc

查看:46
本文介绍了使用管道,引号等命令的Bash变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于获取主机名的命令.localdomain:

I have a command I'm using to get the hostname.localdomain:

dig axfr @dc1.localdomain.com localdomain.com | grep -i Lawler | awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }'

这很好地返回如下结果:

This nicely returns a result like:

michael.lawler.localdomain.com

michael.lawler.localdomain.com

我想进一步将该结果用作Bash脚本中的变量.

I'd like to further use that result as a variable in a Bash script.

似乎无法通过第一个管道.

It seems I'm having trouble getting past the first pipe.

如果我 VAR ="dig axfr @ dc1.localdomain.com localdomain.com | grep -i Lawler | awk'{getline; $ 1 = substr($ 1,1,length($ 1)-1);打印$ 1;退出}'"

...我取回了整个区域传输.我还尝试了许多小的更改,在 dig 命令之前添加了 $ ,没有引号,但似乎没有任何效果.我该如何解决?

...I get back the entire zone transfer. I've also tried many minor changes, adding $ before the dig command, without quotes, but nothing seems to work. How can I fix this?

推荐答案

VAR=$( dig axfr @dc1.localdomain.com localdomain.com |
     grep -i Lawler |
     awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }' )

这篇关于使用管道,引号等命令的Bash变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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