从我在这个网站和其他在线资源中找到的内容来看,它说在变量周围使用双引号。但这对我不起作用。
例如:
我的变量是$GTUNNELNATNETIP
[johnwayne:630 ~] GTUNNELNATNETIP=`cat iptables.out |awk '{print $NF}'|cut -d "/" -f1`
[johnwayne:631 ~] echo $GTUNNELNATNETIP
10.19.192.0
[johnwayne:632 ~]
我正在搜索的文件是iptables2.out
不带引号搜索会找到值但并非没有错误
[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 ~]
它不适用于双引号或单引号。或我尝试过的任何其他格式
[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 ~]
当我传递实际值而没有错误时它可以工作
[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 ~]
那么为什么当文件和目录确实存在时会发生 grep 错误呢?以及如何正确传递变量。我正在尝试计算 grep 返回的行数并在iptables
不显示文件名的情况下获取确切的命令。
像这样:
[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 ~]
与此相反:
[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 ~]