Linux 如何在bash中使用变量grep
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18642246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to grep using a variable in bash
提问by user2751569
From what I've found on this website and other online sources it says to use double quotes around the variable. But that's not working for me.
从我在本网站和其他在线资源上找到的内容来看,它说要在变量周围使用双引号。但这对我不起作用。
For example:
例如:
My variable is $GTUNNELNATNETIP
我的变量是 $GTUNNELNATNETIP
[johnwayne:630 ~] GTUNNELNATNETIP=`cat iptables.out |awk '{print $NF}'|cut -d "/" -f1`
[johnwayne:631 ~] echo $GTUNNELNATNETIP
10.19.192.0
[johnwayne:632 ~]
and the file I'm searching in is iptables2.out
我正在搜索的文件是 iptables2.out
searching without quotes finds the value but not without error
不带引号的搜索找到了值但不是没有错误
[johnwayne:632 ~] grep $GTUNNELNATNETIP iptables2.out
grep: : No such file or directory
iptables2.out:-A POSTROUTING -s 10.19.192.0/22 -o eth1 -j SNAT --to-source 64.134.58.175
[johnwayne:633 ~]
It doesn't work with double or single quotes. Or any other format I've tried
它不适用于双引号或单引号。或者我尝试过的任何其他格式
[johnwayne:633 ~] grep "$GTUNNELNATNETIP" iptables2.out
grep: Trailing backslash
[johnwayne:634 ~] grep '$GTUNNELNATNETIP' iptables2.out
[johnwayne:635 ~] grep `echo $GTUNNELNATNETIP` iptables2.out
grep: : No such file or directory
iptables2.out:-A POSTROUTING -s 10.19.192.0/22 -o eth1 -j SNAT --to-source 64.134.58.175
[johnwayne:636 ~] /bin/grep $GTUNNELNATNETIP iptables2.out
/bin/grep: : No such file or directory
iptables2.out:-A POSTROUTING -s 10.19.192.0/22 -o eth1 -j SNAT --to-source 64.134.58.175
[johnwayne:637 ~]
It works when I pass the actual value without an error
当我传递实际值而没有错误时它起作用
[johnwayne:637 ~] grep 10.19.192.0 iptables2.out
-A POSTROUTING -s 10.19.192.0/22 -o eth1 -j SNAT --to-source 64.134.58.175
[johnwayne:638 ~]
So why does that grep error occur when the file and directory do exist? And how can I pass the variable without error. I'm trying to count how many lines the grep returns and get the exact iptables
command without it showing the file name.
那么当文件和目录确实存在时,为什么会出现 grep 错误呢?以及如何正确传递变量。我正在尝试计算 grep 返回的行数,并在iptables
不显示文件名的情况下获取确切的命令。
like this:
像这样:
[johnwayne:637 ~] /bin/grep 10.19.192.0 iptables2.out
-A POSTROUTING -s 10.19.192.0/22 -o eth1 -j SNAT --to-source 64.134.58.175
[johnwayne:638 ~]
as opposed to this:
与此相反:
[johnwayne:620 ~] grep $GTUNNELNATNETIP iptables2.out
grep: : No such file or directory
iptables2.out:-A POSTROUTING -s 10.19.192.0/22 -o eth1 -j SNAT --to-source 64.134.58.175
[johnwayne:621 ~]
回答by Aleks-Daniel Jakimenko-A.
You should quote it
你应该引用它
grep "$GTUNNELNATNETIP" iptables2.out
Read more about quoting variables in bash
This must work.
阅读有关在 bash 中引用变量的更多信息
这必须有效。
I cannot reproduce your trailing backslash
error, but most probably you have some garbage in $GTUNNELNATNETIP
variable
我无法重现您的trailing backslash
错误,但很可能您的$GTUNNELNATNETIP
变量中有一些垃圾
回答by tripleee
The problem is that echo $GTUNNELNATNETIP
without quotes causes the shell to remove "insignificant" shell special characters. Use echo "$GTUNNELNATNETIP"
and you will find that it does indeed have a trailing backslash, and possibly other problems.
问题是echo $GTUNNELNATNETIP
没有引号会导致外壳删除“无关紧要的”外壳特殊字符。使用echo "$GTUNNELNATNETIP"
,你会发现它确实有一个尾随反斜杠,可能还有其他问题。
(Better yet, learn to use printf
, set
, or env
to examine your variables.)
(更重要的是,学会使用printf
,set
或env
检查你的变量。)
The assignment uses cat
uselesslyand could use some additional refactoring anyway. Try this?
分配使用cat
无益,反正可以使用一些额外的重构。尝试这个?
GTUNNELNATNETIP=$(awk '{split(/\//,g,$NF); print g[1] }' iptables.out)
... but this doesn't attempt yet to fix the problem, because we don't know exactly how the value is flawed. Maybe just add gsub(/\\/,"",g[1]);
before printing?
...但这并没有试图解决问题,因为我们不知道该值究竟是如何存在缺陷的。也许只是gsub(/\\/,"",g[1]);
在打印前添加?