当前位置:网站首页>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
边栏推荐
- 小迈科技 X Hologres:高可用的百亿级广告实时数仓建设
- 让 Google 搜索到自己的博客
- How QQ opens online customer service
- Detailed introduction and Simulation of bitmap
- [tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction
- Maxcompute string replacement function -replace
- You can do sideline work
- Adobe Premiere基础-炫酷文字快闪(十四)
- 3h精通OpenCV(五)-透视变换
- 给定一个数在序列中求最大异或值(01字典)
猜你喜欢

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

Adobe Premiere基础-不透明度(混合模式)(十二)

Spingmvc requests and responses

Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)

DevCloud加持下的青软,让教育“智”上云端

【目标跟踪】|stark配置 win otb

小迈科技 X Hologres:高可用的百亿级广告实时数仓建设
![[target tracking] |stark configuration win OTB](/img/29/a6b3b99b7d2349499aede9e76ab29a.png)
[target tracking] |stark configuration win OTB

Wechat applet development reserve knowledge

Let Google search your blog
随机推荐
Detailed introduction and Simulation of bitmap
Servlet学生管理系统(萌新练手版)
How QQ opens online customer service
记录服务器被入侵病毒:ssh密码被更改登录失败、恶意程序跑满了cpu、jar包启动失败自动kill、一直弹出You have new mail in /var/spool/mail/root
Spingmvc requests and responses
MySQL -connector/j driver download
Software testing - you may not understand the basic theoretical knowledge
国内酒店交易DDD应用与实践——理论篇
The soft youth under the blessing of devcloud makes education "smart" in the cloud
Adobe Premiere foundation - opacity (mixed mode) (XII)
Sd6.23 summary of intensive training
Adobe Premiere基础-批量素材导入序列-变速和倒放(回忆)-连续动作镜头切换-字幕要求(十三)
Adobe Premiere foundation - batch material import sequence - variable speed and rewind (recall) - continuous action shot switching - subtitle requirements (13)
程序员资源推荐指南
Workflow module jar package startup error: liquibase – waiting for changelog lock
lodash深拷贝使用
Adobe Premiere Basics - common video effects (corner positioning, mosaic, blur, sharpen, handwriting tools, effect control hierarchy) (16)
Programmer Resource Recommendation Guide
Adobe Premiere Basics - general operations for editing material files (offline files, replacing materials, material labels and grouping, material enabling, convenient adjustment of opacity, project pa
金鱼哥RHCA回忆录:DO447构建高级作业工作流--创建作业模板调查以设置工作的变量