shell脚本编程基础--文本比较
1.概述
允许测试Linux文件系统上文件的目录和状态。
2.详解
2.1 检查目录
-d测试会检查指定的目录是否存在于系统中。当我们打算将文件写入目录或是准备切换到该目录时,先进行测试是比较好的做法。
#!/bin/bash
#Look before you leap
#
jump_dir=/home/songzhen
#
if [ -d $jump_dir ]
then
echo "The $jump_dir directory exists"
cd $jump_dir
ls
else
echo "The $junp_dir directory does not exist"
fi
执行该脚本,结果如下:

2.2 检查对象是否存在(文件或目录)
-e比较允许你的脚本代码在使用文件或者目录前先检查它们是否存在。
#!/bin/bash
#Check if either a directory or file exists
#
location=$HOME
file_name="sentinel"
#
if [ -e $location ]
then
echo "OK on the $location directory."
echo "Now checking on the file, $file_name."
#
if [ -e $location/$file_name ]
then
echo "OK on the filename"
echo "Updating Current Date..."
date >> $location/$file_name
#
else
echo "File not exist"
echo "touch the File"
touch $file_name
fi
#
else
echo "The $location directory does not exist."
echo "Nothing to update"
fi
执行该脚本,结果如下:

2.3 检查文件
如果确定了对象为文件,必须用-f比较。
#!/bin/bash
#Check if either a directory or file exists
#
item_name=$HOME
echo
echo "The item being checked: $item_name"
echo
#
if [ -e $item_name ]
then
echo "The item, $item_name, does exist."
echo "But is it a file?"
echo
#
if [ -f $item_name ]
then
echo "Yes, $item_name is a file."
else
echo "No, $item_name is not a file."
fi
#
else
echo "The item, $item_name, does not exist."
echo "Nothing to update"
fi
执行该脚本,结果如下:
2.4 检查是否可读
在尝试从文件中读取数据之前,最好先测试一下文件是否可读,可以使用-r比较测试。
#!/bin/bash
#testing if you can read a file
pwfile=/etc/shadow
#
# first, test if the file exists, and is a file
if [ -f $pwfile ]
then
#now test if you can read it
if [ -r $pwfile ]
then
tail $pwfile
else
echo "Sorry, I am unable to read the $pwfile file"
fi
else
echo "Sorry, the file $pwfile does not exist"
fi
执行该脚本,结果如下:

2.5 检查是否可写
-w比较会判断你对文件是否有可写权限。
#!/bin/bash
#Check if either a directory or file exists
#
item_name=$HOME/sentinel
echo
echo "The item being checked: $item_name"
echo
#
if [ -e $item_name ]
then
echo "The item, $item_name, does exist."
echo "But is it a file?"
echo
#
if [ -f $item_name ]
then
echo "Yes, $item_name is a file."
echo "But is it writable?"
echo
#
if [ -w $item_name ]
then
echo "Writing current time to $item_name"
date +%H%M >> $item_name
#
else
echo "Unable to write to $item_name"
fi
#
else
echo "No, $item_name is not a file."
fi
#
else
echo "The item, $item_name, does not exist."
echo "Nothing to update"
fi
#
执行该脚本,结果如下:

2.6 检查空文件
用-s比较来检查文件是否为空,尤其是在不想删除非空文件时经常使用,-s比较成功则说明文件中有数据。
#!/bin/bash
#Testing if a file is empty
#
file_name=$HOME/sentinel
#
if [ -f $file_name ]
then
if [ -s $file_name ]
then
echo "The $file_name file exists and has data in it."
echo "will not remove this file."
else
echo "The $file_name file exists but is empty."
echo "Deleting empty file..."
rm $file_name
fi
else
echo "The $file_name does not exist."
fi
执行该脚本,结果如下:

2.7 检查文件是否可以执行
-x比较是判断特定文件是否有执行权限的一个简单办法。
#!/bin/bash
#testing file execution
#
if [ -x test9.sh ]
then
echo "You can run the script:"
else
echo "Sorry,You are unable to execute the script"]
fi
执行该脚本,结果如下:

2.8 检查所属关系
-O比较可以测试出你是否是文件的属主
#!/bin/bash
#check file ownership
#
if [ -O /etc/passwd ]
then
echo "You are the owner of the /etc/passwd file"
else
echo "Sorry, you are not the owner of the /etc/passwd file"
fi
执行该脚本,结果如下:

2.9 检查默认属组关系
-G比较会检查文件的默认组,如果它匹配了用户的默认组,则测试成功,-G比较只会检查默认组而非用户所属的所有组。
#!/bin/bash
#check file group test
#
if [ -G $HOME/testing ]
then
echo "You are in the same group as the file"
else
echo "The file is not owned by your group"
fi
执行该脚本,结果如下:

