定义:
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
后面一样
------解决方案--------------------------------------------------------
你用的是bash嘛? AIX默认是ksh吧?
那有的地方可能会不一样,declare -a list1=($text1)直接用list1=($text1)
还有ksh if的判断要用[[ ]]不是bash的[]
------解决方案--------------------------------------------------------
手头没有ksh,再试试用set -A list1=($text1)