当前位置:网站首页>Shell basic syntax -- process control
Shell basic syntax -- process control
2022-06-29 18:19:00 【__ Wanbo__】
if else sentence
The basic structure
Be careful :
ifandelifThe statement should be followed by then To start executing the statement blockthenYou can wrap , Write on the same line with a semicolon , Suggest writing on the same line , The structure will be clearer- No, else if This kind of sentence , Can only write
elif
if [ ]; then
# if Sentence block
elif [ ]; then
# Sentence block
else
# Sentence block
fi
Conditional operator
[]And[[]]The difference
[[]]Operators only havebashOnly in the middle , other shell Such as ash, dash There is no such grammar in[[]]Yes[]Features not available in , For example, compare whether two strings are equal , And there are spaces in the string , Use[]You must use double quotation marksWhile usingif [ "$str" = "hello man" ]; then[[]]There is no need to worry about this problemif [[ $str = "hello man" ]]; then- Logical operations . Use
[]Condition operator ,&&,||,<,>These symbols are used as part of the command , Not as a logical operator , Use[[]]This is not a problem with conditional operators , Such as :# Below > The symbol will be used as a redirection command , && As part of the command , Will create 200 and 100 Two documents , And the statement will report an error : [: missing `]' if [ 300 > 200 && 200 > 100 ]; then # The following statements can normally perform logical operations if [[ 300 > 200 && 200 > 100 ]]; then- Fuzzy matching . Use
[[]]You can use=~Operators and regular syntax to achieve fuzzy matching, such as matchingyes:# Use [] Only the following expressions can be used when writing if [ $str = "y" -o $str = "yes" ];then # Use [[]] You can write it as follows if [[ $str =~ ^y(es)$ ]];then echo ${ BASH_REMATCH[1]} # The matching value exists in this system variable ... # You can also use the following wildcard writing if [[ $str = y* ]];then- Experience , Generally, when making judgments, you will use
[]Conditional operator , But when comparing strings , It is best to[[]]Will reduce the probability of error
Condition identifier
Judge the numbers
if [ $a -eq $b ]; then # be equal to equal
if [ $a -ne $b ]; then # It's not equal to not equal
if [ $a -gt $b ]; then # Greater than greater than
if [ $a -ge $b ]; then # Greater than or equal to greater equal
if [ $a -lt $b ]; then # Less than little than
if [ $a -le $b ]; then # Less than or equal to little equal
Judgment string
# Use [] Operator
if [ -z "$str" ]; then # The judgment is empty ( String variables are quoted )
if [ -n "$str" ]; then # Judgment is not empty ( String variables are quoted )
if [ "$str" = "abc" ]; then # Judge equal ( String variables are quoted )
if [ "$str" == "abc" ]; then # Judge equal ( String variables are quoted )
if [ "$str" != "abc" ]; then # Judgment is not equal ( String variables are quoted )
if [ "$str1" \> "$str2" ]; then # The judgment is greater than ( String variables are quoted )
if [ "$str1" \< "$str2" ]; then # Judgment is less than
# Use [[]] Operator
if [[ -z $str ]]; then # The judgment is empty
if [[ -n $str ]]; then # Judgment is not empty
if [[ $str = "ab c" ]]; then # Judge equal
if [[ $str == "ab c" ]]; then # Judge equal
if [[ $str != "ab c" ]]; then # Judgment is not equal
if [[ $str1 > $str2 ]]; then # The judgment is greater than
if [[ $str1 < $str2 ]]; then # Judgment is less than
File to judge
if [ -e $file ]; then # Judge that the file exists
if [ -L $file ]; then # Determine whether the file exists and is a linked file ( Be the first to judge , Otherwise, you will first judge other properties of the file pointed to by the soft link )
if [ -s $file ]; then # Determine whether the file exists and is a character device
if [ -b $file ]; then # Determine whether the file exists and is a block device file
if [ -d $file ]; then # Determine whether the file exists and is a folder / Catalog
if [ -f $file ]; then # Judge whether the file exists and is a normal file
if [ -S $file ]; then # Determine whether the file exists and is a socket file
if [ -p $file ]; then # Determine whether the file exists and is a pipeline file
# Judgment of file permissions
if [ -r $file ]; then # Judge whether the file has readable permission
if [ -w $file ]; then # Judge whether the file has writable permission
if [ -x $file ]; then # Judge whether the file has executable permission
Logical operations
# Use [] when
if [ $a -o $b ];then # or or
if [ $a -a $b ];then # And and
if [ ! $a ];then # Not
if [ expression1 ] || [ expression2 ];then # or
if [ expression1 ] && [ expression2 ];then # And
if ! [ expression ] # Not
# Use [[]] when
if [[ $a && $b ]];then # or
if [[ $a && $b ]];then # And
if [[ ! $a ]];then # Not
if [[ expression1 ]] || [[ expression2 ]];then # or
if [[ expression1 ]] && [[ expression2 ]];then # And
if ! [[ expression ]] # Not
switch case choice
case ${var} in
[1-9]) # Range
# do something
;;
[^a-zA-Z]) # Not in this range
# do something
;;
*)
#do something
;;
esac
select in choice
select var in ch_a ch_b ch_c ch_d
do
# Often combine case in Statements use
done
while loop
while test
do
# The loop body
done
while true # The way to write a dead circle
do
done
for loop
for ((i=0; i<=100; i++))
do
# The loop body
done
for var # Without any parameters, all position parameters will be cycled
do
done
for in loop
for var in a b c d e
do
#
done
for var in {
1..100}
do
#
done
break And continue
shell Medium break and continue And ordinary C Keywords with the same name in a series of programs have different behaviors
break n: sign out n Layer cyclecontinue n: skip n Layer cycle , perform
边栏推荐
- Does rapid software delivery really need to be at the cost of security?
- Workflow module jar package startup error: liquibase – waiting for changelog lock
- shell教程之循环语句for,while,until用法
- 2022.6.29-----leetcode.535
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
- /usr/bin/ld: warning: **libmysqlclient.so.20**, needed by //usr/
- 小迈科技 X Hologres:高可用的百亿级广告实时数仓建设
- Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)
- 程序员资源推荐指南
- JS merge two 2D arrays and remove the same items (collation)
猜你喜欢

