shell脚本之if语句

  • 1.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 
#!/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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: if语句shell脚本中的一种条件语句,用于根据条件执行不同的命令或代码块。if语句的基本语法如下: if [ condition ] then command1 command2 ... else command3 command4 ... fi 其中,condition是一个条件表达式,可以使用各种比较运算符和逻辑运算符来组合。如果condition为真,则执行then后面的命令或代码块;否则执行else后面的命令或代码块。fi表示if语句的结束。 if语句可以嵌套使用,也可以与其他控制语句(如while、for等)结合使用,以实现更复杂的逻辑控制。 ### 回答2: Shell脚本if语句是一种条件语句,用于根据条件的真假来执行不同的操作。if语句可以通过布尔表达式来判断条件的真假。 if语句的语法如下: if [ condition ] then command1 command2 ... fi 其中,condition是一个布尔表达式,如果为真,则执行then下面的命令;如果为假,则忽略then下面的命令。command1、command2等是要执行的命令,可以是任意Shell命令。 if语句可以包含多个条件,可以使用elif和else关键字来实现。语法如下: if [ condition1 ] then command1 elif [ condition2 ] then command2 else command3 fi 其中,condition1、condition2等是多个布尔表达式,使用elif和else关键字来执行不同条件下的不同操作。如果condition1为真,则执行command1;如果condition1为假且condition2为真,则执行command2;如果condition1和condition2都为假,则执行command3。 if语句可以嵌套,即在then下面的命令中可以再次使用if语句。这样可以实现更多复杂的条件判断。 除了使用中括号[]来表示布尔表达式外,Shell还提供了test命令来实现条件判断。test命令的语法与中括号类似,可用于检测文件是否存在、文件属性等。 总之,if语句Shell脚本中非常重要的语句之一,可以根据不同的条件执行不同的操作,是Shell编程中必须掌握的内容之一。 ### 回答3: shell脚本是一种脚本语言,它被广泛地用于自动化执行系统管理员任务、批量处理数据和管理系统资源等等。在shell脚本中,if语句是一个非常重要和常用的控制结构,它用于根据某个条件的真假来执行不同的代码块。 if语句的基本语法如下: ``` if CONDITION; then STATEMENT1; elif CONDITION2; then STATEMENT2; else STATEMENT3; fi ``` 其中,CONDITION是一个逻辑表达式,它可以是一个命令的执行结果、变量是否等于某个值、两个数的比较等等。如果CONDITION为真,那么就执行STATEMENT1,否则继续检查下一个条件CONDITION2,如果CONDITION2为真就执行STATEMENT2,否则执行STATEMENT3。 以下是if语句的一些常见用法: 1. 检查一个命令是否执行成功: ``` if grep -q "searchterm" file.txt; then echo "found it!" else echo "not found" fi ``` 如果grep命令成功找到了文件中的"searchterm",则执行STATEMENT1;否则执行STATEMENT2。 2. 检查变量是否等于某个值: ``` if [ $var -eq 0 ]; then echo "var is zero" fi ``` 如果变量$var的值等于0,则执行STATEMENT1。 3. 检查文件是否存在: ``` if [ -e /path/to/file ]; then echo "file exists" else echo "file doesn't exist" fi ``` 如果文件存在,则执行STATEMENT1;否则执行STATEMENT2。 4. 检查两个数的比较: ``` if [ $a -gt $b ]; then echo "a is greater than b" elif [ $a -lt $b ]; then echo "a is less than b" else echo "a is equal to b" fi ``` 如果$a大于$b,那么执行STATEMENT1;如果$a小于$b,那么执行STATEMENT2;否则执行STATEMENT3。 总之,if语句是在shell脚本中经常使用的控制结构之一,可以帮助开发者根据某个条件的真假来执行不同的代码块,从而实现更加灵活的脚本程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值