【shell脚本】if和case语句


结构化命令允许你改变程序执行的顺序

1 if-then语句

if command
then
commands
fi

$cat test.sh 
#!/bin/bash
if pwd
then
	echo "it worked"
fi

pwd的退出码是0.所以执行了then后面的命令

$./test.sh 
/home/50485581
it worked

注意:将if 和then放在同一行,要用分号隔开

$cat test.sh 
#!/bin/bash
if pwd;then
	echo "it worked"
fi

2 if-then-else语句

3 嵌套if语句

4 test命令

$cat test1.sh 
#!/bin/bash
# Testing the test command
#
my_variable="FULL"
if test $my_variable
then
	echo "The $my_varibale expression returns a True"

else
	echo "The $my_variable" expression returns a False
fi

运行:

$./test1.sh 
The  expression returns a True

修改my_variable=" ",那么就执行else

注意:test命令可以判断3类条件

  • 数值比较
  • 字符串比较
  • 文件比较
    另外bash shell也有另外一种测试条件的方法是方括号[ ]
    if [ condition ]
    then
    commands
    fi

其中第一个方括号之后和第二个方括号之前必须加上一个空格,否则就会报错。

4.1 数值比较

-gt 大于
-lt 小于
-ge 大于等于
-le 小于等于
-eq 等于
-ne 不等于

$cat test2.sh 
#!/bin/bash
# Compare
value1=10
value2=20
#
if [ $value1 -gt 5 ]
then
	echo "$value1 greater than 5"
fi
#
if [ $value1 -eq $value2 ]
then
	echo "$value1 equal $value2"
else
	echo "$value1   is different $value2"
fi
#

运行:

$./test2.sh 
10 greater than 5
10   is different 20

4.2 字符串比较

= 等于
!= 不等于
< 小于

大于
-n 长度是否非0,非0是真
-z 长度是否为0 ,0是真

1、比较2个字符串是否相等

$cat  test3.sh 
#!/bin/bash
testuser=abc
#
if [ $USER = $testuser ]
then
	echo "equal"
else
	echo "no equal"
fi
#

运行:

$./test3.sh 
no equal

2、比较两个字符串大于或者小于
注意: > 和 <被看作是重定向,如果使用到字符串中 主要转义使用\

$cat test4.sh 
#!/bin/bash
# ge and le
value1=abcd
value2=def
#
if [ $value1 \> $value2 ]
then
	echo "$value1 is greater than $value2"
else
	echo "$value1 is less than $value2"
fi
#

运行:

$./test4.sh 
abcd is less than def

3、字符串长度

#!/bin/bash
# string length
str1="abc"
str2=" "
#
if [ -n $str1 ]
then 
	echo "$str1 is not empty"
else
	echo "$str1 is empty"
fi
#
if [ -z $str2 ]
then
	echo "$str2 is empty"
else
	echo "$str2 is not empty"
fi
#

运行:

$./test5.sh 
abc is not empty
  is empty

4.3 文件比较

最强大用的最多的比较形式是文件比较
-d file 检查是否存在并是一个目录
-e file 检查是否存在
-f file 检查是否存在并是一个文件
-r file 检查是否存在并可读
-s file 检查是否存在并非空
-w file 检查是否存在并可写
-x file 检查是否存在并可执行
-O file 检查是否存在并属于当前用户
-G file 检查是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2 旧
1、检查目录
检查目录是否存在,如果存在就切换到此目录,不存在就输出提示信息

$cat test6.sh 
#!/bin/bash
# chenk directory
dir=/home/50485581/testdir
#
if  [ -d $dir ]
then
	echo "$dir  is  exists"
	cd $dir
	ls
else
	echo "$dir is not exists"
fi
#

运行:

$./test6.sh 
/home/50485581/testdir  is  exists

2、检查对象是否存在
-e
3、检查文件
-f
4、检查文件是否可读

$cat test7.sh 
#!/bin/bash
# testing if you can read a file
pwdfile=/etc/shadow
# first ,check if is a file
#
if [ -f $pwdfile ]
then
	#second, check if  read
	if [ -r $pwdfile ]
	then
		tail $pwdfile
	else
		echo "not  read"
	fi
else
	echo "not file"
fi
#

运行:

$./test7.sh 
not  read

5、检查空文件

]$cat test8.sh 
#!/bin/bash
# testing if a file is empty
file1=/home/50485581/test.txt
# testing  if  a  file
if [ -f $file1 ]
then 
	if [ -s $file1 ]
	then
		echo "$file1 is exists and data in it"
	else
		echo "$file1  is empty"
		rm  $file1
	fi	
else
	echo "$file1 is not exists or not a file"
fi

运行:

$./test8.sh 
/home/50485581/test.txt  is empty

6、检查是否可写

7、检查是否可执行

$cat test9.sh 
#!/bin/bash
#testing  file  execution
if [ -x test8.sh ]
then
	echo "you can  excute the script"
	sh test8.sh
else
	echo "no privleges"
fi

8、检查所属组关系
9、检查默认组关系
10、检查文件日期

5 复合条件

if-then允许布尔逻辑,两种布尔运算符
&& ||
与运算

$cat test10.sh 
#!/bin/bash
# && testing
if [ -d $HOME ]  && [ -r /home/50485581/nginx.log ]
then
	echo "$HOME  is a directroy  and can  read"
else
	echo "not"
fi

运行:

$./test10.sh 
/home/50485581  is a directroy  and can  read

6 if-then的高级特性

  • 用于数学表达式的双括号
  • 用于高级字符串处理功能的双方括号

6.1 双括号

双括号允许使用高级数学表达式,test是比较简单的运算
在这里插入图片描述

$cat test11.sh 
#!/bin/bash
# using double parenthesis
#
var1=10
#
if (( $var1 ** 2 > 90 ))
then
	(( var2 = $var1 ** 2 ))
	echo "The square of $var1 is $var2"
fi

运行:

$./test11.sh 
The square of 10 is 100

注意,不需要将双括号中表达式里的大于号转义。这是双括号命令提供的另一个高级特性。

6.2 双方括号

  • 双方括号用于字符串的比较
  • 双方括号可以是用正则表达是,进行模式匹配
$cat test12.sh 
#!/bin/bash
# testing double
#
if [[ $USER ==  r* ]]
then 
	echo "hello,$USER"
else
	echo "sorry"
fi	

运行:

$./test12.sh 
sorry

7 case命令

if-then不停检查同一个变量的值,case将变量与模式匹配,竖线分割成多个模式
case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac

$cat test13.sh 
#!/bin/bash
# using the case command
#
case $USER in
rich | alse)
	echo "welcome,$USER";;
testing)
	echo "testing";;
*)
	echo "sorry";;
esac

运行

$./test13.sh 
sorry

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值