我正在搜索基于 Ips amd 进程的打开文件列表。我用下面的命令
for i in `sudo netstat -tulpna|awk '{print $5}'|grep -E "[0-9]{3}"|grep -v "^192\|10"|cut -f1 -d ":"`;do sudo netstat -tulpna|awk '/XXX.XXX.XXX.XXX/ {print $7}';done
26181/java
26181/java
26181/java
26181/java
26181/java
26181/java
26181/java
26181/java
26181/java
26181/java
26181/java
26181/java
如果我添加单引号并且在 awk 条件下 /XXX.XXX.XXX.XXX/ 我给出静态值,它将打印第 7 列,则工作正常
但是如果我使用双引号和 /$i/ 变量
for i in `sudo netstat -tulpna|awk '{print $5}'|grep -E "[0-9]{3}"|grep -v "^192\|10"|cut -f1 -d ":"`;do sudo netstat -tulpna|awk "/$i/ {print $7}";done
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:62778 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:35708 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:40920 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:40918 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:31211 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:35708 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:40920 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:40918 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:31211 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:35708 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:40920 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:40918 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:31211 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:35708 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:40920 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:40918 ESTABLISHED 26181/java
tcp 0 0 XXX.XXX.XXX.XXX:443 XXX.XXX.XXX.XXX:31211 ESTABLISHED 26181/java
当它应该只打印第 7 列时。我在这里缺少什么
你的
awk
脚本:在这里,
$7
被 shell 扩展并且很可能是空的,导致命令相反,您可以从 shell中转义
$
in :$7
这在像这样的简短
awk
脚本中是可以的。在一个更复杂的脚本中,可能应该传递$i
到awk
using-v variable="$i"
和 usevariable
tomatch()
against the lines(或 use~
as α˓sнιη shows)。您应该首先将
$i
as 变量传递for ...;do ... ;done
给一个awk
变量,然后将其值作为如下模式进行匹配:有关更多信息,请参阅将shell 变量作为 /pattern/ 传递给 awk。