条件测试形式
格式一:test 条件测试
格式二:[ 条件测试 ]
格式三:[[条件测试]]
条件测试的比较的对象:文件、整数、字符串
输入指令:man test查看条件测试文档
文档如下:
(可总结一小部分为 字符串比较可以用=、!=。整数的比较要用 -eq、-ne)
( EXPRESSION )
EXPRESSION is true
! EXPRESSION
EXPRESSION is false
EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING
the length of STRING is zero
STRING1 = STRING2
the strings are equal
STRING1 != STRING2
the strings are not equal
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 is older than FILE2
-b FILE
FILE exists and is block special 文件存在并且是一个块设备(linux下的设备是用文件表示的)
-c FILE
FILE exists and is character special文件存在并且是字符设备
-d FILE
FILE exists and is a directory
-e FILE
FILE exists 文件存在
-f FILE
FILE exists and is a regular file 文件存在且是常规文件
-g FILE
FILE exists and is set-group-ID 文件存在且是被设置了特殊权限
-G FILE
FILE exists and is owned by the effective group ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe 文件存在 且是管道文件
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute (or search) permission is granted
比如判断某个目录是否存在
back_dir=/var/mmy
if[ ! -d $back_dir ];then 方括号内表示判断 back_di是否是一个目录,不是则创建。!表示对判断结果取反
mkdir -p $back_dir
fi
磁盘使用量警告脚本例子
vim disk_use.sh
date --help 查看提取时间
#!/usr/bin/bash
NF表示倒数第一列
disk_use=`df -Th |grep '/$' |awk '{print $(NF-1)}' |awk -F"%" '{print $1}'`
if[ $disk_use -ge 90 ];then
df 查看磁盘分区
grep ‘mem’ 搜索含mem的行
awk 将输入的内容一行一行取出进行操作
字符串比较尽量用" "框起来 即便是字符串变量 也请用双引号框起来
seq指令
输入按套路出牌 创建用户
#!/usr/bin/bash
read -p "Please input number:" num
read -p "Please input prefix:" prefix
for i in `seq $num` 这个seq的作业就是生成1~num个数
do
user=$prefix$i
useradd $user
echo "123" |passwd --stdin $user &>/dev/null
if[ $? -eq 0 ];then
echo "$user is created."
fi
done
创建文件夹:
#!/usr/bin/bash
read -p "dir name:" name
read -p "dir num:" num
for i in `seq $num`
do
mkdir ${name}ppl${i}
if [ $? -eq 0 ];then
echo "${name}ppl${i} is created."
fi
done
shell变量定义的都是字符串,如何判断是字符串还是数字是根据用的指令 比如用let 变量那就是数字了
判断输入是不是数字
read num
if [[ $num != ~^[0-9]+$ ]];then
echo "not a number"
exit
fi