#!/bin/ bash # 用shell中的数组构造统计不同的组合个数. # 下面的组合有( 1 , 6 )( 3 , 4 )( 5 , 5 )( 1 , 6 )( 5 , 5 )( 4 , 3 )( 1 , 8 ) # 不同的组合为( 1 , 6 )( 3 , 4 )( 5 , 5 )( 4 , 3 )( 1 , 8 ) arrayLeft =( 1 3 5 1 5 4 1 ) arrayRight =( 6 4 5 6 5 3 8 ) # comNum用来表示不同组合个数 comNum = 0 arrayLeftLength =${#arrayLeft[* ]} if [[ arrayLeftLength -ge 1 ]] then array[ 0 ]= " ${arrayLeft[0]} ${arrayRight[0]} " arrayDiff[ 0 ]=${array[ 0 ]} ((comNum += 1 )) fi for ((i= 1 ;i<arrayLeftLength;i++ )) do array[$i] = " ${arrayLeft[$i]} ${arrayRight[$i]} " sameFlag = 0 for ((j= 0 ;j<=$(($i- 1 ));j++ )) do if [[ " ${array[$i]} " = " ${array[$j]} " ]] then sameFlag = 1 break fi done if [[ sameFlag -eq 1 ]] then : else arrayDiff[$comNum] = ${array[$i]} ((comNum += 1 )) fi done echo $comNum echo ${array[* ]} echo ${arrayDiff[* ]} for ((i= 0 ;i<arrayLeftLength;i++ )) do echo ${arrayDiff[$i]} done