#!/bin/bash
echo "作业1"
read -p "请输入数组: " -a arr
read -p "请输入要查找元素: " key
((find=0))
for i in "${arr[@]}"
do
if [ "$i" = "$key" ]
then
echo 找到
((find=1))
break
fi
done
if [ "$find" = "0" ]
then
echo "不存在:$key 不在数组中"
fi
ubuntu@ub
#!/bin/bash
echo "作业2"
read -p "请输入数组元素: " -a arr
read -p "请输入查找元素key: " key
for i in "${!arr[@]}"
do
if [ "${arr[i]}" = "$key" ]
then
arr[i]="hehe"
break
fi
done
echo "替换结果:${arr[@]}"
#!/bin/bash
echo "作业3"
read -p "请输入数组: " -a arr
read -p "输入删除元素: " key
new_arr=()
found=0
for i in "${!arr[@]}"
do
if [ "${arr[$i]}" = "$key" ]
then
((index=i))
break
fi
done
unset arr[$index]
echo "删除后的数组:${arr[@]}"
ubuntu@ubuntu:~/homework/C_high$