【操作系统作业—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 ...
随机推荐
- phpmyadmin-错误:配置文件权限错误,不应任何用户都能修改!这里有答案
今天在linux下使用phpMyadmin的时候突然出现这个红色警告.差点把我吓晕在电脑前.不过冷静想一下这个报错,肯定就是linux权限那几个 ‘7’ 惹的祸. 于是 通过命令 ‘ll’ (这是英 ...
- 采用C/C++语言如何实现复数抽象数据类型Complex
记录一下! 采用C/C++语言如何实现复数抽象数据类型Complex #include <stdio.h> typedef struct Complex { double e1; // 实 ...
- 【Leetcode】【Easy】Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- PL/SQL Developer import and export database method and illustrate
PL/SQL Developer import and export database method and illustrate HOW WELL DO YOU KNOW THE APPLE U ...
- Inner Join, Left Outer Join和Association的区别
测试用的CDS视图的源代码,第8行用Inner Join连接TJ02T, 后者存放了所有系统状态的ID和描述. Inner Join测试结果:对于那些在TJ02T里没有维护描述信息的状态,它们不会出现 ...
- python入门23 pymssql模块(python连接sql server增删改数据 )
增删改数据必须connect.commit()才会生效 回滚函数 connect.rollback() 连接数据库 ''' dinghanhua sql server增删改 ''' import py ...
- Gym - 101334E 多叉树遍历
题意:给定一个字符串,求有多少种树与之对应,对应方式是,每次遍历左节点,没有了,就回溯: 分析:d[i,j] = sum(d[i+1,k-1],d[k,j]) (str[i]==str[k]); 坑点 ...
- POJ 2704 Pascal's Travels 【DFS记忆化搜索】
题目传送门:http://poj.org/problem?id=2704 Pascal's Travels Time Limit: 1000MS Memory Limit: 65536K Tota ...
- POJ 1579 Function Run Fun 【记忆化搜索入门】
题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Tota ...
- php 的 number_format使用
$num = 1.0258963147; // 1.0259 $num = number_format($num, 4, '.', ''); $num = number_format($num); $ ...