Linux下shell数组操作 | 小白笔记 |
【摘要】
Linux shell 数组操作 – 小白笔记
参考链接:菜鸟教程
Linux下shell数组操作
认识 shell 数组shell 程序递归遍历文件夹
认识...
Linux shell 数组操作 – 小白笔记
参考链接:菜鸟教程
认识 shell 数组
创建 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[@]}"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 依次 查看脚本、修改脚本可执行属性、运行脚本
cat moli.sh
chmod 755 moli.sh
./moli.sh
或者
bash moli.sh
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 运行脚本,输出如下:
第一个元素为: A
第二个元素为: B
第三个元素为: C
第四个元素为: D
获取数字全部元素:
数组的元素为: A B C D
数组的元素为: A B C D
数组长度:
数组元素个数为: 4
数组元素个数为: 4
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 运行效果如下:
./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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
查看 项目目录分布 ,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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
文章来源: positive.blog.csdn.net,作者:墨理学AI,版权归原作者所有,如需转载,请联系作者。
原文链接:positive.blog.csdn.net/article/details/114363586
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)