如何迭代4个变量,并在5个变量之间进行比较?

时间:2013-11-27 16:05:45

标签: linux bash scripting

这已经解决但如果我的文件(text和text2)与下面的格式一样怎么样呢

LOGICAL UNIT NUMBER 150
UID:                      60:06:01:60:10:20:2E:150
LOGICAL UNIT NUMBER 201
UID:                        60:06:01:60:BB:20:2E:201
LOGICAL UNIT NUMBER 30
UID:                         60:06:01:60:BB:10:2E:30
LOGICAL UNIT NUMBER 50
UID:                         60:06:01:60:BB:10:2E:50
LOGICAL UNIT NUMBER 1500
UID:                       60:06:01:60:BB:10:2E:1500
LOGICAL UNIT NUMBER 100
UID:                            60:06:01:60:BB:10:2E:100
LOGICAL UNIT NUMBER 1100
UID:                            60:06:01:60:BB:10:2E:100
LOGICAL UNIT NUMBER 1600
UID:                            60:06:01:60:BB:10:2E:100

我有以下脚本并且它工作正常但是在某些情况下它只在(i和y)中的相同值之间而不是在所有值之间相等所以我需要一些东西来比较所有(i变量与所有y变量)然后打印输出文件。

exec 5< <(awk '{IGNORECASE=1}/UID/ {print $4}' TEXT)
exec 6< <(awk '{IGNORECASE=1}/LOGICAL/ {print $6}' TEXT)
exec 7< <(awk '{IGNORECASE=1}/UID/ {print $4}' TEXT2)
exec 8< <(awk '{IGNORECASE=1}/LOGICAL/ {print $6}' TEXT2)
while read i <&5 && read x <&6  && read y <&7 && read z <&8
do
if [ "$i" == "$y" ]
then
echo " naviseccli -h 10.1.xx.xx -user admin -password xxxx -scope 0 sancopy -       create -incremental -name copy_LUN_$i -srcwwn $x -destwwn $z -verify -linkbw 1024" >>    INSHALLAH_KELSET
else
echo " no devices found "
fi
done

===================

我已经bash -x INSHALLAH并且在输出

之下
$ bash -x INSHALLAH
+ exec
++ awk '{IGNORECASE=1}/UID/ {print $4}' TEXT
+ exec
++ awk '{IGNORECASE=1}/LOGICAL/ {print $6}' TEXT
+ exec
+ exec
++ awk '{IGNORECASE=1}/UID/ {print $4}' TEXT2
++ awk '{IGNORECASE=1}/LOGICAL/ {print $6}' TEXT2
+ read i
+ read x
+ read y
+ read z
+ '[' 50 == 50 ']'
+ echo ' naviseccli -h 10.1.xx.xx -user admin -password xxxxxx -scope 0 sancopy -create -incremental -name copy_LUN_50 -srcwwn 60:06:01:60:10:20:2E:00:24:DF:C9:2C:F4:AC:E2:11 -destwwn 60:06:01:60:BB:10:2E:00:96:6D:0E:82:B4:09:E3:13 -verify -linkbw 1024'
+ read i
+ read x
+ read y
+ read z
+ '[' 201 == 201 ']'
+ echo ' naviseccli -h 10.1.xx.xx -user admin -password xxxxx -scope 0 sancopy -create -incremental -name copy_LUN_201 -srcwwn 60:06:01:60:10:20:2E:00:08:A5:91:23:FF:AF:E2:11 -destwwn 60:06:01:60:10:20:2E:00:08:A5:91:23:FF:AF:E2:11 -verify -linkbw 1024'
+ read i
+ read x
+ read y
+ read z
+ '[' 300 == 30 ']'
+ echo ' no devices found '
 no devices found
+ read i
+ read x
+ read y
+ read z
+ '[' 150 == 1500 ']'
+ echo ' no devices found '
 no devices found
+ read i
+ read x
+ read y
+ read z
+ '[' 1500 == 100 ']'
+ echo ' no devices found '
 no devices found
+ read i

这是我的2个文件,与下面的文件相同

TEXT

LOGICAL UNIT NUMBER 50 
UID:                        60:06:01:60:BB:10:2E:00:96:6D:0E:82:B4:09:E3:13 
LOGICAL UNIT NUMBER 201 
UID:                       60:06:01:60:10:20:2E:00:08:A5:91:23:FF:AF:E2:11
LOGICAL UNIT NUMBER 30 
UID:                        60:06:01:60:10:20:2E:00:BA:FF:3B:5A:2C:9A:E2:11
LOGICAL UNIT NUMBER 1500 
UID:                      60:06:01:60:BB:10:2E:00:96:6D:0E:82:B4:09:E3:11
LOGICAL UNIT NUMBER 100 
UID:                       60:06:01:60:BB:10:2E:00:96:6D:0E:82:B4:09:E3:14
LOGICAL UNIT NUMBER 150
 UID:                       60:06:01:60:BB:10:2E:00:96:6D:0E:82:B4:09:E3:13

