shell脚本之条件测试

组合条件测试:在多个条件间实现逻辑运算

    与:[ condition1 -a condition2 ]、condition1 && condition2
    或:[ condition1 -o condition2 ]、condition1 || condition2
    非:[ -not condition ]、[ ! condition ]

1.整型测试(整数比较)

运算符                    描述              示例    
num1 -gt num2             大于              [ $num -gt 100 ]    
num1 -lt num2             小于              [ $num -lt 100 ]   
num1 -ge num2             大于等于          [ $num -ge 100 ]    
num1 -le num2             小于等于          [ $num -le 100 ]    
num1 -eq num2             等于              [ $num -eq 100 ]    
num1 -ne num2             不等于            [ $num -ne 100 ]

2.字符测试(字符串比较)

运算符                描述                                示例    
                                                          test  "${String1}" \> "${String2}" 
String1 > String2     如果字String1大于String2,则为真    [ "${String1}" \> "${String2}" ]
                                                          [[ "${String1}" > "${String2}" ]]   
                                                           
                                                          test  "${String1}" \< "${String2}" 
String1 < String2     如果字String1小于String2,则为真    [ "${String1}" \< "${String2}" ]
                                                          [[ "${String1}" < "${String2}" ]]
                                                          
                                                          test  "${String1}" =="${String2}"   
String1 == String2    如果字String1等于String2,则为真    [ "${String1}" =="${String2}" ]
                                                          [[ "${String1}" =="${String2}" ]]
                                                          
                                                          test  "${String1}" !="${String2}" 
String1 != String2    如果字String1不等于String2,则为真  [ "${String1}" !="${String2}" ]
                                                          [[ "${String1}" !="${String2}" ]]
       
-n String             如果String长度为零,则为真          [ -n "${myvar}"]    
-z String             如果String长度为非零,则为真        [ -z "${myvar}" ] 
   
注意:
字符串必须用单引号或双引号引起来,有变量必须用双引号。
test与[]是shell命令,所以字符串比较操作符“<"、">"必须转义。

3.文件测试(判断文件是否存在及其属性)

参数  描述                                   示例
    
-e    文档存在则为真,同-a                   [[ -e /tmp ]]   
-f    文档存在且为普通文件则为真             [[ -f /etc/passwd ]]  
-d    文档存在且为目录则为真                 [[ -d /tmp ]]    
-L    文档存在且为符号链接为件则为真,同h    [[ -L /dev/stdin ]]    
-b    文档存在且为块设备则为真               [[ -b /dev/xvda ]]    
-c    文档存在且为字符设备则为真             [[ -c /dev/zero ]]    
-S    文档存在且为套接字文件则为真           [[ -S /dev/log ]]   
-p    文档存在且为命令管道则为真             [[ -p /var/run/autofs.fifo-net ]]   
-s    文档存在且为非空文件则为真             [[ -s /etc/passwd ]]   
 
-r    文档存在且具有可读权限则为真           [[ -r /etc/passwd ]]   
-w    文档存在且具有可写权限则为真           [[ -w /etc/passwd ]]    
-x    文档存在且具有可执行权限则为真         [[ -x /etc/init.d/dhcpd ]]    
-u    文档存在且具有SUID属性则为真           [[ -u a.txt ]]    
-g    文档存在且具有SGID属性则为真           [[ -g a.txt ]]
    
-nt    file1的mtime新于file2则为真           [[ a.txt -nt b.txt ]]    
-ot    file1的mtime旧于file2则为真           [[ a.txt -ot b.txt ]]

综合示例:

例1:如果当前主机名为空或者为“localhost”,则将其修改为chenss

   #!/bin/bash
   #
   host_name=$(hostname)
   [ -z "${host_name}" -o "${host_name}" == "localhost" ] && hostname chenss

例2:执行脚本,提示输入用户名,如果无此用户,则报错且继续提示输入用户名;如果UID等于0,则提示是root用户;如果大于0小于500,则提示是系统用户;否则,提示是普通用户。

  #!/bin/bash
  #
  until  [ "${user_name}" == "q" -o "${user_name}" == "quit" ] ; do
  
  read -p "PLS enter a username: " user_name
  user_id=$(id -u ${user_name})
  
  if ! id -u ${user_name} &> /dev/null; then
       continue
  elif [[ ${user_id} -eq 0 ]] ; then
       echo "It's a root user."
  elif [ ${user_id} -gt 0 -a ${user_id} -lt 500 ]; then
       echo "It's a system user."
  else
       echo "It's a common user."
  fi
  done

例3:添加10个用户,如果用户存在,则显示有此用户并把用户名标红,如果不存在,则添加并把用户名背景色标绿,且显示一共添加了多少用户。

  #!/bin/bash
  #
  declare -i num=0
  for i in {1..10}; do
       if id -u test$i &> /dev/null; then
             echo -e "user 33[0;31;1mtest$i33[0m is exists."
       else
             useradd test$i && echo -e "33[0;42;1mtest$i33[0m add succesfully."
             let num++
       fi
  done
  echo "Total add $num users."

原创文章,作者:老鼠上了猫,如若转载,请注明出处:http://www.178linux.com/7166

(1)
老鼠上了猫老鼠上了猫
上一篇 2015-08-24 18:27
下一篇 2015-08-24 21:28

相关推荐

  • 第四周作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限;          [root@yangjifeng~]# cp -a /etc/skel /home/tuser1 [root@yangjife…

    Linux干货 2017-08-28
  • 关于Raid的各种类型特点概要

    关于Raid需要了解掌握的要点

    Linux干货 2017-11-26
  • 五种开源协议的比较(BSD,GPL,LGPL,Apache,BSD)

    BSD开源协议(original BSD license、FreeBSD license、Original BSD license) BSD开源协议是一个给于使用者很大自由的协议。基本上使用者可以”为所欲为”,可以自由的使用,修改源代码,也可以将修改后的代码作为开源或者专有软件再发布。 但”为所欲为”的前提当你发布使用了BSD协议的代码,或则以BSD协议代码…

    Linux干货 2016-01-16
  • 解决CentOS SSH 连接慢

    1、关闭DNS反向解析在linux中,默认就是开启了SSH的反向DNS解析,这个会消耗大量时间,因此需要关闭。配置文件路径 vim /etc/ssh/sshd_configUseDNS=no 在配置文件中,虽然UseDNS yes是被注释的,但默认开关就是yes 2、关闭SERVER上的GSS认证在authentication gssapi-with-mic…

    Linux干货 2018-01-10
  • linux启动和内核管理

    linux启动和内核管理:1. 加载BIOS 的硬件信息,获取第一个启动设备2. 读取第一个启动设备MBR 的引导加载程序(grub) 的启动信息3. 加载核心操作系统的核心信息,核心开始解压缩,并尝试驱动所有的硬件设备4. 核心执行init 程序,并获取默认的运行信息5.init 程序执行/etc/rc.d/rc.sysinit 文件6. 启动核心的外挂模…

    Linux干货 2017-03-28
  • CentOS 7 tomcat 7.0.54 的功能实现及详解

    一、 jdk 安装配置 # yum install java-1.8.0-openjdk-devel (依赖的java-1.8.0-openjdk,java-1.8.0-openjdk,headless也会被安装 ) # alternatives -h # vim /e…

    Linux干货 2014-06-09