Linux命令与shell编程脚本(三)

自媒体运营教程 产品运营 20

Linux命令与shell编程脚本(三)

【本文主要记录一些linux常用的命令以及shell脚本编程相关的语法,对于Linux相关的背景知识不在本文描述范围】

前言

在前面章节简述了shell脚本,执行方式是按照顺序执行,这是最基本的操作方式,但是通常情况,并不是所有的脚本都是按照顺序执行方式,比如要在某个路径下新建一个文件,那需要先检查这个路径是否真实存在,亦或者此路径是否可写等,这时候对于新建文件这个操作就需要一定的条件,即需要通过命令改变程序执行顺序。

shell对此提出了结构化命令,对于结构化命令通常包含if-elif、case、for、while、until等,在本节主要介绍if-elif以及case语句;


结构化语句 if_elif

if-elif标准语法:

if command1
then
command set 1
elif command2
then
command set 2
...
elif commandx
then
command set x
fi

上述语法为标准的if-elif语法,也可以适当进行缩减,比如只有if-else也是可以的。

每块命令都会根据命令是否返回退出状态码0来执行,例如command1的执行后退出状态码为0,则执行对应的command set块,此处可以是多条命令,且当command1对应的命令块被执行后,后面的命令块都不会被执行了,if-elif是按照顺序执行方式。如果command1执行后退出状态码不为0,则继续执行command2,直到某个命令执行的退出状态码为0后停止,否则全部命令块都不会被执行;

下面针对if-elif结构化语句给出一个简单的示例:(所有的代码都是可执行,执行环境:菜鸟教程在线编辑器)

#!/bin/bash
#This script is test for if-elif
if NOT_COMMAND
then
var=NOT_COMMAND
echo "$var is a system command1"
elif pwd
then
var=pwd
echo "$var is a system command2"
elif ls
then
var=ls
echo "$var is a system command3"
else
echo "I don't know system command"
fi
echo "I find a system command is $var"

执行结果:

/box
[pwd] is a system command2
I find a system command is pwd

script.sh: line 3: NOT_COMMAND: command not found

示例分析:

上述三个command:NOT_COMMAND、pwd、ls,其中pwd、ls为系统指令,那么执行后的退出状态码为0,按照顺序执行了第二个命令块,由于NOT_COMMAND为非系统指令,所以直接执行报错,且退出状态码非0,故不会执行第一个命令块;


test命令

对于上述if-elif语句执行条件,都是根据command命令执行后退出状态码是否为0作为判断条件,如果退出状态码为0,则执行对应的命令块,反之继续执行下面判断条件。这种使用方法使得if_elif在使用时很受限,故shell脚本提供了test命令,具体的命令格式简单清晰:

if test condition
then
command set
fi

test的另外一种表达方式(需要注意,第一个方括号之后和第二个方括号之前必须添加空格,否则报错):

if [ condition ]
then
command set
fi

上述语法中,只需要 test condition 正确执行,退出状态码即为0,那么对于if_elif的使用范围大大增加,对于test命令可以进行下面三类条件判断:

  • 数值比较
  • 字符串比较
  • 文件比较

数值比较

数值比较是test最常用的场景,具体的使用方法:


Linux命令与shell编程脚本(三)-第1张图片-90博客网

test 数值比较功能

针对数值比较给出一个简单示例:

#!/bin/bash
#This Script is test for test function
var1=10
var2=15
var3=20
echo "Test function is display"
if test $var1 -eq 10
then
echo "$var1 equal to 10"
echo "test for Test function -eq"
fi
if [ $var3 -gt $var2 ]
then
echo "var3($var3) is greater than var2($var2)"
echo "test for Test function -gt"
fi
echo "Test function is End"

执行结果:

Test function is display
10 equal to 10
test for Test function -eq
var3(20) is greater than var2(15)
test for Test function -gt
Test function is End

字符串比较

字符串比较使用示例:


Linux命令与shell编程脚本(三)-第2张图片-90博客网

test 字符串比较

需要注意:

对于 大于、小于字符串比较,必须使用转义符,否则会被当成定向符;大于和小于顺序对比是按照ASCII码挨个对比,不同于sort函数;

针对字符串比较给出一个简单示例:

#!/bin/bash
#This Script is test for test function
var1=Kobe
var2=James
var3=Antony
echo "Test function is display"
if [ $var1 != $var2 ]
then
echo "$var1 and $var2 are not the same basketball players"
echo "test for Test function != "
fi
if [ $var3 \> $var2 ]
then
echo "$var3 is my favorite baskerball player"
else
echo "$var2 is my favorite baskerball player"
echo "test for Test function > "
fi
echo "Test function is End"

执行结果:

相关影片资源迅雷下载推荐

