当前位置:网站首页>Conditional judgment if statement case statement
Conditional judgment if statement case statement
2022-07-21 04:21:00 【Oreo WM】
conditional
To make shell The script program has certain “ intelligence ”, The first problem is how to distinguish between different situations to determine what to do . for example : When the backup directory does not exist , Can automatically create ; When the source code compiles the program , If the configuration fails, the installation will not continue
1. File test –test
Use a special testing tool test command , You can test specific conditions , And judge whether the condition is true according to the return value ( The return value is 0 Indicates that the condition is true ).
Grammar format :
Format 1: test The operator Conditional expression
Format 2: [ The operator Conditional expression ] ps: Note that there should be at least one space between brackets and expressions
Common operators :
The operator describe
-d Determine whether it is a directory
-f Determine if it is a file
-e Determine whether it is a directory or file
-r Determine if you have read permission
-w Determine if there is write permission
-x Judge whether there is executive authority
-L Determine whether it is a linked file
-b Determine whether it is a device file
-c Determine whether it is a character device
-s Determine whether the file size is empty
Shell The environment returns the status value according to the command execution ($?) To judge whether the execution is successful , When the return value is 0 Time means success , otherwise ( Not 0 value ) Indicates failure or exception .
stay shell Script , We often use [] Judge
Integer comparison
stay shell The numerical value size comparison in the script does not use ><= Equal operation comparator
Integer value comparison format :[ Integers 1 The operator Integers 2 ]
Test operator :
The operator describe
-eq(equal) be equal to
-nq (not equal) It's not equal to
-lt (lesser than) Less than
-le (lesser or equal) Less than or equal to
-gt (greater than) Greater than
-ge(greater or equal) Greater than or equal to
String comparison
Format 1:[ character string 1 = character string 2 ]
Format 2:[ -z character string ]
=: String content is the same
!=: String content is different ,! Take the opposite
-z: The string content is empty
Logic test
Format 1:[ expression 1 The operator expression 2 ]
Format 2: command 1 The operator command 2
-a or &&: Logic and , And what it means
-o or ||: Logic or , Or it means
!: Logic is not , Meaning of no
IF sentence
Single branch statements
Format :
if Conditional statements
then command
fi
Two branch statements
Format :
if Conditions
then command 1
else command 2
fi
Multibranched structure
if Conditions 1
then command 1
elif Conditions 2
then command 2
else command 3
fi
measuring httpd Whether the service is installed and running :
#!/bin/bash
netstat -antup|grep ":80" &>/dev/null # Check 80 Whether the port has service listening
if [ $? -eq 0 ] # Judge the monitoring status ,0 normal , Others are abnormal
then echo "Apache Service is running !" # by 0 Then the output service starts normally
elif rpm -q httpd &>/dev/null # Check for installation httpd service
then echo "Apache The service is already installed !" # The test installation result is 0, Then the service is installed
systemctl start httpd # Start the service
echo "Apache The service is starting ......"
echo "Apache Service started successfully , Running !"
else # If the above two are not satisfied , Then the service is not installed
yum install -y httpd &>/dev/null # install httpd service
echo "Apache The service is being installed ......"
echo "Apache Service installed successfully , Starting ......"
systemctl start httpd # Opening service
echo "Apache Service started successfully , Running !"
fi
Nested statement
Sometimes you need to make multiple judgments of different types of conditions
CASE sentence
Command format
case Variable name in
Pattern 1)
command 1
;; // Indicates the end of the command
Pattern 2)
command 2
;;
*) // Default statement ,
command 3 // Don't write
esac // Reverse the ending
case in The pattern supports regular expressions , such as :
Format | describe |
---|---|
[abc5] | Express a、b、c、5 Any one of |
[m-n] | From m To n Any character of |
[0-9] | Express 0 To 9 Any number of |
丨 | Similar to or in logical operation |
such as : |
Simulation results , Let the user enter a score
100 Divided into excellent ;90-99 For excellence ;70-89 For good
60-69 Pass for ;60 The following are the failing grades ; Enter other information to prompt the user for input error
case in Statements are often used in production environments for service startup
such as :httpd Start of service
summary
1. stay shell We often use [] To judge , Rather than using test
2. Numerical comparison in judgment requires eq、gt Wait for the operator , They can't be used ><
3. In actual production if A single sentence is rarely used , The common one is if There are many double branches and multiple branches
4.case Statements are often used to start services
边栏推荐
- UGUI——ToggleGroup
- Mini simple version applet is delivered, which is exclusive to column users
- 不知道 MySQL 咋学?刷完牛客这 50 道题就够了!(第三篇)
- Sword finger offer special assault version day 3
- UGUI——RectMask2D
- TSN 802.1as clock synchronization protocol test
- 快速了解shell脚本
- 文件系统与日志分析
- Network Security Learning (Qianfeng network security notes) 2 -- IP and basic DOS commands
- What is FTP
猜你喜欢
Regular expression and shell Three Musketeers (grep, egrep, SED, and awk) tools
服务器防火墙软件—iptables
pinctrl
磁盘的配额
The new version of WordPress 6.0.1 has been released, and it is recommended to update it all.
What is rendering pipeline and how to draw 3D objects
Quickly understand shell functions and arrays
Ugui official optimization document translation
SSH远程管理及sshd服务支持验证方式
CORS跨域&JSONP回调&域名接管
随机推荐
Uni app - plugin [app cloud packaging] installation failed! (the app cloud packaging plug-in installation fails when the app is packaged) solution
Random number (random header file)
One bite of Stream(5)
剑指offer专项突击版第3天
shell脚本之for循环
McCabe measurement method to calculate program complexity
Shell function call
Uni app - solution to the failure of the return key of the mobile phone with the embedded web page physics in the app platform (after the embedded WebView web page H5 is packaged, the mobile phone's p
BeanUtils.copyProperties()和JSONObject.parseObject()是什么拷贝类型(浅拷贝或者深拷贝)
网络安全学习(千锋网络安全笔记)2--IP与基本DOS命令
Jenkins sets the language to Chinese
STM32 learning (1) basic introduction
Offline installation of Jenkins and offline installation of plug-ins
One bite of Stream(5)
Understanding and use of unity2d custom scriptable tiles (III) -- start building a custom tile based on the tile class (Part 1)
uni-app - 插件[App云打包]安装失败!(app打包时显示app云打包插件安装失败)解决方案
DHCP配置
STM32 learning (2) GPIO
Please tell me about flink1.15 flinksql Kafka topic. If you don't create a submission job in advance, it will fail
Shell Expect自动化交互