這是我的腳本打印兩個變量輸出使用shell腳本
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文件輸出。貓/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
你有什麼試過? Stackoverflow不是要獲得免費的代碼,而是要獲得免費的**幫助**。祝你好運。 – shellter
您可以分享您迄今嘗試過的示例代碼嗎? – mtyurt
使用['tr'](http://linux.die.net/man/1/tr)將逗號轉換爲換行符和['paste'](http://linux.die.net/man/1/)粘貼)到壓縮數據。你需要[流程替代](http://tldp.org/LDP/abs/html/process-sub.html)來完成它。 – myaut