shell两个数组求交集
时间: 2023-08-09 12:05:00 浏览: 188
可以使用循环遍历两个数组,将相同的元素存储到一个新的数组中,最终得到交集。
以下是一个示例代码:
```bash
#!/bin/bash
# 定义两个数组
arr1=(1 2 3 4 5)
arr2=(3 4 5 6 7)
# 定义一个空数组用于存储交集
intersection=()
# 遍历第一个数组
for i in "${arr1[@]}"
do
# 遍历第二个数组
for j in "${arr2[@]}"
do
# 如果元素相同,则存储到交集数组中
if [ "$i" == "$j" ]; then
intersection+=("$i")
break
fi
done
done
# 输出交集
echo "${intersection[@]}"
```
输出结果为:
```
3 4 5
```
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)