当前位置:网站首页>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
边栏推荐
猜你喜欢

Nacos - Configuration Management

What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR

Advanced C language pointer (Part 2)

Embedded Engineer Interview Question 3 Hardware

基础:2.图像的本质

《单片机原理及应用》—定时器、串行通信和中断系统

Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process

Bimianhongfu queren()

为什么LTD独立站就是Web3.0网站!

电视机尺寸与观看距离
随机推荐
公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度
Nacos - 配置管理
MATLAB【函数和图像】
Differences among tasks, threads and processes
C语言指针的进阶(下)
为什么LTD独立站就是Web3.0网站!
Shell脚本-for循环和for int循环
Nacos - 配置管理
LogBack
如何做好固定资产管理?易点易动提供智能化方案
View drawing process analysis
Advanced C language pointer (Part 2)
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
通过 代码实例 理解 浅复制 与 深复制
[MFC development (16)] tree control
What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR
Jeecg restart alarm 40001
AVL树的理解和实现
《单片机原理及应用》—定时器、串行通信和中断系统
目标检测的yolov3、4、5、6总结