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

时间:2015-05-22 11:25:11

标签: linux 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文件输出。 cat /tmp/fdisk.txt文件       #cat /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

2 个答案:

答案 0 :(得分:4)

这些

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输出与您的略有不同)。

答案 1 :(得分:0)

#!/bin/bash

var1=2,3,4,5
var2=two,three,four,five

var1_n=$(echo $var1 | tr "," "\n")
var2_n=$(echo $var2 | tr "," "\n")

paste <(echo "$var1_n") <(echo "$var2_n") -d ' '