读书人

关于shell遍历查询如何实现

发布时间: 2012-12-31 11:57:52 作者: rapoo

关于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

后面一样

引用:
引用:定义:
export list1="TABLE1 TABLE2 TABLE3 TABLE4"
export list2="TABLE1 TABLE2 TABLE3"
想实现,list1和list2比较,如果list2与list1一致,继续执行下面程序,如果不一致,提示“是否执行下面操作”,如果执行的话,键盘输入"Yy" 如果输入的……

[解决办法]
你用的是bash嘛? AIX默认是ksh吧?

那有的地方可能会不一样,declare -a list1=($text1)直接用list1=($text1)

还有ksh if的判断要用[[ ]]不是bash的[]
[解决办法]
手头没有ksh,再试试用set -A list1=($text1)

引用:
引用:你用的是bash嘛? AIX默认是ksh吧?

那有的地方可能会不一样,declare -a list1=($text1)直接用list1=($text1)

还有ksh if的判断要用[[ ]]不是bash的[]

单独执行 list1=($text1)
报括号有问题,我看了一下是英文的括号的。

读书人网 >其他服务器

热点推荐