Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Lesson 8 of my Longco operation and maintenance

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Executive structure of shell programming

Case branch selection structure:

Case word in [mode [| mode]...) Command;]. Esac

The case variable refers to in

Mode 1)

Branch 1

Mode 2)

Branch 2

...

*)

Default branch

Esac

Mode (PATTERN):

1. Ordinary text characters

2.globbing style wildcards

*: any length, any character

?: any single character

[]: any single character in the range

[^]: any single character outside the range

3. |: or

Example: prompt the user for information, and then judge whether the information entered by the user is legal or not

#! / bin/bash#read-p "Please make your choice [yes of no]:" CHOICEcase $CHOICE in yes | YES) echo "right."; NO | no) echo "wrong."; *) echo "Unknow.";; esac

The difference between the branch structure of if and the branch structure of case:

Similarities:

1. If the condition is true, execute the statement of the corresponding branch; if the condition is false, it will not be executed.

two。 You can set the default branch statement, that is, the statement executed when all the conditions do not match

Differences:

1.if judges whether it is correct or not according to the return value of the execution status of the command, while case judges whether the value content of a variable matches the pattern.

Each branch of 2.case must end with ";;"

While and until loop structure:

While

While command; do command; done

While CONDITION; do

Cyclic body

Done

Enter the cycle condition: CONDITION has always been true

Exit loop condition: CONDITION is false

Until

Until command; do command; done

Until CONDITION; do

Cyclic body

Done

Conditions for entering the cycle: CONDITION has always been false

Exit loop condition: CONDITION is false

Where: while CONDITION; do CMD; done is equivalent to until! CONDITION; do CMD; done

Note: for while and until loop structures, if you want to implement variable increment operations, you must manually give

Example: using the while and until loop structure to calculate the sum of all integers within 100

#! / bin/bash#declare-I I=1while [$I-lt 100]; do let SUM+=$I let l++doneecho $SUMFUMQUAGUBING I=1until-I I=1until [$I-I-100]; do let SUM+=$I let I++doneecho $SUM

Loop control statement:

Continue

Break

Continue:

Continue [n]

End this cycle on the nth floor ahead of time and go directly to the next round of condition judgment. If you meet the conditions for entering the cycle, you will start the next cycle.

Break:

Break [n]

End the nth layer cycle ahead of time; do not continue the subsequent cycle

Infinite loop usage:

While true; do

Cyclic body

Done

Until false; do

Cyclic body

Done

In such loop structures, continue and break must be used appropriately to ensure that the loop does not last forever.

A while loop capable of traversing

While read LINES; do

Cyclic body

Done

< /PATH/FROM/SOMEFILE until ! read LINES ; do 循环体 done < /PATH/FROM/SOMEFILE 例:文件abc.txt内容如下: 1,2,3,5,6 b,d,g,h,e 6,3,7,1,7 f,g,e,y,a 写一个脚本,利用循环结构,输出每一行的第二个和第四个字符(以逗号分隔) #!/bin/bash#while read LINES ; do echo -n $LINES | cut -d, -f2,4 | tr '\n' ','done < abc.txt select select循环只要用于创建一个菜单式列表,供用户进行选择; 列表是按照数字顺序排列的,我们只要选择数字即可; 一般来讲,select与case一起使用; seelcet是一个无限循环结构,因此,必须在循环体中使用break命令以退出循环,或者可以使用exit命令直接终止脚本运行; select NAME [in 词语 ...; ] do 命令 ; done select NAME [in LIST] ; do 命令 done 例子: #!/bin/bash#select | in meat soap rice vag ; do case $I in meat) echo "I like it." break ;; soap) echo "I don't like it." continue ;; *) exit esacdone shell脚本编程之函数 systemV风格的服务管理脚本: 给脚本传递一些参数:start,stop,restart,status 例:#ssh username@IP 把那些在脚本中重复出现并且没有任何改变的代码,封装起来,在适当的场景中调用执行; 程序员将这种被封装起来的代码称为功能提,或者叫模块; function -- 函数 在shell脚本编程中,函数是由若干条shell命令组成的语句快,通常用于代码重用和模块化封装; 函数里面的内容和shell程序形式上是一致的;不同之处就是,shell代码可以直接被执行;而函数中的内容,不能独立执行,只有被调用的时候才被执行; 函数是在shell程序的当前shell中运行的; 定义函数: 函数是由两部分组成:函数名称+函数体(能够实现独立功能的shell语句块) 语法一: function func_name{ 函数体 } 语法二: func_name(){ 函数体 } 注意:函数名和()之间不能有空白字符; 函数的使用: 函数在定义的时候,其函数体中包含的所有命令均不会被执行,只有函数被调用的时候,才会执行其中的命令语句; 调用方式:通过直接给出函数名称的方式调用; 有很多的函数是存放与专门用于保存函数的文件中,如果想要调用这样的文件中保存的函数,使用source命令(.)加载文件,然后再以直接给出函数名称的方式调用函数; 使用set命令可以查看所有当前shell中国生效的函数; 使用unset命令可以撤销已经定义的函数; 函数的返回值: 两种返回值: 函数的执行结果的返回值: 1.在函数体中使用了echo或printf命令输出结果; 2.在函数体中某些命令输出的结果; 函数的状态返回值: 1.函数中最后一跳命令的执行状态返回值; 2.自定义退出状态码; return [n] n:0-255(1 2 127尽可能不适用) 0:表示无错误返回 1-255:有错误返回 注意:只要函数在执行是,遇到了return命令,不管函数中的命令语句是否全部执行完成,立刻退出函数; 函数的声明周期: 从被调用开始,到遇到return命令或全部的语句执行完成为止; 函数的实参: 在函数体中,可以使用$1,$2,...位置变量为函数提供参数;还可以使用$*或$@的方式引用所有位置参数;还可以使用$#计算为函数传递的参数个数; 在调用函数的时候,直接在函数名称后面以空白字符分隔多个参数即可; 比如: func_name arg1 arg2 ... 例子: #!/bin/bash#addusers(){ for I in {1..5} ; do if id $1$I &>

/ dev/null |; then echo "User $1$ I exists." Return 5 else useradd $1$ I & > / dev/null echo $1$ I | passwd-- stdin $1$ I echo "Create user $1$ I funished." Fi done} addusers $1

The position parameter passed to the function is a sequence of strings separated by blank characters after the function name when the function is called; it is not the same thing as the position parameter of the script

Variables:

Variables in shell are weak variables

1. No prior declaration is required.

two。 There is no need to specify the variable type, the default is character type

Variable classification:

Environment variables: current shell and child shell

Local variable: current shell

Local variable: local VAR_NAME=VALUE current function body

Location variable:

Special variables:

Suggestion: manually destroy all variables defined or declared by yourself

Recursive call to function

Simply put, it is to call the function itself in the body of the function

Example: factorial: Numbai N* (NMur1)! = N* (NMU1) * (NMur2)! =. = N* (NMUL1) * (NMUL2) *. * 2mm 1

#! / bin/bash#fact () {if [$1-eq 0] | | [$1-eq 1]; then echo 1 else echo "$[$1cm $(fact $[$1-1])]" fi} ehco-n "$1mm =" fact $1 "

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report