Linux Shell 结构化语句模板

01. if-then 语句

如果command的退出状态码是0,则进入判断执行commands,否则执行脚本中的下一个命令。其中command的执行结果无论对错均会显示到界面。

if	command
then
	commands
fi
if	command; then
	commands
fi

02. if-then-else 语句

如果command的退出状态码是0,则进入判断执行commands,否则执行下一个分支判断。其中command的执行结果无论对错均会显示到界面。

if	command
then
	commands
else
	commands
	# 可以打印前一个分支失败后的提示
fi

03. 嵌套 if-then-elif 语句

if-then 与 if-then-else 之间可以相互嵌套,也可以选择另外一种方式

if command1
then
	command set 1
elif command2
then
	command set 2
elif command3
then
	command set 3
elif command4
then
	command set 4
else
	command set 5
	# 一般为一些提示信息
fi

注意:case命令可以代替if-then语句的大量嵌套

04. test 语句

if-then语句不能测试命令退出状态码之外的条件,但是可以通过test命令辅助执行。if-then语句是否能测试
命令退出状态码之外的条件。condition 是test命令的一些参数和值

if test condition
then
	commands
fi

例如:判断变量中是否有值

#! /bin/bash
#
my_variable="Full"
#
if test $my_variable
then
	echo "The $my_variable expression returns a True"
else
	echo "The $my_variable expression returns a False"
fi

另一种条件测试方法,无需在if-then语句中声明test命令。注意,第一个方括号之后和第二个方括号之前必须加上一个空格,否则就会报错。

if [ condition ]
then
	commands
fi

数值比较(不能使用浮点数)
在这里插入图片描述

#! /bin/bash
#
value1=10
value2=11
#
if [ $value1 -gt 5 ]
then
echo "The test value $value1 is greater than 5"
fi
#
if [ $value1 -eq $value2 ]
then
echo "The values are equal"
else
echo "The values are different"
fi

字符串比较
在这里插入图片描述
记住,在比较字符串的相等性时,比较测试会将所有的标点和大小写情况都考虑在内,包括字符顺序(比较测试中使用的是标准的ASCII顺序,根据每个字符的ASCII数值来决定排序结果。 sort 命令使用的是系统的本地化语言设置中定义的排序顺序。对于英语,本地化设置指定了在排序顺序中小写字母出现在大写字母前)。

比较字符串大小时,大于号和小于号必须转义,否则shell会把它们当作重定向符号,把字符串值当作文件名。

#! /bin/bash
#
val1=baseball
val2=hockey
#
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi

-n 和 -z 可以检查一个变量是否含有数据。
在这里插入图片描述
在这里插入图片描述
文件比较
在这里插入图片描述
在这里插入图片描述

jump_directory=/home/arthur
#
if [ -d $jump_directory ]

在这里插入图片描述

location=$HOME
file_name="sentinel"
#
if [ -e $location ]
#
if [ -e $location/$file_name ]

在这里插入图片描述

item_name=$HOME
#
if [ -e $item_name ]
#
if [ -f $item_name ]

在这里插入图片描述

pwfile=/etc/shadow
# first, test if the file exists, and is a file
if [ -f $pwfile ]then
# now test if you can read it
if [ -r $pwfile ]

在这里插入图片描述

file_name=$HOME/sentinel
#
if [ -f $file_name ]
#
if [ -s $file_name ]

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

05. 复合条件测试

在这里插入图片描述

if [ -d $HOME ] && [ -w $HOME/testing ]

06. if-then 高级特性

在这里插入图片描述
双括号表达式
在这里插入图片描述
可以在if语句中用双括号命令,也可以在脚本中的普通命令里使用来赋值。注意,不需要将双括号中表达式里的大于号转义。

val1=10
#
if (( $val1 ** 2 > 90 ))
#
(( val2 = $val1 ** 2 ))

双方括号表达式(可以定义一个正则表达式,例如 r*)

if [[ $USER == r* ]]

07. case 语句

在这里插入图片描述

case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac
#!/bin/bash
# using the case command
#
case $USER in
rich | barbara)
	echo "Welcome, $USER"
	echo "Please enjoy your visit";;
testing)
	echo "Special testing account";;
jessica)
	echo "Do not forget to log off when you're done";;
*)
	echo "Sorry, you are not allowed here";;
esac

08. for 语句

for var in list
do
	commands
done

读取列表中的值

在最后一次迭代后, $test变量的值会在shell脚本的剩余部分一直保持有效。它会一直保持最后一次迭代的值(除非你修改了它)
在这里插入图片描述
读取列表当中的复杂值
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
记住, for循环假定每个值都是用空格分割的。如果有包含空格的数据值,你就陷入麻烦了。
在这里插入图片描述
另外要注意的是,在某个值两边使用双引号时, shell 并不会将双引号当成值的一部分。

从变量读取列表

list="Alabama Alaska Arizona Arkansas Colorado"
list=$list" Connecticut"
#
for state in $list
do
echo "Have you ever visited $state?"
done

从命令读取值

file="states"
for state in $(cat $file)
do
	echo "Visit beautiful $state"
done

更改字段分隔符
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
用通配符读取目录

for file in /home/rich/test/*
do
if [ -d "$file" ]
then
	echo "$file is a directory"
elif [ -f "$file" ]
then
	echo "$file is a file"
fi
done

在这里插入图片描述
可以将任意多的通配符放进列表中。
在这里插入图片描述
在这里插入图片描述

09. C语言风格 for 语句

for (( variable assignment ; condition ; iteration process ))

在这里插入图片描述
在这里插入图片描述
使用多个变量
在这里插入图片描述

10. while 语句

在这里插入图片描述

while test command
do
	other commands
done
var1=10
while [ $var1 -gt 0 ]
do
	echo $var1
	var1=$[ $var1 - 1 ]
done

使用多个测试命令
在这里插入图片描述
while命令允许你在while语句行定义多个测试命令。只有最后一个测试命令的退出状态码会被用来决定什么时候结束循环。注意,每个测试命令都出现在单独的一行上。

11. until 语句

在这里插入图片描述

until test commands
do
	other commands
done

在这里插入图片描述
在这里插入图片描述
shell可以执行指定的多个测试命令,只有在最后一个命令成立时停止。

12. 嵌套循环

在这里插入图片描述

var1=5
while [ $var1 -ge 0 ]
do
	echo "Outer loop: $var1"
	#
	for (( var2 = 1; $var2 < 3; var2++ ))
	do
		var3=$[ $var1 * $var2 ]
		echo " Inner loop: $var1 * $var2 = $var3"
	done
	#
	var1=$[ $var1 - 1 ]
done

在这里插入图片描述

13. 循环处理文件数据

在这里插入图片描述
在这里插入图片描述

14. 控制循环

在这里插入图片描述
跳出单层循环
在这里插入图片描述
在这里插入图片描述
跳出内部循环

for (( a = 1; a < 4; a++ ))
do
	echo "Outer loop: $a"
	for (( b = 1; b < 100; b++ ))
	do
		if [ $b -eq 5 ]
		then
			break
		fi
		echo " Inner loop: $b"
	done
done

跳出外部循环,可以指定层数
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
指定继续执行哪一级循环

for (( a = 1; a <= 5; a++ ))
do
	echo "Iteration $a:"
	for (( b = 1; b < 3; b++ ))
	do
		if [ $a -gt 2 ] && [ $a -lt 4 ]
		then
			continue 2
		fi
		var3=$[ $a * $b ]
		echo " The result of $a * $b is $var3"
	done
done

15. 处理循环的输出

可以对循环的输出使用管道或进行重定向。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42226855/article/details/113094217