关于shell遍历查询怎么实现?
定义:
export list1="TABLE1 TABLE2 TABLE3 TABLE4"
export list2="TABLE1 TABLE2 TABLE3"
想实现,list1和list2比较,如果list2与list1一致,继续执行下面程序,如果不一致,提示“是否执行下面操作”,如果执行的话,键盘输入"Yy" 如果输入的"Nn",程序退出!
while true
do
echo "list1与list2不一致。"
echo "处理是否继续?(y/n)"
read input
if [[ ${input} = "Y" ]] || [[ ${input} = "y" ]]
then
#处理继续
continue
else
#处理终了
break
fi
done
现在难点在于list1和list2比较,shell判断list2与list1是否一致?
[解决办法]
declare -a list1=(TABLE1 TABLE2 TABLE3 TABLE4)
declare -a list2=(TABLE1 TABLE2 TABLE3)
if [ ${#list1[@]} -ne ${#list2[@]} ]; then
echo "list1与list2不一致"
else
for(( i=0;i<${#list1[@]};i++ ))
do
if [ ${#list1[$i]} != ${#list2[$i]} ]; then
echo "list1与list2不一致"
break
fi
done
fi
[解决办法]
先转化一下
export text1="TABLE1 TABLE2 TABLE3 TABLE4"
export text2="TABLE1 TABLE2 TABLE3"
OLD_IFS="$IFS"
IFS=" "
declare -a list1=($text1)
declare -a list2=($text2)
IFS=$OLD_IFS
后面一样