0

我正在做 bash,我正在寻找数组比较的问题,这就是下面的解决方案。我想知道为什么他们有 do in=false

#!/bin/bash
# enter your array comparison code here
# initialize arrays a b c
a=(3 5 8 10 6) 
b=(6 5 4 12) 
c=(14 7 5 7)
#comparison of first two arrays a and b
for x in "${a[@]}"; do 
  in=false 
  for y in "${b[@]}"; do 
    if [ $x = $y ];then 
      # assigning the matching results to new array z
      z[${#z[@]}]=$x
    fi
  done 
done
#comparison of third array c with new array z
for i in "${c[@]}"; do 
  in=false
  for k in "${z[@]}"; do
    if [ $i = $k ];then
      # assigning the matching results to new array j
      j[${#j[@]}]=$i
    fi
  done 
done 
# print content of array j
echo ${j[@]}

那么应该看起来像这样正确吗?第一个 for 循环做空白,然后其他循环分析数组。

#!/bin/bash
a=(3 5 8 10 6) 
b=(6 5 4 12) 
c=(14 7 5 7)
#comparison of first two arrays a and b
for x in "${a[@]}"; do 

  for y in "${b[@]}"; do 
    if [ "$x" = "$y" ];then 
      # assigning the matching results to new array z
      z+=("$x")
    fi
  done 
done
#comparison of third array c with new array z
for i in "${c[@]}"; do 

  for k in "${z[@]}"; do
    if [ "$i" = "$k" ];then
      # assigning the matching results to new array j
     j+=("$i")
    fi
  done 
done 
# print content of array j
echo ${j[@]}
4

0 回答 0