命令变量的奇怪行为 | awk 中的 getline

命令变量的奇怪行为 | awk 中的 getline

问题:命令变量值li12[q]不能被引用为它自己的实际内容。

症状:一旦我真正引用了命令变量值li12[q],就会失败并出现 fatal: 用于|重定向的表达式具有空字符串值。

笔记:

  1. 但是命令变量确实有自己的值,这和前面的k5变量li12[q]一样!getline
  2. li12[q]由于命令变量名本身可以被引用,因此相应的语法和结构应该没有问题。
# 
# 
# lspci -v|grep -Ei "fib|scsi|raid|sas|net|ide|ata"|grep -i ^[0-9]|awk  '{kin1[NR]=$1; prt[NR]="";for(ik=2;ik<=NF;ik++){prt[NR]=prt[NR]$ik" "}}END\
{while("ls -l /sys/class|grep -Ei \"scsi|sas|fc|net|ide|ata\"|grep -Eiv \"disk|tape|device|gen\""|getline){li11[++h1]=$9}; for(i=1;i<=NR;i++)\
{for(s=1;s<=h1;s++){h2=0;d3="(find /sys/class/"li11[s]" -type l -exec bash -c '\''echo s `ls -l {}`'\'' \\;|grep -i devices|grep -Ei \""kin1[i]"(/|$)\")\
2>/dev/null"; while(d3|getline){k5=li12[++h2]=$10$11$12; k6=h2}; for(q=1;q<=h2;q++){if(li12[q] ~/[a-zA-Z]/){ while("echo -e \"li12[q]\""|getline k7); k2=k7; k3=$1;\
if( q == h2 ){s=h1+1}; if(q>1){kin1[i]=prt[i]=""};printf " %-16s%-6s%-16s%-16s%-100s%-16s\n",kin1[i],k6,k5,k3,prt[i],k2}}}}}'
 03:00.0         1     /sys/class/scsi_host/host0/device->../../../devices/pci0000:00/0000:00:02.2/0000:03:00.0/host0s RAID bus controller: Hewlett-Packard Company Smart Array Gen8 Controllers (rev 01)                  li12[q]         
 04:00.0         1     /sys/class/net/eth0/device->../../../devices/pci0000:00/0000:00:02.0/0000:04:00.0s Ethernet controller: Emulex Corporation OneConnect 10Gb NIC (be3) (rev 01)                          li12[q]         
 04:00.1         1     /sys/class/net/eth1/device->../../../devices/pci0000:00/0000:00:02.0/0000:04:00.1s Ethernet controller: Emulex Corporation OneConnect 10Gb NIC (be3) (rev 01)                          li12[q]         
 04:00.2         1     /sys/class/iscsi_host/host3/device->../../../devices/pci0000:00/0000:00:02.0/0000:04:00.2/host3s Mass storage controller: Emulex Corporation OneConnect 10Gb iSCSI Initiator (be3) (rev 01)          li12[q]         
 04:00.3         1     /sys/class/iscsi_host/host4/device->../../../devices/pci0000:00/0000:00:02.0/0000:04:00.3/host4s Mass storage controller: Emulex Corporation OneConnect 10Gb iSCSI Initiator (be3) (rev 01)          li12[q]         
 05:00.0         1     /sys/class/fc_host/host1/device->../../../devices/pci0000:00/0000:00:03.0/0000:05:00.0/host1s Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)             li12[q]         
 05:00.1         1     /sys/class/fc_host/host2/device->../../../devices/pci0000:00/0000:00:03.0/0000:05:00.1/host2s Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)             li12[q]         
