Linux in respect of such a study - flow control statements

Linux system type flow control statements

if/for/while/case

if a conditional test statement

if the type of sentence structure : [single branch / branch dual / multi-branch] Structure

if the composition of the structure of a single branch statements: if the composition of the then Fi Image

if the syntax structure of single branch statement

    if a conditional test operation

       then the command sequence

    be

if branch statement structure example single

Example 1: judge / media / cdrom file exists, if there is over conditions and judge the entire shell script, anyway, is to create the directory

 

 

 

 

 FIG operation execution result and bash mkcdrom.sh ls -d / media / cdrom command

if the composition of the sentence structure of the biantennary: if the else Fi keywords into the then

if the syntax structure of single branch statement

    if a conditional test operation

       then the command sequence 1

    else command sequence 2

    be

if configuration example bis branching statements

Example 1: Use the bifurcation if condition to verify if a host is online, then the return value according to the result, either display information online host, the host is not online or display information. Script here mainly use the ping command to test network connectivity with other hosts, and the Linux system ping command like Windows to try as 4 times on end, so as to avoid user wait too long and need -c parameterized a predetermined number of attempts, using -i parameters define each data transmission interval of the packet, and using -W parameter defines waiting timeout .

 

 

 

 Figure execute commands operating results bash chkhost.sh

( The most common branch structure) composed of a multi-branch if statement structure: if the else Fi keywords into the then elif

if grammatical structure of a multi-branch statement

    1 if condition test operation

       then the command sequence 1

    2, which test elif

       then the command sequence 2

    else command sequence 3

    be

if branch statement structure example of a multiple

例子 1 : 下面使用多分支的 if 条件语句来判断用户输入的分数在哪个成绩区间内,然后输出如 Excellent、Pass、Fail 等提示信息。在 Linux 系统中,read 是用来读取用户输入信息的命令,能够把接收到的用户输入信息赋值给后面的指定变量,-p 参数用于向用户显示一定的提示信 息。在下面的脚本示例中,只有当用户输入的分数大于等于 85 分且小于等于 100 分,才输出 Excellent 字样;若分数不满足该条件(即匹配不成功),则继续判断分数是否大于等于 70 分 且小于85 分,如果是,则输出 Pass 字样;若两次都落空(即两次的匹配操作都失败了) , 则输出 Fail 字样:

 

 

 

 

for条件循环语句

for 循环语句允许脚本一次性读取多个信息,然后逐一对信息进行操作处理,当要处理的数据 有范围时,使用 for 循环语句再适合不过了。

for语句结构的语法

for 变量名 in 取值列表

do

命令序列

done

for语句结构例子

例子 1 :

下面使用 for 循环语句从列表文件中读取多个用户名,然后为其逐一创建用户账户并设 置密码。首先创建用户名称的列表文件 users.txt,每个用户名称单独一行。读者可以自行决定具体的用户名称和个数:

接下来编写 Shell 脚本 Example.sh。在脚本中使用 read 命令读取用户输入的密码值,然 后赋值给 PASSWD 变量,并通过-p 参数向用户显示一段提示信息,告诉用户正在输入的内容 即将作为账户密码。在执行该脚本后,会自动使用从列表文件 users.txt 中获取到所有的用户 名称,然后逐一使用“id 用户名”命令查看用户的信息,并使用$?判断这条命令是否执行成 功,也就是判断该用户是否已经存在。

需要多说一句,/dev/null 是一个被称作 Linux 黑洞的文件,把输出信息重定向到这个文件等 同于删除数据(类似于没有回收功能的垃圾箱),可以让用户的屏幕窗口保持简洁。

 

 

 

 

 

注 : 执行批量创建用户的 Shell 脚本 Example.sh,在输入为账户设定的密码后将由脚本自 动检查并创建这些账户。由于已经将多余的信息通过输出重定向符转移到了/dev/null 黑洞 文件中,因此在正常情况下屏幕窗口除了“用户账户创建成功” (Create success)的提示 后不会有其他内容。

例子 2 :

尝试让脚本从文本中自动读取主机列表,然后自动逐个测试这些主机是否在线。然后前面的双分支 if 条件语句与 for 循环语句相结合,让脚本从主机列表文件 ipadds.txt 中自动读取 IP 地址(用来表示主机)并将其赋值给 HLIST 变量,从而通过判断 ping 命令执 行后的返回值来逐个测试主机是否在线。脚本中出现的$(命令)是一种完全类似于第 3 章的转义字符中反引号`命令` Shell 操作符,效果同样是执行括号或双引号括起来的字符串中的命令。大家在编写脚本时,多学习几种类似的新方法,可在工作中大显身手:

 

 

 

 

 

图 执行bash CheckHosts.sh命令的运行结果

while条件循环语句

while 条件循环语句是一种让脚本根据某些条件来重复执行命令的语句,它的循环结构往 往在执行前并不确定终执行的次数,完全不同于 for 循环语句中有目标、有范围的使用场景。while 循环语句通过判断条件测试的真假来决定是否继续执行命令,若条件为真就继续执行,为假就结束循环。

while条件循环语句语法结构

while 条件测试操作

do

命令序列

done

while语句例子 :

例子1 : 猜价格

 

 

 

 

注 :

该脚本使用$RANDOM 变量来调取出一个随机的数值(范围为 032767, 将这个随机数对 1000 进行取余操作,并使用 expr 命令取得其结果,再用这个数值与用户通过 read 命令输入的数值进行比较判断。这个判断语句分为三种情况,分别是判断用户输入的数值是等于、 大于还是小于使用 expr 命令取得的数值。当前,现在这些内容不是重点,我们当前要关注的是 while 条件循环语句中的条件测试始终为 true,因此判断语句会无限执行下去,直到用户输入的数值等于 expr 命令取得的数值后,这两者相等之后才运行 exit 0 命令,终止脚本的执行。

在这个 Guess.sh 脚本中,我们添加了一些交互式的信息,从而使得用户与系统的互动性得以增强。而且每当循环到 let TIMES++命令时都会让 TIMES 变量内的数值加 1,用来统计循环总计执行了多少次。这可以让用户得知总共猜测了多少次之后,才猜对价格。

case条件测试语句

case 语句是在多个范围内匹配数据,若匹配成功则执行相关命令并结束整个条件测试;而如果数据不在所列出的范围内,则会去执行星号(*)中所定义的默认命令.

case语句结构

case 变量值 in

模式1)

              命令序列1

              ;;

模式2}

              命令序列2

              ;;

......

*)

默认命令序列

esac

case语句结构例子

例子 1 :

我们编写脚本 Checkkeys.sh,提示用户输入一个字符并将其赋值给变量 KEY, 然后根据变量 KEY 的值向用户显示其值是字母、数字还是其他字符。

 

 

 

图为执行 bash CheckKeys.sh命令的结果

Guess you like

Origin www.cnblogs.com/studyandstudy/p/12194733.html