gpt4 book ai didi

linux - 使用 shell 脚本在同一行中打印两个变量输出

转载 作者:太空宇宙 更新时间:2023-11-04 05:56:18 25 4
gpt4 key购买 nike

这是我的脚本

fdisk -l > /tmp/fdisk.txt
DISK_NAME=$(cat /tmp/fdisk.txt | grep -w "Disk /dev" | awk '{print $2}' | cut -d ':' -f1)
echo disk name are : $DISK_NAME
DISK_SIZE=$(cat /tmp/fdisk.txt | grep -w Disk |grep -v "identifier" | awk '{print $3}' )

HEAD=$(cat /tmp/fdisk.txt | grep "heads" | awk '{print $1}')
SECTOR=$(cat /tmp/fdisk.txt | grep -w "sectors/track" | awk '{print $3}')
CYLINDER=$(cat /tmp/fdisk.txt | grep -w "cylinders" | awk '{print $5}')
TOTAL=$( cat /tmp/fdisk.txt | grep -w "total" | awk '{print $8}')
echo disk size are : $DISK_SIZE
echo Head are : $HEAD
echo Sector are : $SECTOR
echo Cylinder are : $CYLINDER
echo Total are : $TOTAL

输出是:

Disk /dev/mapper/openstackvm00--vg-root doesn't contain a valid partition table
Disk /dev/mapper/openstackvm00--vg-swap_1 doesn't contain a valid partition table

disk name are : /dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/mapper/openstackvm00--vg-root /dev/mapper/openstackvm00--vg-swap_1
disk size are : 10.7 32.2 8589 10.7 40.5 1073
Head are : 255 64 86 255 255 255
Sector are : 63 51 1 63 63 63
Cylinder are : 1305 19275 195083 1305 4928 130
Total are : 20971520 62914560 16777216 20971520 79183872 2097152

在此输出中,我不需要前两行

 Disk /dev/mapper/openstackvm00--vg-root doesn't contain a valid partition table
Disk /dev/mapper/openstackvm00--vg-swap_1 doesn't contain a valid partition table

我想要这样的输出

 Device Name  Size   Head Sector Cylinder Total
/dev/sda 10 255 63 1305 20971520
/dev/sdb -- -- -- -- ---
/dev/sdc -- -- -- - -- - --

我怎么会变成这样?

fdisk -l 输出重定向到 Fdisk.txt 文件输出。 cat/tmp/fdisk.txt 文件 # 猫/tmp/fdisk.txt

  Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0005b9c4

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 248832 83 Linux
/dev/sda2 501758 20969471 10233857 5 Extended
/dev/sda5 501760 20969471 10233856 8e Linux LVM

Disk /dev/sdb: 32.2 GB, 32212254720 bytes
64 heads, 51 sectors/track, 19275 cylinders, total 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe3162587

Device Boot Start End Blocks Id System
/dev/sdb1 2048 62914559 31456256 8e Linux LVM

Disk /dev/sdc: 8589 MB, 8589934592 bytes
86 heads, 1 sectors/track, 195083 cylinders, total 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa4cdf171

Device Boot Start End Blocks Id System
/dev/sdc1 2048 16777215 8387584 83 Linux

Disk /dev/sdd: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x3632934a

Device Boot Start End Blocks Id System
/dev/sdd1 2048 10487807 5242880 83 Linux
/dev/sdd2 10487808 14682111 2097152 8e Linux LVM
/dev/sdd3 14682112 15706111 512000 83 Linux
/dev/sdd4 15706112 15910911 102400 83 Linux

Disk /dev/mapper/openstackvm00--vg-root: 40.5 GB, 40542142464 bytes
255 heads, 63 sectors/track, 4928 cylinders, total 79183872 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/openstackvm00--vg-swap_1: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders, total 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

最佳答案

这些

Disk /dev/mapper/openstackvm00--vg-root doesn't contain a valid partition table
Disk /dev/mapper/openstackvm00--vg-swap_1 doesn't contain a valid partition table

错误会打印到 stderr,因此您需要将 stderr 重定向到 /dev/null 以消除它们,如下所示:

fdisk -l 2>/dev/null >/tmp/fdisk.txt

关于您的主要问题,虽然这在 shell 中当然可行,但您可能需要考虑使用 awk。这是一个例子:

$ cat t.sh
#!/bin/bash

awk '
BEGIN { FS="[[:space:]:,]+" }

/contain a valid partition table/ { next }

/^Disk [/]dev/ {
disk=$2
a[disk]["size"] = $5/1024/1024/1024
}

/heads.+sectors[/]track.+cylinders.+total.+sectors/ {
a[disk]["heads"] = $1
a[disk]["sectors"] = $3
a[disk]["cylinders"] = $5
a[disk]["total"] = $8
}

END {
format_header = "%-38s %10s %10s %10s %10s %10s\n"
format_row = "%-38s %7.1f GB %10s %10s %10s %10s\n"
printf format_header, "Device Name", "Size", "Head", "Sector", "Cylinder", "Total"
for (disk in a) {
printf format_row, disk, a[disk]["size"], a[disk]["heads"], a[disk]["sectors"], a[disk]["cylinders"], a[disk]["total"]
}
}' fdisk.txt

运行示例:

$ ./t.sh
Device Name Size Head Sector Cylinder Total
/dev/mapper/openstackvm00--vg-swap_1 1.0 GB 255 63 130 2097152
/dev/mapper/openstackvm00--vg-root 37.8 GB 255 63 4928 79183872
/dev/sda 10.0 GB 255 63 1305 20971520
/dev/sdb 30.0 GB 64 51 19275 62914560
/dev/sdc 8.0 GB 86 1 195083 16777216
/dev/sdd 10.0 GB 255 63 1305 20971520

您甚至可以使用进程替换来替换 fdisk.txt 以删除临时文件。替换

}' fdisk.txt

}' <(fdisk -l 2>/dev/null)

请注意,此信息也可能存在于 /proc 下的某个位置,可能可以更可靠、更方便地进行解析(我的 fdisk 输出与您的输出略有不同)。

关于linux - 使用 shell 脚本在同一行中打印两个变量输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30395472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com