当前位置:网站首页>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
边栏推荐
- Glitch free clock switching technology
- factory type_ Id:: create process resolution
- NIO-零拷贝
- R语言观察日志(part24)--初始化设置
- 如何高效拉齐团队认知
- 【MFC开发(17)】高级列表控件List Control
- Nacos - service discovery
- Foundation: 2 The essence of image
- What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
- Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
猜你喜欢
Computer tips
Principle and application of single chip microcomputer - principle of parallel IO port
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
Model and view of QT
"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis
个人装修笔记
Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting
明明设计的是高带宽,差点加工成开路?
Li Kou 1358 -- number of substrings containing all three characters (double pointer)
《单片机原理及应用》—定时器、串行通信和中断系统
随机推荐
Advanced C language pointer (Part 2)
FreeRTOS learning easy notes
如何做好固定资产管理?易点易动提供智能化方案
Only in China! Alicloud container service enters the Forrester leader quadrant
3、Modbus通讯协议详解
JCL and slf4j
软件工程师面试刷题网站、经验方法
Shell脚本-if else语句
Shell脚本-select in循环
Audio-AudioRecord create(一)
Nacos - service discovery
Principle and application of single chip microcomputer - principle of parallel IO port
性能提升2-3倍!百度智能云第二代昆仑芯服务器上线
【面试必刷101】链表
IT 技术电子书 收藏
基础:3.opencv快速入门图像和视频
《微机原理》-绪论
嵌入式工程师面试-常问问题集
Shell脚本-case in语句
Glitch free clock switching technology