简体   繁体   中英

bash awk file compare

I have a config
[LogicalUnit1]  UnitInquiry "NFSN00Y5IP51ZL"  LUN0 /mnt/extent0 64MB
[LogicalUnit2]  UnitInquiry "NFSN00N49CQL28"  LUN0 /mnt/extent1 64MB
[LogicalUnit3]  UnitInquiry "NFSNBRGQOCXK"  LUN0 /mnt/extent4 10MB
[LogicalUnit4]  UnitInquiry "NFSNE7IXADFJ"  LUN0 /mnt/extent5 25MB

which is read via a bash script, using awk i parse the file and get variables

    awk '/UnitInquiry/ {print $1, $3, $5, $6}' $ctld_config | while read a b c d ; do
        if [ -f $a ]
        then
              ctladm create -b block -o file=$c -S $b -d $a
              ctladm devlist -v > $lun_config
        else
              truncate -s $d $c ; ctladm create -b block -o file=$c -S $b -d $a
        fi

this will initialize the luns properly on bootup, however if i add a lun then it will recreate them all again, how can i compare whats running, to whats configured and only reinitialize the ones not already live, there is a command to list the devices

ctladm devlist -v
LUN Backend       Size (Blocks)   BS Serial Number    Device ID       
  0 block                131072  512 "NFSN00Y5IP51ZL  [LogicalUnit1]  
      lun_type=0
      num_threads=14
      file=/mnt/extent0
  1 block                131072  512 "NFSN00N49CQL28  [LogicalUnit2]  
      lun_type=0
      num_threads=14
      file=/mnt/extent1
  2 block                 20480  512 "NFSNBRGQOCXK"   [LogicalUnit3]  
      lun_type=0
      num_threads=14
      file=/mnt/extent4
  3 block                 51200  512 "NFSNE7IXADFJ"   [LogicalUnit4]  
      lun_type=0
      num_threads=14
      file=/mnt/extent5

Why not add the following after the then :

ctladm devlist -v | grep -q "$a" && continue

This will

  • run the command that show the currently active devices
  • check if the LogicalUnit name you want to register is already listed, and if yes...
  • skip the rest of the loop.

If $a (logical unit name) is not unique enough, you can also grep for another, more unique identifier, eg the serial number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM