Linux下shell数组操作 | 小白笔记 |

Linux shell 数组操作 – 小白笔记


参考链接:菜鸟教程


认识 shell 数组


0-1


创建 shell 脚本
vim moli.sh

  • 程序内容如下:
#!/bin/bash

my_array=(A B "C" D)

echo "第一个元素为: ${my_array[0]}"
echo "第二个元素为: ${my_array[1]}"
echo "第三个元素为: ${my_array[2]}"
echo "第四个元素为: ${my_array[3]}"

echo  获取数字全部元素:

echo "数组的元素为: ${my_array[*]}"
echo "数组的元素为: ${my_array[@]}"

echo  数组长度:
echo "数组元素个数为: ${#my_array[*]}"
echo "数组元素个数为: ${#my_array[@]}"


  • 依次 查看脚本、修改脚本可执行属性、运行脚本
cat moli.sh 

chmod 755 moli.sh 

./moli.sh 
或者
bash moli.sh 

  • 运行脚本,输出如下:
第一个元素为: A
第二个元素为: B
第三个元素为: C
第四个元素为: D
获取数字全部元素:
数组的元素为: A B C D
数组的元素为: A B C D
数组长度:
数组元素个数为: 4
数组元素个数为: 4

shell 程序递归遍历文件夹


vim getdir.sh

#!/bin/bash
# 运行时,需要一个 路径 传参 

function getdir(){
	echo $1
	for file in $1/*
	do 
	if test -f $file
	then
		echo $file
	else
		getdir $file
	fi
	done
}

getdir $1

  • 运行效果如下:

./getdir.sh /build/linux 


# 输出如下:

/build/linux
/build/linux/123.sh
/build/linux/allFile.sh
/build/linux/getdir.sh
/build/linux/moli.sh
/build/linux/sampleFile
/build/linux/sampleFile/1.txt
/build/linux/sampleFile/2.txt
/build/linux/sampleFile/3.txt
/build/linux/test.sh

查看 项目目录分布 ,Linux 下 一个 tree 命令即可

查看系统
cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)


yum install tree

tree
.
|-- 123.sh
|-- allFile.sh
|-- getdir.sh
|-- moli.sh
|-- sampleFile
|   |-- 1.txt
|   |-- 2.txt
|   `-- 3.txt
`-- test.sh


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨理学AI

不必打赏,关注博主公众号即可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值