【操作系统作业—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 ...
随机推荐
- 关于i 标签盛放背景图像
1.html部分 <div class="hover right"> <i class="log_change state_psd">& ...
- String字符串操作
char chars[] ={'a','b','c'}; String s = new String(chars); int len = s.length();//字符串长度 System.out.p ...
- OSGi Bundle
OSGi Framework looks like OS, Bundle looks like program, OS can create a process to run program with ...
- matlab练习程序(矩形变换为圆)
最近对图像坐标的变换很感兴趣啊,这次是将一张图像变换为圆形. 变换原理就是按变换前后像素到圆心的距离按比例缩减就行了. 改变x,y方向上的系数,应该还可以变换为椭圆,不过我还没有尝试. 注意我这里相当 ...
- python实现oracle数据泵导出功能
脚本如下:[oracle@ycr python]$ more dump.py #/usr/bin/python#coding:utf8 import sysimport osimport time n ...
- 收放卷及张力控制 PID调试技巧
1) 小 Kp( 0.01) , 大 Ti ( 20000ms) 2)逐渐增大Kp, 减小Ti ( 20000ms – 3000ms),避免发生震荡 3)观察I-out 是否在0附近 可能原因:卷径不 ...
- arcgis for javascript ArcGISDynamicMapServiceLayer 过滤图层点
记录下过滤 ArcGISDynamicMapServiceLayer 图层服务的方法 function filterArcGISDynamicMapServiceLayer(){ map.graphi ...
- iOS UI(布局)约束是什么?view1.attr1 = view2.attr2 * multiplier + constant
/* Create constraints explicitly. Constraints are of the form "view1.attr1 = view2.attr2 * mul ...
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】
任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...
- POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Sub ...