从列表中获取所有交换分区的id和路径

时间:2018-12-08 14:36:48

标签: regex bash shell awk sed

我需要从命令中获取所有交换分区

blkid
/dev/block/sda4: LABEL="system" UUID="da594c53-9beb-f85c-85c5-cedf76546f7a" TYPE="ext4" 
/dev/block/swap0: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="swap" 
/dev/block/swap1: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="swap" 
/dev/block/vnswap0: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="swap" 
/dev/block/zram0: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="swap" 
/dev/block/sde10: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="ext4" 
/dev/block/sde11: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="ext4" 
/dev/block/sdd13: SEC_TYPE="msdos" UUID="00BC-614E" TYPE="vfat" 
/dev/block/sdd17: SEC_TYPE="msdos" UUID="00BC-614E" TYPE="vfat" 
/dev/block/sdd22: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="ext4" 
/dev/block/mmcblk0p1: UUID="9016-4EF8" TYPE="vfat" 
/dev/block/loop64: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="ext4"

我需要的是获取该分区的路径和UUID并将其放入单独的变量中,以了解它是否是交换分区TYPE必须为TYPE =“ swap” UUID和路径值是可变的(可以更改),这是一些示例

/dev/block/swap0
/dev/block/swap1
/dev/block/swap2
/dev/block/vnswap0
/dev/block/vnswap1
/dev/block/zram0 

我实际上找到了一个可以完成此工作的代码,但是它是受版权保护的,并且我不希望使用它,因此不胜感激。.这有点不同,它在“ / proc / swaps”中查找交换。 / p>

grep zram /proc/swaps
/dev/swap0                              partition       2097148 2816    16383
/dev/zram0                              partition       2097148 2816    16383
/dev/zram1                              partition       2097148 2816    16383

我将在下面提供它仅供参考

# Find all swapregions and target each one for swapoff
# Don't assume it's in the first field of swaps, find it
    SR="\/dev\/"
    PS="/proc/swap*"

    DIE=`awk -v SBD="$SR" ' $0 ~ SBD {
          for ( i=1;i<=NF;i++ )
            {
              if ( $i ~ ( "^" SBD ) )
               {
                  printf "%s;", $i
               }
            }
          }' $PS`

    saveifs=$IFS
    IFS=';'

    # I could have put all this in awk and just eval'd it 
    # But where's the fun in that

    for i in $DIE
    do
        case $i in
            *zram*)
                  j=`echo $i | sed 's/.*zram//'`
                 ( ( 
                     echo $j > /sys/class/zram-control/hot_remove
                     echo 1 > /sys/block/zram${j}/reset
                     swapoff $i
                  ) & )
                  ;;
            *)
                  if [ -n $i ]; then
                      ( ( swapoff $i ) & ) 
                  fi
                  ;;
        esac
    done

    IFS=$saveifs

    exit 0

3 个答案:

答案 0 :(得分:1)

您能不能试一下(我现在正在开车,所以没有测试,一到家也将测试它:))。

awk '$0 ~ /swap/ && match($0,/\/[^:]*/){path=substr($0,RSTART,RLENGTH);match($0,/UUID=\"[^"]*/);print path,substr($0,RSTART+6,RLENGTH-6);path=""}'  Input_file

根据需要的OP 2变量,因此现在也要添加解决方案。

uuids=$(awk '$0 ~ /swap/ && match($0,/UUID=\"[^"]*/){print substr($0,RSTART+6,RLENGTH-6)}'  Input_file)
paths=$(awk '$0 ~ /swap/ && match($0,/\/[^:]*/){substr($0,RSTART,RLENGTH);print substr($0,RSTART,RLENGTH)}' Input_file)

答案 1 :(得分:0)

script.awk

/TYPE="swap"/ {
  path = $1
  uuid = $2
  sub(":", "", path)
  sub("UUID=\"", "", uuid)
  sub("\"", "", uuid)
  print path, uuid
}

返回以下结果:例如blkid | awk -f script.awk

/dev/block/swap0 57f8f4bc-abf4-655f-bf67-946fc0f9f25b
/dev/block/swap1 57f8f4bc-abf4-655f-bf67-946fc0f9f25b
/dev/block/vnswap0 57f8f4bc-abf4-655f-bf67-946fc0f9f25b
/dev/block/zram0 57f8f4bc-abf4-655f-bf67-946fc0f9f25b

对您有用吗?

答案 2 :(得分:0)

使用Perl,只需使用“命令”而不是“ cat ranger.txt”

/tmp> cat ranger.txt
blkid
/dev/block/sda4: LABEL="system" UUID="da594c53-9beb-f85c-85c5-cedf76546f7a" TYPE="ext4"
/dev/block/swap0: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="swap"
/dev/block/swap1: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="swap"
/dev/block/vnswap0: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="swap"
/dev/block/zram0: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="swap"
/dev/block/sde10: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="ext4"
/dev/block/sde11: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="ext4"
/dev/block/sdd13: SEC_TYPE="msdos" UUID="00BC-614E" TYPE="vfat"
/dev/block/sdd17: SEC_TYPE="msdos" UUID="00BC-614E" TYPE="vfat"
/dev/block/sdd22: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="ext4"
/dev/block/mmcblk0p1: UUID="9016-4EF8" TYPE="vfat"
/dev/block/loop64: UUID="57f8f4bc-abf4-655f-bf67-946fc0f9f25b" TYPE="ext4"
/tmp> cat ranger.txt | perl -ne ' /(^.+?):.*UUID=\"(.+?)\".*TYPE=\"swap\"/g and print "$1 $2\n" '
/dev/block/swap0 57f8f4bc-abf4-655f-bf67-946fc0f9f25b
/dev/block/swap1 57f8f4bc-abf4-655f-bf67-946fc0f9f25b
/dev/block/vnswap0 57f8f4bc-abf4-655f-bf67-946fc0f9f25b
/dev/block/zram0 57f8f4bc-abf4-655f-bf67-946fc0f9f25b
/tmp>