Lazada广告推广3大注意事项!

lazada怎么投广告?lazada怎么设置广告?lazada怎么投广告?这些Lazada广告的疑问后台接踵而来,我明白大家对Lazada广告这块非常重视,因为Lazada广告是能最快获取流量的途径,但是在我们正式开始做Lazada广告之前, ...

推广,Lazada广告推广3大注意事项!

Test function is display
Kobe and James are not the same basketball players
test for Test function !=
James is my favorite baskerball player
test for Test function >
Test function is End

文件比较

文件比较在shell脚本中应该是使用频率最高的,具体使用方法如下:


Linux命令与shell编程脚本(三)-第3张图片-90博客网

test 文件比较

针对文件比较给出一个简单的示例:

#!/bin/bash
#This Script is test for test function
var1=$(pwd)
var2=home/kobe
var3=script
echo "Test function is display"
if [ -d $var1 ]
then
echo "$var1 is a real path"
fi
if [ -e $var2 ]
then
echo "$var2 is not exist"
else
echo "$var2 is a not exist path "
fi
if [ -f $var3 ]
then
echo "$var3 is a exist file"
else
echo "$var3 is not a file"
fi
echo "Test function is End"

执行结果:

Test function is display
/box is a real path
home/kobe is a not exist path
script is not a file
Test function is End

除了上述的几种比较方式,还支持 复合条件测试,具体使用方法如下:

  • [ condition1 ] && [ condition2 ]
  • [ condition1 ] || [ condition2 ]

对于使用布尔逻辑与,需要两个条件都满足,退出状态码才会是0

对于使用布尔逻辑或,只需要有一个条件满足,退出状态码即为0


if-elif高级特性

if-elif还支持两项高级特性:

  • 用于数学表达式的双括号
  • 用于高级字符串处理功能的双方括号

双括号的使用格式:

(( expression ))

对应于具体的使用方法如下:


Linux命令与shell编程脚本(三)-第4张图片-90博客网

双括号使用方法

#!/bin/bash
#This Script is test for test function
var1=10
var2=85
var3=5
echo "Test function is display"
if (( $var1 ** 2 > $var2 ))
then
(( var12 = $var1 ** 2 ))
echo "The square of $var1 is $var12, and is greater than $var2"
fi
if (( var1++ > $var3 ))
then
echo "var1 is equal to $var1"
(( var14=--var1 ))
(( var13=var1++ ))
echo "var1 is equal to $var1"
echo "var13 is equal to $var13"
echo "var14 is equal to $var14"
fi
echo "Test function is End"

执行结果:

Test function is display
The square of 10 is 100, and is greater than 85
var1 is equal to 11
var1 is equal to 11
var13 is equal to 10
var14 is equal to 10
Test function is End

双方括号的使用格式:

[[ expression ]]

双方括号里面 expression 使用了test命令中标准的字符串比较,但是它提供test命令未提供的模式匹配;如在模式匹配中可以使用正则表达式来匹配字符串值;

简单的示例:

#!/bin/bash
#This Script is test for test function
var1=$(pwd)
echo "Test function is display"
if [[ $var1 == *b* ]]
then
echo "In this path includes $var1"
else
echo "In this path only includes $var1"
fi
echo "Test function is End"

执行结果:

Test function is display
In this path includes /box
Test function is End

case

作为if_elif语法的变形,case对于多个带有多个elif语句来说比较友好,case命令的使用格式:

case variable in
pattern1 | pattern2)
command1;;
pattern3)
command2;;
*)
default command3;;
esac

case命令将指定的变量与不同模式进行比较,如果变量和模式匹配,那shell会执行对应模式下的指令。

可以通过竖线操作符在一行中分割出多个模式,星号会捕获所有与已知模式不匹配的值。

一个简单的示例:

#!/bin/bash
#This Script is test for test function
var=kobe
echo "Test function is display"
case $var in
james)
echo "$var is my favorite basketball player " ;;
kobe)
echo "$var is my favorite basketball player ";;
*)
echo "Sorry, None of them is my favorite basketball player";;
esac
echo "Test function is End"

执行结果:

Test function is display
kobe is my favorite basketball player
Test function is End

对于其他结构化执行for、while、until在下一章节讲述

相关影片资源迅雷下载推荐

自动化测试中,测试数据与脚本分离以及参数化方法

测试数据和测试代码在实际的测试工作中,往往是分开存放的,利于测试数据和测试脚本的分开来维护。比如,为测试用例添加几组新的测试数据,只需要修改测试数据文件,测试代码不需要动;如果是测试用例增加了新的校验 ...

脚本,自动化测试中,测试数据与脚本分离以及参数化方法

标签: 脚本 Linux命令与shell编程脚本(三)

抱歉,评论功能暂时关闭!