小迈科技 X Hologres:高可用的百亿级广告实时数仓建设

牛客小Bai月赛52 D 环上食虫(尺取+st表)

Adobe Premiere foundation - opacity (matte) (11)

Kubekey2.2.1 kubernetes1.23.7 offline package production +harbor Department summer and upload image

JS merge two 2D arrays and remove the same items (collation)

Proxmox VE Install 7.2

软件测试——基础理论知识你都不一定看得懂

Codeworks 5 questions per day (1700 for each) - the next day

MySQL -connector/j driver download

kubekey2.2.1 kubernetes1.23.7离线包制作+harbor部暑并上传镜像
随机推荐
jdbc_相关代码
Niuke small Bai monthly race 52 D ring insectivorous (feet +st table)
EasyCVR部署服务器集群时,出现一台在线一台不在线是什么原因?
Configure the local domain name through the hosts file
WBF: new method of NMS post filter frame for detection task?
idea怎么使用?
My first experience of remote office | community essay solicitation
3h精通OpenCV(七)-颜色检测
How do I add SmartArt to slides in PowerPoint?
Mysql database daily backup and scheduled cleanup script
Detailed introduction and Simulation of bitmap
markdown知识轻轻来袭
Serial port experiment based on stm32f103zet6 library function
牛客小Bai月赛52 D 环上食虫(尺取+st表)
Proxmox VE Install 7.2
字典树(随学)
[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction
Image feature computation and representation -- content based image retrieval
QQ如何开通在线客服
3H proficient in opencv (VI) - image stacking