Linux-Shell脚本编程-学习-6-Shell编程-使用结构化命令-文件比较-case编程
这一片主要说test文件的比较,文件比较在日常使用的频率比较高,这里重点把每个部分都试着说说看
1. 检车目录 -d
-d测试会检查指定的文件名是否在系统上以目录的形式存在,当我们要写文件到某个目录之前,或者是将文件放置到某个目录位置的时候,就需要使用-d来检测这个目录是否存在
#!/bin/bash #look before you leap if [ -d $HOME ]
then
echo " your Home directory exists"
cd $HOME
ls -a
else
echo " there is a problem with your HOME directory"
fi
运行截图,代码的意思就是要检测HOME这个目录是否存在,如果这个目录存在就切换到这个目录,并且执行 ls -a命令,输出HOme目录下的所有文件
2. 检查对象是否存在 -e
-e比较允许你在脚本中使用对象前检查文件或者目录是否寻再
#!/bin/bash #chesking if a directory exists if [ -e $HOME ]
then
echo "ok,on the directory.now to check the file"
if [ -e $HOME/testing ]
then
echo "addending date to existing file"
date >> $HOME/testing
else
echo "create a file"
date > $HOME/testing
fi
else
echo "sorry you do not have a Home directory"
fi这段代码的意思就是先要检测HOME文件是否存在,如果存在,那么就检测testing文件是否存在,如果存在就吧date的数据追加写到testing文件中,如果testing文件补充存在就创建testing文件,并把date数据写入到文件中
3. 检查文件 -f
确定指定的对象是个文件 -f,区分一个文件名是目录还是文件,需要使用 -f
#!/bin/bash #cheak if a file if [ -e $HOME ]
then
echo "the object exists,is it a file?"
if [ -f $HOME ]
then
echo " yes it is a file"
else
echo " no if is not a file"
if [ -f $HOME/.bash_history ]
then
echo " but this is a file"
else
echo "this is not a file too"
fi
fi
else
echo "sorry the object dose not exists"
fi exit 0
这段代码的意思就是首先检查HOME目录是否存在,如果存在,则判断home是不是一个文件,如果是就是输出这是一个文件,如果不是,就输出这不是一个文件,并且判断home下的bash_history是不是一个文件,如果是就输出是,如果不是就输出不是,最后如果HOME文件不存在,这直接执行最后一段代码,输出这个对象不存在。
4. 检查文件是否可读 -r
当我在尝试从文件中读取数据是,最好先判断一下该文件是否可读。
#!/bin/bash #testing if you can read a file pwfile=/etc/shadow if [ -f $pwfile ];then
if [ -r $pwfile ];then
tail $pwfile
else
echo " sorry o am unable to read the $pwfile"
fi
else
echo " sorry $pwfile is not a file "
fi/etc/shadow文件还有系统用户加密后的密码,所以他对系统上的普通用户是不可读的
5.检查空文件-s
当我们要删除一个文件的时候,有时候需要查看这个文件是否是一个空文件,不过这里要注意的是,-s检测到文件是控空文件的时候返回的退出码是1
#!/bin/bash #testing if a file is empty file=testfile
touch $file if [ -s $file ];then
echo "the $file file exists and has date in"
else
echo "the $file exists and is empty"
fi
date > $file
if [ -s $file ];then
echo "the $file file exists and has date in"
else
echo "the $file exists and is empty"
fi
</pre></p></blockquote></p><p></p><p>6. 检查文件是否可写 -w</p><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><p></p><p>判断是是否对该文件有写权限</p><p><pre name="code" class="plain">#!/bin/bash #check if a file is writeable logfile=$HOME/logtest
touch $logfile chmod u-w $logfile new=`date +%y%m%d-%H%M` if [ -w $logfile ];then
echo "the program ran at : $logfile" > $logfile
echo "the first attempt succeeded"
else
echo "the first attempt failes"
fi
chmod u+w $logfile if [ -w $logfile ];then
echo "the program ran at : $logfile" > $logfile
echo "the second attempt succeeded"
else
echo "the second attempt failes"
fi
7.检查文件是否可执行 -x
-x比较是一个渐变的判断一个特定文件是否有可执行的权限的方法。
#!/bin/bash #testing file execution if [ -x shelltest2.sh ];then
echo " you can run the script:"
./shelltest2.sh
else
echo " you are unable to execute the script"
fi
8. 检查文件所属关系 -O
-O比较允许你轻松的测试你是否是文件属主
#!/bin/bash #check file ownership if [ -O /etc/passwd ];then
echo " yao are the file owner"
else
echo " you are not file owner"
fi
9. 检查默认属组关系
#!/bin/bash #check file group test if [ -G $HOME/shellcode/testfile ];then
echo " yao are in the same grouup"
else
echo " the file is not owned by tour group"fi
10. 检查文件日期
#!/bin/bash #test file dates if [ $HOME/shellcode/testfile -nt $HOME/shellcode/testfile1 ];then
echo " testfile new than testfile1"
else
echo " testfile1 new than testfile"
fi
到这里,文件的test就基本介绍完了,后面的讲继续介绍
还有一部分这里就不做详细解释,大家可以自行百度学习一下
1. 复合条件测试
【】&&【】
【】||【】
2. if-then高级特性
((expression))
【【expression】】
3. case命令
case variable in
pattern1 | pattern2) commands1;;
pattern3)commands2;;
*) default commands;;
Linux-Shell脚本编程-学习-6-Shell编程-使用结构化命令-文件比较-case编程的更多相关文章
- 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令
许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then ...
- 详细介绍Linux shell脚本基础学习
Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Lin ...
- Linux shell脚本全面学习
Linux shell脚本全面学习 1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它 ...
- Linux shell脚本基础学习详细介绍(完整版)二
详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续 ...
- Linux shell脚本基础学习详细介绍(完整版)一
Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Lin ...
- Shell脚本基础学习
Shell脚本基础学习 当你在类Unix机器上编程时, 或者参与大型项目如k8s等, 某些框架和软件的安装都是使用shell脚本写的. 学会基本的shell脚本使用, 让你走上人生巅峰, 才怪. 学会 ...
- Shell脚本的学习(二)
Shell脚本的学习(二) 方法: 1) 一个计算器: 2)递归实现打印目录 3)方法调用
- Shell脚本的学习(一)
Shell脚本的学习(一) 一)代码式shell脚本简介 1.下载 Xshell 5 建一个文件夹 mkdri home/data ; 1)查看一个在data里建一个1.sh 查看是否建立成功. 2) ...
- linux shell脚本使用结构化命令
内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...
随机推荐
- 应用性能指数(APDEX)是如何计算出来的?
应用性能指数(APDEX)是如何计算出来的?在应用性能管理领域聚合指标是一种常见手段,主要是用来把成百上千的指标通过某种计算方法聚合成一个或几个指标,用来反映应用的整体健康状态.在这些聚合指标中,比较 ...
- 融云SDK:获取用户Token的方法
融云SDK查看ServerAPI里面有个获取Token的方法,本以为只要传三个参数就可以.后来发现,在请求头有几个必须要传的参数,否则服务器返回401(未授权).拿获取Token接口为例子 如图所示, ...
- 【luogu P3371 单源最短路径】 模板 SPFA
题目链接:https://www.luogu.org/problemnew/show/P3371 我永远都喜欢Flyod.dijkstra + heap.SPFA #include <cstdi ...
- Swift_数组详解
Swift_数组详解 点击查看源码 初始化 //初始化 fileprivate func testInit() { //空数组 var array = [Int]() print(array) arr ...
- 02-第一个iOS程序
第一个iOS程序 第一个iOS程序简介 初学iOS开发,研究的程序不要过于复杂,应该从最基本的开始 大房子都是由小砖一块一块堆成的,而大型app是由无数个小程序段组成的 接下来实现一个简单的“加法计算 ...
- c# 分布式系统开发
开篇吹牛,吹大牛了各位. 接连几篇博文,已经将了我们系统常用的东西,主要针对服务端,非桌面系统. 聊了这么久了,最后将这所有内容打包,完成一个系统.可能称为组件才合适,因为我没有提供启动程序. 每一个 ...
- 20181009noip HZ EZ 两校联考trade(优先队列,贪心)
题面戳这里 思路: 裸的,贪心... 考场上写了一个数据分治(70ptsDP,30pts线段树优化贪心,GG了后30分) 这道题其实很简单的 我们看图: 我们在A时刻买一个东西,在B时刻卖出去,我们可 ...
- Sql Server 查看存储过程最后修改时间
Sql Server 查看存储过程最后修改时间 select * from sys.procedures order by modify_date desc
- 03 shell编程之case语句与函数
本文所有内容均来自当年博主当年学习笔记,若有不足欢迎指正 Shell编程之case语句与函数 学习目标: 掌握case语句编程 掌握shell函数的使用 目录结构: Case语句 Case语句的作用 ...
- 云监控自定义HTTP状态码说明
您在使用站点监控时,返回的6XX状态码均为云监控自定义HTTP状态码,具体含义如下表所示: 状态码 含义 备注 610 HTTP连接超时 监测点探测您的网站时出现连接超 ...