# 
# 
# 
# lspci -v|grep -Ei "fib|scsi|raid|sas|net|ide|ata"|grep -i ^[0-9]|awk  '{kin1[NR]=$1; prt[NR]="";for(ik=2;ik<=NF;ik++){prt[NR]=prt[NR]$ik" "}}END\         
{while("ls -l /sys/class|grep -Ei \"scsi|sas|fc|net|ide|ata\"|grep -Eiv \"disk|tape|device|gen\""|getline){li11[++h1]=$9}; for(i=1;i<=NR;i++)\
{for(s=1;s<=h1;s++){h2=0;d3="(find /sys/class/"li11[s]" -type l -exec bash -c '\''echo s `ls -l {}`'\'' \\;|grep -i devices|grep -Ei \""kin1[i]"(/|$)\")\
2>/dev/null"; while(d3|getline){k5=li12[++h2]=$10$11$12; k6=h2}; for(q=1;q<=h2;q++){if(li12[q] ~/[a-zA-Z]/){ while("echo -e "li12[q]""|getline k7); k2=k7; k3=$1; \
if( q == h2 ){s=h1+1}; if(q>1){kin1[i]=prt[i]=""};printf " %-16s%-6s%-16s%-16s%-100s%-16s\n",kin1[i],k6,k5,k3,prt[i],k2}}}}}'
awk: cmd. line:3: (FILENAME=- FNR=7) fatal: expression for `|' redirection has null string value
# 
#

编辑这里是上面的代码,格式更加清晰,由 @glennjackman 提供:

lspci -v \
| grep -Ei "fib|scsi|raid|sas|net|ide|ata" \
| grep -i ^[0-9] \
| awk  '
    {
        kin1[NR] = $1
        prt[NR] = ""
        for (ik = 2; ik <= NF; ik++) {prt[NR] = prt[NR] $ik " "}
    }
    END {
        while ("ls -l /sys/class | grep -Ei \"scsi|sas|fc|net|ide|ata\" | grep -Eiv \"disk|tape|device|gen\"" | getline) {
            li11[++h1] = $9
        }
        for (i = 1; i <= NR;i++) {
            for (s = 1; s <= h1;s++) {
                h2 = 0
                d3 = "(find /sys/class/"li11[s]" -type l -exec bash -c '\''echo s `ls -l {}`'\'' \\; | grep -i devices | grep -Ei \""kin1[i]"(/|$)\") 2>/dev/null"
                while (d3 | getline){
                    k5 = li12[++h2] = $10 $11 $12
                    k6 = h2
                }
                for (q = 1; q <= h2;q++){
                    if (li12[q] ~/[a-zA-Z]/) {
                        while ("echo -e "li12[q]"" | getline k7)
                            k2 = k7
                        k3 = $1
                        if (q == h2) {s = h1+1}
                        if (q > 1) {kin1[i] = prt[i] = ""}
                        printf " %-16s%-6s%-16s%-16s%-100s%-16s\n", kin1[i], k6, k5, k3, prt[i], k2
                    }
                }
            }
        }
    }
'

现在,在 RHEL 6.4 系统上使用上述相同的 awk 脚本会出现不同的失败结果/进度。
注意:关于同一空字符串值的所有先前失败结果都仅在 RHEL 5.8 系统上运行。
sh: ../../../devices/pci0000:00/0000:00:02.2/0000:03:00.0/host0: No such file or directory
 03:00.0         1     /sys/class/scsi_host/host0/device->../../../devices/pci0000:00/0000:00:02.2/0000:03:00.0/host0s               RAID bus controller: Hewlett-Packard Company Smart Array Gen8 Controllers (rev 01)                                  
sh: ../../../devices/pci0000:00/0000:00:02.0/0000:04:00.0: No such file or directory
 04:00.0         1     /sys/class/net/eth0/device->../../../devices/pci0000:00/0000:00:02.0/0000:04:00.0s               Ethernet controller: Emulex Corporation OneConnect 10Gb NIC (be3) (rev 01)                                          
sh: ../../../devices/pci0000:00/0000:00:02.0/0000:04:00.1: No such file or directory
 04:00.1         1     /sys/class/net/eth1/device->../../../devices/pci0000:00/0000:00:02.0/0000:04:00.1s               Ethernet controller: Emulex Corporation OneConnect 10Gb NIC (be3) (rev 01)                                          
sh: ../../../devices/pci0000:00/0000:00:02.0/0000:04:00.2/host3: No such file or directory
 04:00.2         1     /sys/class/iscsi_host/host3/device->../../../devices/pci0000:00/0000:00:02.0/0000:04:00.2/host3s               Mass storage controller: Emulex Corporation OneConnect 10Gb iSCSI Initiator (be3) (rev 01)                          
sh: ../../../devices/pci0000:00/0000:00:02.0/0000:04:00.3/host4: No such file or directory
 04:00.3         1     /sys/class/iscsi_host/host4/device->../../../devices/pci0000:00/0000:00:02.0/0000:04:00.3/host4s               Mass storage controller: Emulex Corporation OneConnect 10Gb iSCSI Initiator (be3) (rev 01)                          
sh: ../../../devices/pci0000:00/0000:00:03.0/0000:05:00.0/host1: No such file or directory
 05:00.0         1     /sys/class/fc_host/host1/device->../../../devices/pci0000:00/0000:00:03.0/0000:05:00.0/host1s               Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)                             
sh: ../../../devices/pci0000:00/0000:00:03.0/0000:05:00.1/host2: No such file or directory
 05:00.1         1     /sys/class/fc_host/host2/device->../../../devices/pci0000:00/0000:00:03.0/0000:05:00.1/host2s               Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)  

答案1

空白是免费的,你应该使用一些。

这就是你在做的事吗?

lspci -v \
| grep -Ei "fib|scsi|raid|sas|net|ide|ata" \
| grep -i ^[0-9] \
| awk  '
    {
        kin1[NR] = $1
        prt[NR] = ""
        for (ik = 2; ik <= NF; ik++) {prt[NR] = prt[NR] $ik " "}
    }
    END {
        while ("ls -l /sys/class | grep -Ei \"scsi|sas|fc|net|ide|ata\" | grep -Eiv \"disk|tape|device|gen\"" | getline) {
            li11[++h1] = $9
        }
        for (i = 1; i <= NR;i++) {
            for (s = 1; s <= h1;s++) {
                h2 = 0
                d3 = "(find /sys/class/"li11[s]" -type l -exec bash -c '\''echo s `ls -l {}`'\'' \\; | grep -i devices | grep -Ei \""kin1[i]"(/|$)\") 2>/dev/null"
                while (d3 | getline){
                    k5 = li12[++h2] = $10 $11 $12
                    k6 = h2
                }
                for (q = 1; q <= h2;q++){
                    if (li12[q] ~/[a-zA-Z]/) {
                        while ("echo -e "li12[q]"" | getline k7)
                            k2 = k7
                        k3 = $1
                        if (q == h2) {s = h1+1}
                        if (q > 1) {kin1[i] = prt[i] = ""}
                        printf " %-16s%-6s%-16s%-16s%-100s%-16s\n", kin1[i], k6, k5, k3, prt[i], k2
                    }
                }
            }
        }
    }
'

答案2

@rowboat 非常感谢您的精彩回答,我已经测试过这个答案,并且它已经成功运行!

只需替换为以下语句:
来自:

while ("echo -e "li12[q]"" | getline k7)

到 :

while (("echo -e "li12[q]"") | getline k7)

相关内容