当前位置:网站首页>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 - service discovery
- Matlab [function derivation]
- 你了解数据是如何存储的吗?(C整型和浮点型两类)
- Matlab [functions and images]
- 基础:2.图像的本质
- Dynamic proxy
- Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling
- It is designed with high bandwidth, which is almost processed into an open circuit?
- Configuration and startup of Chang'an chain synchronization node
猜你喜欢
How can enterprises and developers take the lead in the outbreak of cloud native landing?
【C】 Summary of wrong questions in winter vacation
《微机原理》——微处理器内部及外部结构
3、Modbus通讯协议详解
Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
《微机原理》—总线及其形成
C语言指针的进阶(上篇)
中小企业固定资产管理办法哪种好?
Foundation: 2 The essence of image
Audio audiorecord create (I)
随机推荐
DataBinding源码分析
易点易动助力企业设备高效管理,提升设备利用率
Bimianhongfu queren()
1.jetson与摄像头的对接
"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis
3. Detailed explanation of Modbus communication protocol
集团公司固定资产管理的痛点和解决方案
你了解数据是如何存储的吗?(C整型和浮点型两类)
Common interview questions for embedded engineers 2-mcu_ STM32
又到年中,固定资产管理该何去何从?
It is designed with high bandwidth, which is almost processed into an open circuit?
Personal decoration notes
Graduation season, I want to tell you
factory type_ Id:: create process resolution
Shell脚本-变量的定义、赋值和删除
Brief introduction to AES
《单片机原理及应用》-片外拓展
Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition
TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