shell script测试命令(test)
shell script测试命令(test)
test命令
检查系统上面某些文件或者相关的属性
常用选项
- test -e :检查该文件名是否存在
例:检查/dmtsai是否存在
[root@localhost scripts1]# test -e dmtsai && echo "exist" || echo "not exist"
not exist
注:检查系统中是否存在 dmtsai这个文件,存在则输出exist不存在则输出not exist
- test -f :该文件名是否存在且为文件
例:检查scripts1是否存在且是否为文件
[root@localhost scripts1]# test -f scripts1 && echo "exist" || echo "not exist"
exist
注:检查系统中是否存在 scripts1这个文件,存在则输出exist不存在则输出not exist
- test -d :该文件名是否存在且为目录
例:检查/etc是否为目录
[root@localhost scripts1]# test -d /etc && echo "exist"|| echo "not exist"
exist
注:检查系统中是否存在 /etc这个文件,存在则输出exist不存在则输出not exist
针对文件的权限检查的常规选项
- test -r :检测文件是否存在且具有“可读”权限(单root权限常有例外)
例:检查根下的家目录中是file是否存在且有可读的权限
[root@station40 ~]# ll
total 24
-rw-------. 1 root root 953 Apr 15 05:16 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Apr 15 05:34 file==
-rw-r--r--. 1 root root 9615 Apr 15 05:16 install.log
-rw-r--r--. 1 root root 3161 Apr 15 05:15 install.log.syslog
-rw-r--r--. 2 root root 0 Apr 16 05:44 lijain
-r--r--r--. 1 root root 95 Apr 15 07:46 lijian.sh
lrwxrwxrwx. 1 root root 6 Apr 16 05:46 lijian.sh1 -> lijain
-rw-r--r--. 2 root root 0 Apr 16 05:44 lijian.sh2
[root@station40 ~]# test -w lijain.sh || echo write
注:考虑到一个文件有其属主和属组还有其他三类权限用户,所以验证test的权限检查选项是否有针对三种用户有不同。可以看出file这个三种用户都有可读权限.
[root@station40 ~]# test -r file && echo read || echo not read
read
现在把属主的读权限去掉
[root@station40 ~]# chmod u-r file
[root@station40 ~]# ll
total 24
-rw-------. 1 root root 953 Apr 15 05:16 anaconda-ks.cfg
--w-r--r--. 1 root root 0 Apr 15 05:34 file
-rw-r--r--. 1 root root 9615 Apr 15 05:16 install.log
-rw-r--r--. 1 root root 3161 Apr 15 05:15 install.log.syslog
-rw-r--r--. 2 root root 0 Apr 16 05:44 lijain
-r--r--r--. 1 root root 95 Apr 15 07:46 lijian.sh
lrwxrwxrwx. 1 root root 6 Apr 16 05:46 lijian.sh1 -> lijain
-rw-r--r--. 2 root root 0 Apr 16 05:44 lijian.sh2
再次检查file文件的可读权限
[root@station40 ~]# test -r file && echo read || echo not read
read
可以知道test权限的检查与属主的权限决定,所以接下来可以依次来验证(属组和其他用户)都可以得出test的权限查看是三类用户只要有一类具有某种权限就认定该文件具有该权限。
[root@station40 ~]# chmod u-r,g-r,o-r file
[root@station40 ~]# ll
total 24
-rw-------. 1 root root 953 Apr 15 05:16 anaconda-ks.cfg
--w-------. 1 root root 0 Apr 15 05:34 file
-rw-r--r--. 1 root root 9615 Apr 15 05:16 install.log
-rw-r--r--. 1 root root 3161 Apr 15 05:15 install.log.syslog
-rw-r--r--. 2 root root 0 Apr 16 05:44 lijain
-r--r--r--. 1 root root 95 Apr 15 07:46 lijian.sh
lrwxrwxrwx. 1 root root 6 Apr 16 05:46 lijian.sh1 -> lijain
-rw-r--r--. 2 root root 0 Apr 16 05:44 lijian.sh2
现在把三类用户的可读权限全部去掉在使用test检查该文件的可读权限,分析结果应该是输出 not read
[root@station40 ~]# test -r file && echo read || echo not read
read
从上述结果可以看出与猜想的结果存在差异。这就是归结为test该命令的特性。因为刚才实行命令都是在root的权限下实现,所以现在切换至普通用户再进行检测file的可读性
[lijian@station40 root]$ test -r file && echo read || echo not read
not read
在切换至普通用户后再对file这个文件进行test -r的检测显示结果为不可读。所以同理test的其他检查权限的选项都类似。
整数数值的比较(test n1 option n2)
- test n1 -eq n2 比较两个整数数值是否相等
[lijian@station40 root]$ test 4 -eq 5 || echo error
error
[lijian@station40 root]$ test 5 -eq 5 && echo correct|| echo error
correct
上面的验证可以看出test在整数之间的大小比较用法
- test n1 -gt n2比较n1是否大于n2
[lijian@station40 root]$ test 5 -gt 2 && echo greater than|| echo error
greater than
[lijian@station40 root]$ test 0 -gt 2 && echo greater than|| echo error
error
对于数值比较方式方法都与上述较为类似故不做具体陈述
判定字符串的数据
- Test -z string判定字符串是否为0,若string为空字符串,则为true
[lijian@station40 root]$ test -z jjj && echo correct || echo error
error
[lijian@station40 root]$ test -z && echo correct || echo error
correct
可以看出对于非空字符和空字符都可以检查出来
多重条件判定
- test -r file -a -x file(文件同时具有可读可执行权限时为正确)
[root@station40 ~]# ll
total 24
-rw-------. 1 root root 953 Apr 15 05:16 anaconda-ks.cfg
--w-------. 1 root root 0 Apr 15 05:34 file
-rw-r--r--. 1 root root 9615 Apr 15 05:16 install.log
-rw-r--r--. 1 root root 3161 Apr 15 05:15 install.log.syslog
-rw-r--r--. 2 root root 0 Apr 16 05:44 lijain
-r--r--r--. 1 root root 95 Apr 15 07:46 lijian.sh
lrwxrwxrwx. 1 root root 6 Apr 16 05:46 lijian.sh1 -> lijain
-rw-r--r--. 2 root root 0 Apr 16 05:44 lijian.sh2
[root@station40 ~]# test -r lijain -a -r lijain
[root@station40 ~]# echo $?
0
可以看出lijain这个文件是具有可读可写权限的所以-a就可以回传正确。反之如下
[lijian@station40 root]$ test -w file -a -r file
[lijian@station40 root]$ echo $?
1
shell script测试命令(test)的更多相关文章
- shell及脚本4——shell script
一.格式 1.1 开头 必须以 "# !/bin/bash" 开头,告诉系统这是一个bash shell脚本.注意#与!中间有空格. 二.语法 2.1 数值运算 可以用decla ...
- shell script
一.shell script的编写与执行 1.shell script 的编写中还需要用到下面的注意事项: a.命令的执行是从上到下,从左到右地分析与执行 b.命令.参数间的多个空白都会被忽略掉 c. ...
- (copy) Shell Script to Check Linux System Health
source: http://linoxide.com/linux-shell-script/shell-script-check-linux-system-health/ This article ...
- shell script练习
执行脚本的几种方式: 1. sh a.sh 或者 bash a.sh 调用的是 /bin/bash 进程执行的,所以脚本不需要执行权限. 2. 直接使用绝对路径执行, /home/script/a ...
- 这些年我们一起搞过的持续集成~Jenkins+Perl and Shell script
这些年我们一起搞过的持续集成~Jenkins+Perl and Shell script ##转载注明出处:http://www.cnblogs.com/wade-xu/p/4378224.html ...
- CentOS Linux下一个tomcat起停,查看日志的shell script
CentOS 的tomcat安装目录:/usr/local/tomcat vi MyTomcatUitl.sh 创建文件chmod u+x MyTomcatUtil.sh 赋执行 ...
- Shell script for logging cpu and memory usage of a Linux process
Shell script for logging cpu and memory usage of a Linux process http://www.unix.com/shell-programmi ...
- shell script入门
从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...
- shell script 的追踪与 debug
shell script 的追踪与 debug scripts 在运行之前,最怕的就是出现语法错误的问题了!那么我们如何 debug 呢?有没有办法不需要透过直接运行该 scripts 就可以来判断是 ...
随机推荐
- 201521123001《Java程序设计》第4周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 答:1.被继承的类称为父类,继承父类的类称为子类 2.继承时子类将获得父类的属性与方法,并具 ...
- 201521123092,《java程序设计》第1周学习总结
1.本周学习总结 这一周是我学习java的第一周,刚接触一门全新的编程语言,觉得还是有点困难的,很多基础性的java知识需要一点点学习,我会请教同学以及查询网上的学习资料,认真学好这一门学科. 本周学 ...
- Java 第十一周总结
1. 本周学习总结 2. 书面作业 1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了使用synchronized修饰方法实现互斥同步访问,还有什么办法实现互斥同步访 ...
- C#中if_else以及for循环的简单理解
if_else语句的语法: if(判断条件) { 执行语句 }else { 执行语句 } 判断条件位true执行if大括号的语句,false执行else大括号的语句. if_else的扩展: 连续判断 ...
- 小巧玲珑:机器学习届快刀XGBoost的介绍和使用
欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:张萌 序言 XGBoost效率很高,在Kaggle等诸多比赛中使用广泛,并且取得了不少好成绩.为了让公司的算法工程师,可以更加方便的 ...
- 假设我的朋友账号分别是v{1,2,3,4,5},且这五人想要共享一个目录,因此应该加入同一个群组,假设这个群组为vbird,且这五个账号的密码均为password.那该如何建置这五个账号?
假设我的朋友账号分别是v{1,2,3,4,5},且这五人想要共享一个目录,因此应该加入同一个群组,假设这个群组为vbird,且这五个账号的密码均为password.那该如何建置这五个账号?#!/bin ...
- 【Spring】Spring的bean装配
前言 bean是Spring最基础最核心的部分,Spring简化代码主要是依赖于bean,下面学习Spring中如何装配bean. 装配bean Spring在装配bean时非常灵活,其提供了三种方式 ...
- SAP Gateway简介
SAP Gateway在S4/HANA时代的ABAP开发模型中有着重要的地位.SAP Gateway是什么?它对ABAP开发有怎样的影响?可以为我们提供哪些方便?这篇译文将浅要地讨论这些话题. SAP ...
- Go语言备忘录:基本数据结构
本文内容是本人对Go语言的变量.常量.数组.切片.映射.结构体的备忘录,记录了关键的相关知识点,以供翻查. 文中如有错误的地方请大家指出,以免误导!转摘本文也请注明出处,多谢! 参考书籍<Go语 ...
- Ubuntu16.04 Using Note
I meet lots of problems when i installed and use ubuntu 16.04.below is my using note: (my operating ...