1:判断文件的类型和权限:

if [ -f 1.txt ];then echo ok;fi                 判断1.txt是否是文件且存在;满足输出ok;结束

if [ -d 1.txt ];then echo ok;fi                 判断1.txt是否是目录且存在;满足输出ok;结束

判断文件的类型 :  -f 文件  -d 目录  -b 块设备  -l 软链接  -s socket文件

判断文件的权限:   -r 可读  -w 可写  -x 可执行

if [ -d /tmp ] && [ -f 1.txt ];then echo ok;fi      &&并且

if [-d /tmp -a -f 1.txt];then echo ok;fi            -a参数并且

判断/tmp是否为目录并且1.txt是否为文件,如果是,输出ok;结束

if [ -d /tmp ] || [ -f 1.txt ];then echo ok;fi      ||或者

if [-d /tmp -o -f 1.txt];then echo ok;fi            -o参数或者

判断/tmp是否为或者并且1.txt是否为文件,如果满足其中之一,输出ok;结束

2:判断字符串是否为空:   -n 字符串是否不为空     -z 字符串是否为空

例:判断用户输入是否为数字

#!/bin/bash

read -p "Please input a number" num

m=`echo $num |sed 's/[0-9]//g'`

if [ -n "$m" ]                     #如果变量$m没有双引号,不能正常判断结果。单引号也不行!

then

echo "The character you input isnot a number,please retry."

else 

echo $num

fi

判断系统中是否存在一个用户

#!/bin/bash

read -p "Please input user name:" name

if grep -q "^$name:" /etc/passwd        #变量切记用双引号,单引号不可以。-q为不输出匹配结果

then

    echo "This user is exist."

else

    echo "This user is not exist."

fi