2.10 检查文件日期
-nt和-ot用来对两个文件的创建日期进行比较,在编写软件安装脚本时非常有用,但是要注意的是要先确认文件是存在的。
#!/bin/bash
#testing file dates
if [ test9.sh -nt test8.sh ]
then
echo "The test9.sh file is newer than test8.sh."
else
echo "The test8.sh file is newer than test9.sh."
fi
执行该脚本,结果如下:

shell脚本编程基础--文本比较的更多相关文章
- Linux shell脚本编程基础之练习篇
shell脚本编程基础之练习篇. 1.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. #!/bin/bash ] then echo "请输入一个参数& ...
- SHELL脚本编程基础知识
SHELL脚本编程基础知识 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Linux之父Linus有一句话很经典:"Talk is cheap, show me the ...
- shell脚本编程基础介绍
Linux系统——shell脚本编程基础介绍 1.什么是shell 它是一个命令解释器,在linux/unix操作系统的最外层,负责直接与用户对话,把用户的输入解释给操作系统,并处理各种操作输出的结果 ...
- linux基础—课堂随笔_03 SHELL脚本编程基础
shell脚本编程基础 条件选择:if语句 选择执行: 注意:if语句可嵌套 单分支 if(开头)判断条件:then条件为真的分支代码 fi(结尾) 双分支 if(开头)判断条件:then条件为真的分 ...
- shell脚本编程基础
最近学习了shell脚本编程,感觉自己的脚本写的不太好,所以想把shell脚本相关的知识系统的整理一下,便于以后的学习和使用. 一.shell脚本基础 shell脚本是利用shell的功能 ...
- Linux Shell脚本编程-基础1
概述: shell脚本在Linux系统管理员的运维工作中非常重要.shell脚本能够帮助我们很方便的管理服务器,因为我们可以指定一个任务计划,定时的去执行某一个脚本以满足我们的需求.本篇将从编程基础 ...
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---57
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- Linux基础篇–shell脚本编程基础
本章内容概要 编程基础 脚本基本格式 变量 运算 条件测试 配置用户环境 7.1 编程基础程序:指令+数据程序编程风格: 过程式:以指令为中心,数据服务于指令 对象式:以数据为中心 ...
- Shell脚本编程基础笔记一
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/8176137.html 一:脚本文件的创建.格式.运行 1:创建shell脚本 首先,要创建一个文件 touch ...
随机推荐
- CSPS模拟 71
全程傻眼 T1 毛衣衬 meet_in_middle.. 不再使用二分查找,而是直接枚举对面状态,虽然底数爆炸但是指数减半,复杂度是对的. T2 猫儿嗔 逆序关系有支配关系? $DAG$树.. 把逆序 ...
- OI 经典诗歌
键盘行 学校机房夜送客,枫叶蒟蒻秋瑟瑟.主人下马客在船,代码欲写无键盘.夜不AC惨将别,别时茫茫屏幕亮. 忽闻楼上键盘声,主人忘归客不发.寻声暗问敲者谁,键盘声停欲语迟.上楼相近邀相见,添酒回灯重开宴 ...
- Python基本数据结构之文件操作
用word操作一个文件的流程如下: 1.找到文件,双击打开 2.读或修改 3.保存&关闭 用python操作文件也差不多: f=open(filename) # 打开文件 f.write(&q ...
- 随机点名小程序--- -JAVA版本
话不多少,直接上代码 一个能够直接运行的随机点名的小程序,一个界面化的小程序.望广大网友多多支持! 1.创建一个随机点名的类 public class ProcessRandomName { JFra ...
- iOS地理反地理编码--CoreLocation
.sidebar{float:left;width:220px;} .container-fluid>.content{margin-left:240px;} a{color:#0069d6;t ...
- Redis实战--使用Jedis实现百万数据秒级插入
echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 当我们 ...
- 关于MXNet
关于人工智能,机器学习,深度学习 三者关系:从宏观到微观. 机器学习是人工智能的一部分,深度学习是机器学习的一部分. 基础:大数据. 关于深度学习 深度学习基于神经网络, 关于神经网络:通过叠加网络层 ...
- lqb 入门训练 Fibonacci数列 (循环 PS:提柜要栈溢出)
入门训练 Fibonacci数列 时间限制:1.0s 内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时, ...
- (三)初识NumPy(数据CSV文件存取和多维数据的存取)
本章主要介绍的是数据的CSV文件存取和多维数据的存取. 一.数据的CSV文件存取 1.CSV的写文件: np.savetxt(frame, array, fmt='%.18e', delimiter= ...
- ACE框架 基于共享内存的进程间通讯
ACE框架将基于共享内存的进程间通讯功能,如其它IO组件或IPC组件一样,设计成三个组件.流操作组件ACE_MEM_Stream,连接器组件ACE_MEM_Connector,以及接收连接组件ACE_ ...
