shell脚本之if语句
2018-01-26 17:25
615 查看
1.
2.
3.多个
4.注意转义,比较字符串
5.定义空,可以是等于号后面什么都没有,或者是两个单引号
6.
7.
8.
9.运算的写法
if的简单判断
[root@master shell]# cat 1.sh #!/bin/bash #testing the if statement if date;then echo "it work" fi [root@master shell]# sh 1.sh Fri Jan 26 15:54:35 CST 2018 it work
2.
if中调用变量
[root@master shell]# cat 2.sh #!/bin/bash testuser=student if grep $testuser /etc/passwd;then echo "The bash files for user $testuser are:" ls -a /home/$testuser else echo "The user name $testuser does not exits on this " fi [root@master shell]# sh 2.sh student:x:1000:1000:student:/home/student:/bin/bash The bash files for user student are: . .. .bash_logout .bash_profile .bashrc hx1 out.txt
3.多个
if条件
[root@master shell]# cat 3.sh #!/bin/bash #using numerci test comparisons VAL1=10 VAL2=11 if [ $VAL1 -gt 5 ];then echo "The test value $VAL1 is greate than 5" fi if [ $VAL1 -eq $VAL2 ];then echo "The values are equal" else echo "The values that $VAL1 and $VAL2 are different" fi [root@master shell]# sh 3.sh The test value 10 is greate than 5 The values that 10 and 11 are different [root@master shell]# cat 4.sh #!/bin/bash #testing string equality testuser=student if [ $USER = $testuser ];then echo "Welcome $testuser" else echo "Welcome $USER" fi [root@master shell]# sh 4.sh Welcome root
4.注意转义,比较字符串
[root@master shell]# cat 5.sh bc95 #!/bin/bash #mis-using string comparisons VAL1=baseball VAL2=hockey if [ $VAL1 \> $VAL2 ];then echo "$VAL1 is greater than $VAL2" else echo "$VAL1 is less than $VAL2" fi [root@master shell]# sh 5.sh baseball is less than hockey
5.定义空,可以是等于号后面什么都没有,或者是两个单引号
[root@master shell]# cat 6.sh #!/bin/bash #testing string length VAL1=testing VAL2='' if [ -n "$VAL1" ];then echo "The string '$VAL1' is not empty" else echo "The string '$VAL1' is empty" fi if [ -n "$VAL2" ];then echo "The string '$VAL2' is not empty" else echo "The string '$VAL2' is empty" fi [root@master shell]# sh 6.sh The string 'testing' is not empty The string '' is empty
6.
-d是否是目录
[root@master shell]# cat 7.sh #!/bin/bash #look before you leap if [ -d $HOME ];then echo "your HOME directory exists" cd $HOME ls -a else echo "There is a problem with your HOME directory" fi [root@master shell]# sh 7.sh your HOME directory exists . .bash_logout .mysql_history .ssh .. .bash_profile .pki .tcshrc admin-openrc .bashrc python .viminfo anaconda-ks.cfg .cshrc rhel-server-7.2-x86_64-dvd.iso .bash_history demo-openrc .rnd
7.
-e是否存在,
-f是否为文件
[root@master shell]# cat 8.sh #!/bin/bash #check if a file if [ -e $HOME ];then echo "The object exists,is it a file?" if [ -f $HOME ];then echo "Yes,it is a file!" else echo "No,it is not a file!" fi else echo "Sorry,the object does not exists!" fi [root@master shell]# sh 8.sh The object exists,is it a file? No,it is not a file!
8.
&&同时满足
[root@master shell]# cat 8.sh #!/bin/bash #check if a file if [ -e $HOME ];then echo "The object exists,is it a file?" if [ -f $HOME ];then echo "Yes,it is a file!" else echo "No,it is not a file!" fi else echo "Sorry,the object does not exists!" fi [root@master shell]# sh 9.sh I cannot write to the file.
9.运算的写法
(())
[root@master shell]# cat 10.sh #!/bin/bash #using double parenthesis VAL1=10 if (($VAL1 ** 2 > 90));then ((VAL2 = $VAL1 ** 2)) echo "The square of $VAL1 is $VAL2" fi [root@master shell]# sh 10.sh The square of 10 is 100 [root@master shell]# cat 11.sh #!/bin/bash #using pattern matching if [[ $USER == s* ]];then echo "Hello $USER" else echo "Sorry,I do not know you." fi [root@master shell]# sh 11.sh Sorry,I do not know you. [root@master shell]# su student [student@master shell]$ sh 11.sh Hello student
相关文章推荐
- shell脚本编程之for语句、if语句使用介绍
- shell脚本if与read语句用法练习
- linux之基础shell脚本编程2 if语句循环判断
- shell脚本 - 学习if语句和变量赋值
- shell脚本之if语句
- shell 脚本 if 语句
- shell 脚本if语句
- 续写vim,shell脚本基础编辑,read命令,if与case判断语句,文件查找方式,压缩与解压,
- Shell 脚本基础 - 使用 if 语句进行条件检测
- 使用if语句编写Shell脚本
- shell脚本之while for if 语句和编写计划任务
- shell脚本中的逻辑判断,文件目录属性判断,if特殊用法,case语句
- shell脚本编程之if语句学习笔记
- Shell脚本应用(if语句的结构)
- if嵌套语句 shell脚本实例 测试是否闰年
- shell脚本编程之for语句、if语句使用介绍
- shell脚本练习一(if语句、case语句、for语句、while语句)
- Linux-shell脚本-if语句/case语句
- shell脚本编程之for语句、if语句使用介绍
- shell脚本:使用if-then语句和test命令