[root@wzlvm shell]# cat shell_array_test.sh
#!/bin/bash
#
#
#
# Aut wzl
# Shell Document
## 判断是否传入参数如果有参数
if [ $# -gt 0 -a $# -le 1 ];then
## 获得实际参数 决定属组长度
num=$1
## 定义递增索引和初始值
i=0
## 循环判断 索引小于等于属组个数,则进入循环
while [ $i -lt $num ]
do
## 循环让用户输入属组值用变量保存
read -e -p ”请输入第${i}个值:“ str
## 变量的值赋值给属组
array[$i]=$str
## 索引递增加1
i=$[i+1]
## 结束
done
## 遍历属组
## 定义读取属组的索引
j=0
## 循环读取属组
while [ $j -lt ${#array[*]} ]
## 开始
do
##读取属组数值
#echo "${array[j]}"
printf "${array[j]}\t"
## 索引加1
j=$[j+1]
##结束
done
printf "\n"
# 上面的遍历可以使用一个简单定for循环
# for last in ${array[*]}
# do
# echo $last
# done
## 如果没有参数
else
echo '输入参数个数不正确'
fi
[root@wzlvm shell]# sh shell_array_test.sh
输入参数个数不正确
[root@wzlvm shell]# sh shell_array_test.sh 5 9
输入参数个数不正确
[root@wzlvm shell]# sh shell_array_test.sh 5
”请输入第0个值:“3
”请输入第1个值:“4
”请输入第2个值:“5
”请输入第3个值:“2
”请输入第4个值:“1
3 4 5 2 1
后面可以再做属组排序,后续
643

被折叠的 条评论
为什么被折叠?



