商丘哪里做网站,建工社网校官网,福州建设工程招投标信息网,安徽城乡建设厅网站证件结构化命令
if语句
if-then
最基本的结构化命令是 if-then 语句。if-then 语句的格式如下#xff1a; if command
thencommands
ifif command; then # 通过把分号#xff08;;#xff09;放在待求值的命令尾部#xff0c;可以将 then 语句写在同一行commands
ifbash sh…结构化命令
if语句
if-then
最基本的结构化命令是 if-then 语句。if-then 语句的格式如下 if command
thencommands
ifif command; then # 通过把分号;放在待求值的命令尾部可以将 then 语句写在同一行commands
ifbash shell 的 if 语句会运行 if 之后的命令。如果该命令的退出状态码参见第 11 章为 0命令成功运行那么位于 then 部分的命令就会被执行。如果该命令的退出状态码是其他值则 then 部分的命令不会被执行bash shell 会接着处理脚本中的下一条命令。fi 语句用来表示if-then 语句到此结束。 #!/bin/bash
# testing the if statement
if pwd # if 行中的 pwd 命令由于退出状态码是 0会执行if-then 内部的语句
thenecho heheh
if#!/bin/bash
# testing an incorrect command
if IamNotCommand # if 行中的 IamNotCommand 命令由于命令不存在不会执行if-then 内部的语句
thenecho heheh
ifif-then-else
if-then-else 语句在语句中提供了另外一组命令 if command
thencommands
elsecommands
if#!/bin/bash
# testing the else section
#
testuserNoSuchUser
#
if grep $testuser /etc/passwd
thenecho The script files in the home directory of $testuser are:ls /home/$testuser/*.shecho
elseecho The user $testuser does not exist on this system.echo
fi
echo We are outside the if statementelif 语句行提供了另一个要测试的命令这类似于原始的 if 语句行。 if command1
then commands
elif command2
thenmore commands
fi test命令
test 命令可以在 if-then 语句中测试不同的条件。如果 test 命令中列出的条件成立那么test 命令就会退出并返回退出状态码 0。如果条件不成立那么 test 命令就会退出并返回非 0 的状态码这使得 if-then 语句不会再被执行。
test 命令的格式非常简单 test condition
if test condition
thencommands
fi如果不写 test 命令的 condition 部分则它会以非 0 的退出状态码退出。 使用 test 命令确定变量中是否为空 #!/bin/bash
# testing if a variable has content
#
my_variableFull
#
if test $my_variable # my_variable不是空返回状态0
thenecho The my_variable variable has content and returns a True.echo The my_variable variable content is: $my_variable
elseecho The my_variable variable doesnt have content,echo and returns a False.
fibash shell 提供了另一种条件测试方式无须在 if-then 语句中写明 test 命令 if [ condition ]
then commands
fi方括号定义了测试条件。注意第一个方括号之后和第二个方括号之前必须留有空格否则就会报错。
test 命令和测试条件可以判断 3 类条件 数值比较注意不支持浮点数只能用于整数 字符串比较 文件比较
数值比较
比较描述n1 -eq n2检查 n1 是否等于 n2n1 -ge n2检查 n1 是否大于或等于 n2n1 -gt n2检查 n1 是否大于 n2n1 -le n2检查 n1 是否小于或等于 n2n1 -lt n2检查 n1 是否小于 n2n1 -ne n2检查 n1 是否不等于 n2 #!/bin/bash
# Using numeric test evaluations
#
value110
value211
#
if [ $value1 -gt 5 ]
thenecho The test value $value1 is greater than 5.
fi
#
if [ $value1 -eq $value2 ]
thenecho The values are equal.
elseecho The values are different.
fi字符串比较
比较描述str1 str2检查 str1 是否和 str2 相同str1 str2检查 str1 是否大于 str2str1 str2检查 str1 是否小于 str2str1 ! str2检查 str1 是否和 str2 不同-n str1检查 str1 的长度是否不为 0-z str1检查 str1 的长度是否为 0 #!/bin/bash
# Using string test evaluations
#
testuserchristine
#
if [ $testuser christine ]
thenecho The testuser variable contains: christine
elseecho The testuser variable contains: $testuser
fi文件比较
比较描述-d file检查 file 是否存在且为目录-e file检查 file 是否存在-f file检查 file 是否存在且为文件-r file检查 file 是否存在且可读-s file检查 file 是否存在且非空-w file检查 file 是否存在且可写-x file检查 file 是否存在且可执行-O file检查 file 是否存在且属当前用户所有-G file检查 file 是否存在且默认组与当前用户相同file1 -nt file2检查 file1 是否比 file2 新file1 -ot file2检查 file1 是否比 file2 旧 #!/bin/bash
# 检查目录
#
jump_directory/home/Torfa
#
if [ -d $jump_directory ]
thenecho The $jump_directory directory exists.cd $jump_directoryls
elseecho The $jump_directory directory does NOT exist.
fiif-then的高级特性 在子 shell 中执行命令的单括号 用于数学表达式的双括号 用于高级字符串处理功能的双方括号
单括号
单括号允许在 if 语句中使用子 shell。单括号形式的 test 命令格式如下 (command)在 bash shell 执行 command 之前会先创建一个子 shell然后在其中执行命令。如果命令成功结束则退出状态码会被设为 0then 部分的命令就会被执行。 #!/bin/bash
# Testing a single parentheses condition
#
echo $BASH_SUBSHELL
#
if (echo $BASH_SUBSHELL)
thenecho The subshell command operated successfully.
#
elseecho The subshell command was NOT successful.
#
fi当脚本第一次在 if 语句之前执行 echo $BASH_SUBSHELL 命令时是在当前 shell 中完成的。该命令会输出 0表明没有使用子 shell$BASH_SUBSHELL 当前子 shell 环境的嵌套级别初始值是 0。在if 语句内脚本在子 shell 中执行 echo $BASH_SUBSHELL 命令该命令会输出 1表明使用了子 shell。子 shell 操作成功结束接下来是执行 then 部分的命令 双括号
双括号命令允许在比较过程中使用高级数学表达式。
符号描述val后增val–后减val先增–val先减!逻辑求反-位求反**幂运算左位移右位移位布尔 AND逻辑 AND|逻辑OR #!/bin/bash
# Testing a double parentheses command
#
val110
#
if (( $val1 ** 2 90 ))
then(( val2 $val1 ** 2 ))echo The square of $val1 is $val2,echo which is greater than 90.
#
fi双方括号
双方括号命令提供了针对字符串比较的高级特性。双方括号的格式如下 [[ expression ]]expression 可以使用 test 命令中的标准字符串比较。除此之外它还提供了 test 命令所不具备的另一个特性——模式匹配。 在进行模式匹配时可以定义通配符或正则表达式 #!/bin/bash
# Using double brackets for pattern matching
#
#
if [[ $BASH_VERSION 5.* ]]
thenecho You are using the Bash Shell version 5 series.
fi
# 上述脚本中使用了双等号。双等号会将右侧的字符串5.*视为一个模式并应用模式匹配规则。
# 双方括号命令会对$BASH_VERSION 环境变量进行匹配看是否以字符串 5.起始。
# 如果是则测试通过shell 会执行 then 部分的命令。Case命令
有了 case 命令无须再写大量的 elif 语句来检查同一个变量的值了。case 命令会采用列表格式来检查变量的多个值 case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac#!/bin/bash
# Using a short case statement
#
case $USER in
rich | christine)echo Welcome $USERecho Please enjoy your visit.;;
barbara | tim)echo Hi there, $USERecho Were glad you could join us.;;
testing)echo Please log out when done with test.;;
*)echo Sorry, you are not allowed here.
esac
$循环
for
bash shell 提供了 for 命令以允许创建遍历一系列值的循环。每次迭代都使用其中一个值来执行已定义好的一组命令。for 命令的基本格式如下 for var in list
docommands
done#!/bin/bash
# basic for command 读取列表中的值
for test in Alabama Alaska Arizona Arkansas California Colorado
doecho The next state is $test
donefor 命令使用空格来划分列表中的每个值。如果某个值含有空格则必须将其放入双引号内 #!/bin/bash
# an example of how to properly define values
for test in Nevada New Hampshire New Mexico New York
doecho Now going to $test
done可以从变量中读取列表 #!/bin/bash
# using a variable to hold the list
listAlabama Alaska Arizona Arkansas Colorado
list$list Connecticut
for state in $list #
doecho Have you ever visited $state?
done$list 变量包含了用于迭代的值列表。注意脚本中还使用了另一个赋值语句向$list 变量包含的值列表中追加或者说是拼接了一项。这是向变量中已有的字符串尾部添加文本的一种常用方法。 从命令中读取值列表 #!/bin/bash
# reading values from a file
filestates.txt
for state in $(cat $file)
doecho Visit beautiful $state
done使用 cat 命令来输出文件 states.txt 的内容states.txt 文件中每 个值各占一行而不是以空格分隔。 更改字段分割符
IFS环境变量定义了 bash shell 用作字段分隔符的一系列字符。在默认情况下bash shell 会将下列字 符视为字段分隔符 空格 制表符 换行符
如果 bash shell 在数据中看到了这些字符中的任意一个那么它就会认为这是列表中的一个新字段的开始。
可以在 shell 脚本中临时更改 IFS 环境变量的值指定字段分隔符的字符。 #!/bin/bash
# reading values from a file
filestates.txtIFS.OLD$IFS
IFS$\n # 告诉 bash shell 忽略数据中的空格和制表符
for state in $(cat $file)
doecho Visit beautiful $state
done
IFS$IFS.OLD # 恢复默认值保证后续使用默认值的操作能正常运行# IFS: # 如果要遍历文件中以冒号分隔的值则只需将 IFS 的值设为冒号即可# IFS$\n:; # 将换行符、冒号、分号和双引号作为字段分隔符使用通配符读取目录 #!/bin/bash
# iterate through all the files in a directory
for file in /home/rich/test/*
doif [ -d $file ]thenecho $file is a directoryelif [ -f $file ]thenecho $file is a filefi
done#!/bin/bash
# 可以在 for 命令中列出多个目录通配符
for file in /home/rich/.b* /home/rich/badtest
doif [ -d $file ]thenecho $file is a directoryelif [ -f $file ]thenecho $file is a fileelseecho $file doesnt existfi
doneC 语言风格的 for 命令
bash 中仿 C 语言的 for 循环的基本格式如下 for (( variable assignment ; condition ; iteration process ))# demo
for (( a 1; a 10; a ))注意有些地方与 bash shell 标准的 for 命令并不一致 变量赋值可以有空格 迭代条件中的变量不以美元符号开头 迭代过程的算式不使用 expr 命令格式 #!/bin/bash
# testing the C-style for loopfor (( i1; i 10; i ))
doecho The next number is $i
done仿 C 语言的 for 命令也允许为迭代使用多个变量 #!/bin/bash
# multiple variables
for (( a1, b10; a 10; a, b-- ))
doecho $a - $b
donewhile
while 命令在某种程度上糅合了 if-then 语句和 for 循环。while 命令的格式如下 while test command
doother commands
donewhile 命令中定义的 test command 与 if-then 语句中的格式一模一样。可以使用任何 bash shell 命令或者用 test command 进行条件测试。
while 命令的关键在于所指定的 test command 的退出状态码必须随着循环中执行的命令而改变。如果退出状态码不发生变化那 while 循环就成了死循环。 #!/bin/bash
# while command test
var110
while [ $var1 -gt 0 ]
doecho $var1var1$[ $var1 - 1 ]
donewhile 命令允许在 while 语句行定义多个测试命令。只有最后一个测试命令的退出状态码会被用于决定是否结束循环。 #!/bin/bash
# testing a multicommand while loop
var110
while echo $var1[ $var1 -ge 0 ]
doecho This is inside the loopvar1$[ $var1 - 1 ]
doneuntil
与 while 命令工作的方式完全相反until 命令要求指定一个返回非 0 退出状态码的测试命令。只要测试命令的退出状态码不为 0bash shell 就会执行循环中列出的命令。一旦测试命令返回了退出状态码 0循环就结束了。 until test command
doother commands
done循环控制
breadk
break 命令是退出循环的一种简单方法。你可以用 break 命令退出任意类型的循环包括while 循环和 until 循环。 #!/bin/bash
# breaking out of a for loop
for var1 in 1 2 3 4 5 6 7 8 9 10
doif [ $var1 -eq 5 ]thenbreakfiecho Iteration number: $var1
done
echo The for loop is completed在处理多个循环时break 命令会自动结束你所在的最内层循环 #!/bin/bash
# breaking out of an inner loop
for (( a 1; a 4; a ))
doecho Outer loop: $afor (( b 1; b 100; b ))doif [ $b -eq 5 ]thenbreak # 结束内层循环fiecho Inner loop: $bdone
done有时你位于内层循环但需要结束外层循环。break 命令接受单个命令行参数 break n其中 n 指定了要跳出的循环层级。在默认情况下n 为 1表明跳出的是当前循环。如果将 n 设为 2那么 break 命令就会停止下一级的外层循环 #!/bin/bash
# breaking out of an outer loop
for (( a 1; a 4; a ))
doecho Outer loop: $afor (( b 1; b 100; b ))doif [ $b -gt 4 ]thenbreak 2 # 结束外层循环fiecho Inner loop: $bdone
donecontinue
continue 命令可以提前中止某次循环但不会结束整个循环。你可以在循环内部设置 shell不执行命令的条件。 #!/bin/bash
# using the continue command
for (( var1 1; var1 15; var1 ))
doif [ $var1 -gt 5 ] [ $var1 -lt 10 ]thencontinuefiecho Iteration number: $var1
done也可以在 while 循环和 until 循环中使用 continue 命令但要特别小心。记住当 shell执行 continue 命令时它会跳过剩余的命令