当前位置:网站首页>Shell script -if else statement

Shell script -if else statement

2022-07-01 08:48:00 Little snail's way

The simplest use is to use only if sentence , Its syntax is :

if  condition
then
    statement(s)
fi

condition It's the judgment condition , If condition establish ( return “ really ”), that then The following statement will be executed ; If condition Don't set up ( return “ false ”), Then no statement will be executed .
Be careful , Finally, it must be fi To close ,fi Namely if Spell it backwards . It's just that fi To end , So even if there are multiple statements, you don't need to use { } Surrounded .

Code 1 Compare the size of two numbers

#!/bin/bash
read a
read b
if (( $a == $b ))
then
    echo "a and b equal "
fi

Output :

84
84
a and b equal 

(()) It is a mathematical calculation command , In addition to the most basic addition, subtraction, multiplication and division , You can also do more than 、 Less than 、 Equal to equal relation operation , As well as 、 or 、 Illogical operation . When a and b When equal ,(( $a == $b )) The judgment is true , Get into if, perform then The back echo sentence .

Code 2

#!/bin/bash
read age
read iq
if (( $age > 18 && $iq < 60 ))
then
    echo " You're all grown up , Why did you fail in IQ !"
    echo " Keep learning , Can quickly improve your IQ ."
fi

Output :

20
56
 You're all grown up , Why did you fail in IQ !
 Keep learning , Can quickly improve your IQ ..

&& Is the logic “ And ” Operator , Only when && The judgment conditions on both sides are “ really ” when , The whole judgment condition is “ really ”.

Readers familiar with other programming languages should note , Even if then There are multiple statements behind , You don't need to use { } Surrounded , Because there is fi The end .

if else sentence

Code

#!/bin/bash
read a
read b
if (( $a == $b ))
then
    echo "a and b equal "
else
    echo "a and b It's not equal , Input error "
fi

Output :

10
20
a  and  b  It's not equal , Input error 

As you can see from the results ,a and b It's not equal , The judgment is not true , So it's implemented else The following sentence .

if elif else sentence

Be careful ,if and elif You have to follow behind then.

Code : Enter the age , Output the corresponding stage of life :

#!/bin/bash
read age
if (( $age <= 2 )); then
    echo " baby "
elif (( $age >= 3 && $age <= 8 )); then
    echo " Young children "
elif (( $age >= 9 && $age <= 17 )); then
    echo " juvenile "
elif (( $age >= 18 && $age <=25 )); then
    echo " adult "
elif (( $age >= 26 && $age <= 40 )); then
    echo " youth "
elif (( $age >= 41 && $age <= 60 )); then
    echo " middle-aged "
else
    echo " The elderly "
fi

Output :

19
 adult 

100
 The elderly 

Code 2: Enter an integer , Output the English expression of the day of the week corresponding to the integer :

#!/bin/bash
printf "Input integer number: "
read num
if ((num==1)); then
    echo "Monday"
elif ((num==2)); then
    echo "Tuesday"
elif ((num==3)); then
    echo "Wednesday"
elif ((num==4)); then
    echo "Thursday"
elif ((num==5)); then
    echo "Friday"
elif ((num==6)); then
    echo "Saturday"
elif ((num==7)); then
    echo "Sunday"
else
    echo "error"
fi

Output :

Input integer number: 4
Thursday

 Running results 2:
Input integer number: 9
error
原网站

版权声明
本文为[Little snail's way]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010835460450.html