【操作系统作业—lab1】linux shell脚本 遍历目标文件夹和所有文件 | 包括特殊字符文件名的处理
要求:写一个linux bash脚本来查看目标文件夹下所有的file和directory,并且打印出他们的绝对路径。
运行command:./myDir.sh input_path output_result
要求输出格式为:

代码思路:
BFS遍历,数据结构为queue,数组实现。
代码实现:
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b") #处理特殊字符文件名 queue[]="head"
path_[]=''
head_index= #head = the first inde -
tail_index= #tail=length the last index +
head="null"
dir_count=
file_count=
path=`` #if the output directory is not exist, make a new directory
#处理目标文件所处地址不存在问题 out_path=$
e_path=""
while [ ${out_path##*/} != ${out_path%%/*} ]
do
dir_name=${out_path%%/*}
if [ ! -e $e_path""$dir_name ]
then mkdir $e_path""$dir_name
fi
e_path=$e_path""$dir_name"/"
out_path=${out_path#*/}
done
touch $ #use queue to take BFS function enQueue(){ #insert into tail
queue[${tail_index}]=$
path_[${tail_index}]=$path"/"$
tail_index=$((${tail_index}+))
} function deQueue(){ #dequeue from head
head_index=$((${head_index}+))
head=${queue[head_index]}
} #start of this program
enQueue $
while [ ${head_index} -ne $((${tail_index}-)) ]
do
deQueue
path_[]=`pwd`"/"$
path=${path_[$head_index]}
echo "["${head}"]" >>$ for var in `ls ${path_[$head_index]}`
do
if [ -d $path"/""${var}" ] then
dir_count=$((${dir_count}+))
enQueue $var
fi
echo $path"/""${var}" >>$
file_count=$((${file_count}+))
done
echo "" >>$
done file_count=$((${file_count}-${dir_count}))
echo "[Directories Count]:"${dir_count} >>$
echo "[Files Count]:"$file_count >>$ IFS=$SAVEIFS
写作业的时候遇到了很多小问题,因为自己也是刚刚才接触到shell,总结了一些解决方法,放在了另一篇随笔里。
【操作系统作业—lab1】linux shell脚本 遍历目标文件夹和所有文件 | 包括特殊字符文件名的处理的更多相关文章
- Linux shell 脚本攻略之创建不可修改文件
摘自:<Linux shell 脚本攻略>
- LINUX SHELL脚本攻略笔记[速查]
Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...
- 老李分享:《Linux Shell脚本攻略》 要点(二)
老李分享:<Linux Shell脚本攻略> 要点(二) poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课 ...
- Linux shell脚本编程(三)
Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...
- Linux shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- Linux Shell 脚本入门
linux shell 脚本格式 #!/bin/sh#..... (注释)命令...命令... 使用vi 创建完成之后需设置权限 chmod +x filename.sh 执行命令: ./filena ...
- 阿里Linux Shell脚本面试25个经典问答
转载: 阿里Linux Shell脚本面试25个经典问答 Q:1 Shell脚本是什么.它是必需的吗? 答:一个Shell脚本是一个文本文件,包含一个或多个命令.作为系统管理员,我们经常需要使用多个命 ...
- 老李分享:《Linux Shell脚本攻略》 要点(三)
老李分享:<Linux Shell脚本攻略> 要点(三) 1.生产任意大小的文件 [root@localhost dd_test]#[root@localhost dd_test]# ...
- linux ——shell 脚本
linux—shell 脚本 精简基础 2018/10/30 13 ...
随机推荐
- mysql四-1:单表查询
一.单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二.关键 ...
- BZOJ2438: [中山市选2011]杀人游戏(tarjan)
题意 题目链接 Sol 这题挺考验阅读理解能力的.. 如果能读懂的话,不难发现这就是在统计有多少入度为\(0\)的点 缩点后判断一下即可 当然有一种例外情况是\(1 -> 3, 2 -> ...
- 关于maven包的引入net.sf.json的问题
最开始通过在pom.xml文件中加入 <dependency> <groupId>net.sf.json-lib</groupId> <artifactId& ...
- java面试题之----super和this
super和this的异同: super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句) this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句) supe ...
- [转载]AMOLED结构详解,BOE专家给你分析驱动补偿
关键词: AMOLED, 驱动补偿 有机发光显示二极管(OLED)作为一种电流型发光器件已越来越多地被应用于高性能显示中.由于它自发光的特性,与LCD相比,AMOLED具有高对比度.超轻薄.可弯曲等诸 ...
- 函数名: lseek
函数名: lseek 功 能: 移动文件读/写指针 头文件:#include <sys/types.h> #include <unistd.h> 用 法: off_t lsee ...
- 统计一段文章的单词频率,取出频率最高的5个单词和个数(python)
练习题:统计一段英语文章的单词频率,取出频率最高的5个单词和个数(用python实现) 先全部转为小写再判定 lower() 怎么判定单词? 1 不是字母的特殊字符作为分隔符分割字符串 (避免特殊字符 ...
- 【转载】#440 - A Class Can Implement More than One Interface
It's possible for a class to implement more than one interface. For example, a Cow class might imple ...
- OC property(声明)
@interface Student : NSObject { int _age; int _no; float _height; } // 当编译器遇到@property时,会自动展开成getter ...
- Python:运算与循环
1.格式化输出 name = input("请输入你的名字:") age =input("请输入你的年龄:") job =input("请输入你的工作 ...