要求:写一个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脚本 遍历目标文件夹和所有文件 | 包括特殊字符文件名的处理的更多相关文章

  1. Linux shell 脚本攻略之创建不可修改文件

    摘自:<Linux shell 脚本攻略>

  2. LINUX SHELL脚本攻略笔记[速查]

    Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...

  3. 老李分享:《Linux Shell脚本攻略》 要点(二)

    老李分享:<Linux Shell脚本攻略> 要点(二)   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课 ...

  4. Linux shell脚本编程(三)

    Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...

  5. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  6. Linux Shell 脚本入门

    linux shell 脚本格式 #!/bin/sh#..... (注释)命令...命令... 使用vi 创建完成之后需设置权限 chmod +x filename.sh 执行命令: ./filena ...

  7. 阿里Linux Shell脚本面试25个经典问答

    转载: 阿里Linux Shell脚本面试25个经典问答 Q:1 Shell脚本是什么.它是必需的吗? 答:一个Shell脚本是一个文本文件,包含一个或多个命令.作为系统管理员,我们经常需要使用多个命 ...

  8. 老李分享:《Linux Shell脚本攻略》 要点(三)

    老李分享:<Linux Shell脚本攻略> 要点(三)   1.生产任意大小的文件 [root@localhost dd_test]#[root@localhost dd_test]# ...

  9. linux ——shell 脚本

                                                      linux—shell 脚本  精简基础                 2018/10/30 13 ...

随机推荐

  1. jquery toggle(listenerOdd, listenerEven)

    1. example: <!DOCTYPE HTML><html>    <head></head>    <body>        &l ...

  2. linux定期任务cron

    做个给服务器定期检测的python程序,要python跑起来自己检测时间再执行?我想到了用cron服务. 遇到了个问题python没写绝对路径,没有执行,改了绝对路径就好了.其实人家配置文件开头写了个 ...

  3. ASP.NET MVC 音乐商店 - 6. 使用 DataAnnotations 进行模型验证

    在前面的创建专辑与编辑专辑的表单中存在一个问题:我们没有进行任何验证.字段的内容可以不输入,或者在价格的字段中输入一些字符,在执行程序的时候,这些错误会导致数据库保存过程中出现错误,我们将会看到来自数 ...

  4. SharePoint - CAML

    1. CAML是顺序操作,如果要实现类似 “A or B or C or D” 的结果,最好写成 “(((A or B) or C) or D)”的形式,但写成 “((A or B) or (C or ...

  5. CentOS安装QQ2012

    QQ想必大家都会用,在windows系统下一般都是使用客户端软件,但是在linxu系统下就没有想windows那样的客户端了.下面就次啊用wine在CentOS中安装腾讯QQ2012.

  6. Web安全入门笔记-XSS

    windows 10 360浏览器 0x00.概述 1.什么是 XSS Cross-Site Scripting(跨站脚本攻击)简称 XSS,是一种代码注入攻击.攻击者通过在目标网站上注入恶意脚本,使 ...

  7. Java NIO(六) Selector

    Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接. 下面是 ...

  8. Eclipse 各版本名称的由来

    2001年11月7日 ,Eclipse 1.0发布 半年之后,2002年6月27日Eclipse进入了2.0时代.2.0时代的Eclipse经历了2.0和2.1两个大的版本.其中2.0在 之后又推出了 ...

  9. 破解Power Designer 12.5

    PowerDesigner15.1破解补丁链接: https://pan.baidu.com/s/1boJoT6B 密码: 9x77 解压,将文件 pdflm15.dll替换安装目录的同名文件即可.

  10. hibernate基础配置

    数据库表名和类名 一致 注解:可写可不写: XML:可写可不写: <class name="Student"> 不一致 注解:  public class Teache ...