TEXT2

LOGICAL UNIT NUMBER 50   
UID:                       60:06:01:60:10:20:2E:00:24:DF:C9:2C:F4:AC:E2:11
LOGICAL UNIT NUMBER 201 
UID:                        60:06:01:60:10:20:2E:00:08:A5:91:23:FF:AF:E2:11
LOGICAL UNIT NUMBER 300 
UID:                        60:06:01:60:10:20:2E:00:BA:FF:3B:5A:2C:9A:E2:11
LOGICAL UNIT NUMBER 150 
UID:                        60:06:01:60:BB:10:2E:00:96:6D:0E:82:B4:09:E3:11
LOGICAL UNIT NUMBER 1500 
UID:                       60:06:01:60:BB:10:2E:00:96:6D:0E:82:B4:09:E3:13

我必须为1000台设备创建大约1000个会话,而无法创建手册。

2 个答案:

答案 0 :(得分:1)

根据您的问题,我不明白其他文件与i和y值的关系。这是用于将file1的所有值与file2进行比较的简化版本。

while read i<&5; 
do  
   while read y<&7; 
      do echo $i,$y; 
   done 7< <(awk '{print}' t2.txt) ; 
done 5< <(awk '{print}' t1.txt)

这可能会对您有所帮助:

while read i && read j;
do
   while read y && read z;
   do
        if [ "$i" == "$y" ]
        then
                echo " naviseccli -h 10.1.xx.xx -user admin -password xxxx -scope 0 sancopy -       create -incr
emental -name copy_LUN_$i -srcwwn $j -destwwn $z -verify -linkbw 1024" >>    INSHALLAH_KELSET
        else
                echo " no devices found "
        fi
   done < <(awk '{IGNORECASE=1; OFS="\n"}/UID/ {print $4, $6}' TEXT2) ;
done < <(awk '{IGNORECASE=1; OFS="\n"}/UID/ {print $4, $6}' TEXT)

答案 1 :(得分:0)

您希望分两步完成此操作。

首先,使用awk提取所需的信息。在这种情况下,请使用awk记住跨输入行的变量这一事实。以“LOGICAL UNIT NUMBER”开头的行将变量“lun”设置为该值。以“UID:”开头的行打印出lun和wwn。

然后使用“join”匹配2个文件(使用第一列匹配它们)并输出命令:

# Extract the data into "LUN WWN" files, sorted.
awk '/^LOGICAL UNIT NUMBER/ { lun = $4 } /^UID:/ { print lun " " $2 }' <text1.txt | sort -n >items1.txt
awk '/^LOGICAL UNIT NUMBER/ { lun = $4 } /^UID:/ { print lun " " $2 }' <text2.txt | sort -n >items2.txt
# Use join to match up the LUNs of each file (LUN SRCWWN DSTWWN) then use awk to write out the commands:
join items1.txt items2.txt | awk '{ print "naviseccli -h 10.1.xx.xx -user admin -password xxxx -scope 0 sancopy -create -incremental -name copy_LUN_" $1 " -srcwwn " $2 " -destwwn " $3 " -verify -linkbw 1024" }'

如果您将其放入bash脚本中,则可以利用单引号可以包含多行文本的事实。这将使其更具可读性:

# Extract the data into "LUN WWN" files, sorted.
awk '
     /^LOGICAL UNIT NUMBER/ { lun = $4 }
     /^UID:/                { print lun " " $2 }
    ' <text1.txt | sort -n >items1.txt
awk '
     /^LOGICAL UNIT NUMBER/ { lun = $4 }
     /^UID:/                { print lun " " $2 }
    ' <text2.txt | sort -n >items2.txt
# Use join to match up the LUNs of each file (LUN SRCWWN DSTWWN) then use awk to write out the commands:
join items1.txt items2.txt | \
    awk '{ print "naviseccli -h 10.1.xx.xx -user admin -password xxxx -scope 0 sancopy -create -incremental -name copy_LUN_" $1 " -srcwwn " $2 " -destwwn " $3 " -verify -linkbw 1024" }'

输出:

naviseccli -h 10.1.xx.xx -user admin -password xxxx -scope 0 sancopy -create -incremental -name copy_LUN_50 -srcwwn 60:06:01:60:BB:10:2E:00:96:6D:0E:82:B4:09:E3:13 -destwwn 60:06:01:60:10:20:2E:00:24:DF:C9:2C:F4:AC:E2:11 -verify -linkbw 1024
naviseccli -h 10.1.xx.xx -user admin -password xxxx -scope 0 sancopy -create -incremental -name copy_LUN_201 -srcwwn 60:06:01:60:10:20:2E:00:08:A5:91:23:FF:AF:E2:11 -destwwn 60:06:01:60:10:20:2E:00:08:A5:91:23:FF:AF:E2:11 -verify -linkbw 1024