当前位置:网站首页>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
边栏推荐
- jeecg 重启报40001
- Principle and application of single chip microcomputer - principle of parallel IO port
- Only in China! Alicloud container service enters the Forrester leader quadrant
- Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
- Shell脚本-位置参数(命令行参数)
- ARM v7的体系结构A、R、M区别,分别应用在什么领域?
- 3. Detailed explanation of Modbus communication protocol
- 【MFC开发(16)】树形控件Tree Control
- Personal decoration notes
- 串口转WIFI模块通信
猜你喜欢
![[MFC development (16)] tree control](/img/b9/1de4330c0bd186cfe062b02478c058.png)
[MFC development (16)] tree control

Share 7 books I read in the first half of 2022

Brief introduction to AES

5mo3 UHI HII HII 17mn4 19Mn6 executive standard

Computer tips

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

【MFC开发(16)】树形控件Tree Control
V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code

《单片机原理及应用》-片外拓展

Jeecg restart alarm 40001
随机推荐
vscode自定义各个区域的颜色
MATLAB【函数和图像】
Shell脚本-select in循环
MD文档中插入数学公式,Typora中插入数学公式
Screenshot tips
Redis——Lettuce连接redis集群
V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
Matlab [function derivation]
集团公司固定资产管理的痛点和解决方案
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
个人装修笔记
Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
截图小妙招
Nacos - Configuration Management
FreeRTOS学习简易笔记
分享2022上半年我读过的7本书
Personal decoration notes
日常办公耗材管理解决方案
一文纵览主流 NFT 市场平台版税、服务费设计
固定资产管理系统让企业动态掌握资产情况