Shell Scripting condition test

heel

Shell conditional test

Format 1: test of conditional expression
Format 2: [Conditional Expression]
Format 3: [[Conditional Expression]]

File test

[-E dir | file] test file or directory is non-existence
[-d dir] test exists, but is a directory
[-f file] test for the presence, but also the file
[-r file] test whether a file has read access
[- if x file] test file has execute permissions
[-w file] test whether a file has write access
[-L file] test whether the file is a link file
[-b file] test whether the file is a block device
[-c file] test whether a file is a character device
[-s file] test whether the file exists and is not empty
[-S file] test whether the file is a socket
[-nt file1 file2] The first file is newer than the second file
[file1 -ot file2] first the second file is older than the file
[file1 -ef file2] the first file and the second file is the same inode device

Command line usage

## 测试 /soft 目录是否存在,不存在就创建 /soft 目录
[root@Shell ~]# [ ! -d /soft ] && mkdir /soft
[root@Shell ~]# [ -d /soft ] || mkdir /soft

Scripts use

#!/usr/bin/bash
## 定义备份目录站点
dir=/soft/code
if [ ! -d $dir ];then
mkdir -p $dir
fi
ls $dir

Numerical comparison

Numerical Comparison [1 integer operator integer 2]
[1 -gt 10] is greater than
[1 -lt 10] is less than
[1 -eq 10] is equal to
[1 -ne 10] is not equal to
[1 -ge 10] or greater
[l - le 10] or less

View Disk / current use, if the usage exceeds 80% of the records in /tmp/disk_use.txta file (alarm message can be changed)

#!/usr/bin/bash
Disk_Free=$(df -h|grep "/$"|awk '{print $5}'|awk -F '%' '{print $1}')
if [ $Disk_Free -ge 80 ];then
    echo "Disk Is Use:${Disk_Free}%" > /tmp/disk_use.txt
fi

Analyzing logic

&& (- a) two conditions must be true, it is true or false
|| (-o) both conditions are false, it is false or true
! Conditions or when the command is false, the result is true

## 比较1小于2同时5大于10,两个条件必须都为真(and)
[root@Shell ~]# [ 1 -lt 2 -a 5 -gt 10 ];echo $?
[root@Shell ~]# [[ 1 -lt 2 && 5 -gt 10 ]];echo $?
## 比较1小于2同时5大于10,两个条件一个为真 (or)
[root@Shell ~]# [ 1 -lt 2 -o 5 -gt 10 ];echo $?
[root@Shell ~]# [[ 1 -lt 2 || 5 -gt 10 ]];echo $?

String comparison

= Or == equal
! = Not equal to
-z test whether the string is empty, hole True
if -n test string is not empty, non-empty true

[root@Shell ~]# [ "$USER" = "root" ];echo $?
[root@Shell ~]# [ "$USER" != "nobody" ];echo $?
## 设个空值的变量,用-z和-n测试一下
[root@Shell ~]# BBB=""
[root@Shell ~]# [ -z "$BBB" ] ;echo $?
[root@Shell ~]# [ -n "$BBB" ] ;echo $?

Regular comparison

## 判断变量是不是以 **r** 开头的
[root@Shell ~]# [[ "$USER" =~ ^r ]];echo $?
## 判断变量是不是数字
[root@bgx]# num=123
[root@bgx]# [[ "$num" =~ ^[0-9]+$ ]];echo $?

Batch create user scripts

#!/usr/bin/bash
read -p "Please input number: " num
if [[ ! "$num" =~ ^[0-9]+$ ]];then
    echo "error number!" && exit 1
fi
read -p "Please input prefix: " prefix
if [ -z "$prefix" ];then
    echo "error prefix"
    exit
fi
for i in `seq $num`
do
    user=$prefix$i
    useradd $user
    echo "123" |passwd --stdin $user &>/dev/null
     if [ $? -eq 0 ];then
        echo "$user is created."
    fi
done

Guess you like

Origin www.cnblogs.com/songguoyou/p/11884236.html