shell中if语句
下面语句判断用户是否是root用户,如果是,则执行以下语句!
if [ $(id -u) != "0" ]; then
printf "Error: You must be root to run this script!\n"
exit 1
fi
shell中的if与C中的if有区别,shell中的if : 0为真,然后执行then,C是非0走then
且shell中的if不支持整数变量直接if;
必须:if [i -ne 0]
更多请参考点击打开链接
1 数值比较
n1 -eq n2 检查n1是否与n2相等
n1 -ge n2 检查n1是否大于或等于n2
n1 -gt n2 检查n1是否大于n2
n1 -le n2 检查n1是否小于或等于n2
n1 -lt n2 检查n1是否小于n2
n1 -ne n2 检查 n1是否不等于n2
2 字符串比较
str1 = str2 检查str1是否和str2相同
str1 != str2 检查str1是否和str2不同
str1 < str2 检查str1是否比str2小
str1 > str2 检查str1是否比str2大
-n str1 检查str1的长度是否非0
-z str1 检查str1长度是否为0
(注意:大于小于符号必须转义,否则shell会吧他们当做重定向符号)
例1:
#!/bin/bash
testuser=rich
if [ $USER = $testuser ];then
echo "Welcome $testuser"
fi
例2:
#!/bin/bash
val1=baseball
val2=hockey
if [ $val1 \> $val2 ];then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
val1=baseball
val2=hockey
if [ $val1 \> $val2 ];then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
例3:
#!/bin/bash
val1=testing
val2=''
if [ -n "$val1" ];then
echo "the string '$val1' is not empty"
else
echo "the string '$val1' is empty"
fi
if [ -z "val2" ];then
echo "the string '$val2' is empty"
else
echo "the string '$val2' is not empty"
fi
val1=testing
val2=''
if [ -n "$val1" ];then
echo "the string '$val1' is not empty"
else
echo "the string '$val1' is empty"
fi
if [ -z "val2" ];then
echo "the string '$val2' is empty"
else
echo "the string '$val2' is not empty"
fi
3 文件比较
-d file 检查file是否存在并是一个目录
-e file 检查file是否存在
-f file 检查file是否存在并是一个文件
-r file 检查file是否存在并可读
-s file 检查file是否存在并非空
-w file 检查file是否存在并可写
-x file 检查file是否存在并可执行
-O file 检查file是否存在并属当前用户所有
-G file 检查file是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧
例1:
#!/bin/bash
if [ -d $HONE ];then
echo "Your HOME directory exists"
cd $HOME
ls -a
else
echo "There is a problem with your HOME directory"
fi
if [ -d $HONE ];then
echo "Your HOME directory exists"
cd $HOME
ls -a
else
echo "There is a problem with your HOME directory"
fi
例2:
#!/bin/bash
# checking a file is writeable
logfile=$HOME/t16test
touch $logfile
chmod u-w $logfile
now=`date +%Y%m%d-%H%M`
if [ -w $logfile ]
then
echo "The program ran at: $now" > $logfile
echo "The first attempt succeeded"
else
echo "The first attempt failed"
fi
chmod u+w $logfile
if [ -w $logfile ]
then
echo "The program ran at: $now" > $logfile
echo "The second attempt succeeded"
else
echo "The second attempt failed"
fi
# checking a file is writeable
logfile=$HOME/t16test
touch $logfile
chmod u-w $logfile
now=`date +%Y%m%d-%H%M`
if [ -w $logfile ]
then
echo "The program ran at: $now" > $logfile
echo "The first attempt succeeded"
else
echo "The first attempt failed"
fi
chmod u+w $logfile
if [ -w $logfile ]
then
echo "The program ran at: $now" > $logfile
echo "The second attempt succeeded"
else
echo "The second attempt failed"
fi
4 复合条件测试
if-then语句允许你使用不二逻辑来组合测试。有两种布尔运算符可用
【command1】 && 【command2】
【command1】 | | 【command2】
例:
#!/bin/bash
if [ -d $HOME ] && [ -w $HOME/testing ]
then
echo "the file exits and you can write to it"
else
echo "I cannot wrote to the file"
fi
if [ -d $HOME ] && [ -w $HOME/testing ]
then
echo "the file exits and you can write to it"
else
echo "I cannot wrote to the file"
fi
5 双括号
双括号命令允许你将高级的数学表达式放入比较中,命令格式如下:
((expression))
术语expression可以是任意的数学赋值或比较表达式,除了以上test命令使用的标准运算符,还有以下:
val ++ 后增
val -- 后减
++ val 先增
-- val 先减
! 逻辑求反
~ 位求反
** 幂运算
<< 左位移
>> 右位移
& 位布尔和
| 位布尔或
&& 逻辑和
|| 逻辑或
例:
#!/bin/bash
val1=10
if (( $val1 ** 2 > 90 ))
then
(( val2 = $val1 ** 2 ))
echo "The square of $val1 is $val2"
fi
val1=10
if (( $val1 ** 2 > 90 ))
then
(( val2 = $val1 ** 2 ))
echo "The square of $val1 is $val2"
fi
6 双方括号
双方括号命令提供了针对字符串比较的高级特性。其格式如下:
[[expression]]
双方括号里的expression使用了test命令中采用的标准字符串进行比较。但它提供了test命令未提供的另一个特性--模式匹配
例:
#!/bin/bash
if [[ $USER == r* ]]
then
echo "HELLO $USER"
else
echo "Sorry,I do not know you"
fi
if [[ $USER == r* ]]
then
echo "HELLO $USER"
else
echo "Sorry,I do not know you"
fi
7 case命令
case variable in
pattern1 | pattern2) command1;;
pattern3) command2;;
*) default command;;
esac