shell脚本学习——数组的练习

試著忘記壹切 提交于 2020-03-03 07:22:21

1 使用循环批量输出数组的元素

#!/bin/bash
#使用循环批量输出数组的元素
array=(a b c d e)
for ((i=0;i<${#array[@]};i++))	#从数组的第一个下标0开始,循环数组的所有下标
do
	echo ${array[i]}	#打印数组元素
done

在这里插入图片描述

2 通过竖向列举方法定义数组元素并批量打印

#!/bin/bash
#通过竖向列举方法定义数组元素并批量打印
array=(
westos
redhat
python
linux
)

for ((i=0;i<${#array[@]};i++))
do
echo "This is NO.$i,then content is ${array[i]}"
done
echo ====================================
echo "array lenth:${#array[@]}"

在这里插入图片描述

3 将命令结果作为数组元素定义并打印

#!/bin/bash
#将命令结果作为数组元素定义并打印
dir=($(ls /array))

for ((i=0;i<${#dir[*]};i++))
do
	echo "This is NO.$i,filename is ${dir[$i]}"
done

在这里插入图片描述

4 利用bash for 循环打印下面这句话中字母数不大于6的单词

#!/bin/bash
#利用bash for 循环打印下面这句话中字母数不大于6的单词
# May there be enough clouds in your life to make a beautiful sunset

array=(May there be enough clouds in your life to make a beautiful sunset
)
for ((i=0;i<${#array[@]};i++))
do
	if [ ${#array[$i]} -le 6 ]
	then
		echo ${array[$i]}
	fi
done

在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!