Shell脚本(一)_shell脚本中will(1)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

date -d “-1 hour” +%H # -1 hour表示当前时间前1小时,可以以此类推

09


或者一分钟前:



date -d “-1 min” +%M # -1 min表示当前时间前1分钟,可以以此类推

09




---


### 20.2 Shell脚本中的变量


shell脚本中的变量可以是一个数值、一个命令或者一个路径。**定义变量的格式为:`变量名=变量的值`,在脚本中引用变量时需要加上符号`$`**。




---


下面编写一个与变量有关的脚本:



vim variable.sh #写入下面内容



#!/bin/bash

In this script we will use variables.

Writen by lzx 2018-7-27

d=date +%H:%M:%S #反引号的作用是将引号内的字符串当成shell命令执行,返回命令的执行结果
echo “The script begin at $d.”
echo “Now we’ll sleep 2 seconds.”
sleep 2
d1=date +%H:%M:%S
echo “The script end at $d1.”


上面d和d1作为变量出现。


执行上面脚本,查看执行结果:



sh variable.sh

The script begin at 10:27:22.
Now we’ll sleep 2 seconds.
The script end at 10:27:24.




---


#### 数字运算


在脚本中进行数字运算,也会用到变量。


下面再自定义一个脚本:



vim sum.sh #写入下面内容



#! /bin/bash

For get the sum of two numbers.

Writen by lzx 2018-7-27

a=1
b=2
sum= [ [ [a+ b ] e c h o " b] echo " b]echo"a+ b = b= b=sum"


**数学计算要用`[]`括起来,并且前面要加上符号`$`**。



sh sum.sh

1+2=3




---


#### 和用户交互


自定义一个脚本:



vim read.sh



#!/bin/bash

Using ‘read’ in shell script.

Writen by lzx 2018-7-27

read -p “Please input a number:” x
read -p “Please input another number:” y
sum= [ [ [x+ y ] e c h o " T h e s u m o f t w o n u m b e r s i s : y] echo "The sum of two numbers is: y]echo"Thesumoftwonumbersis:sum"


**read命令用于和用户交互,它把用户输入的字符串作为变量值**



sh read.sh

Please input a number:5
Please input another number:11
The sum of two numbers is:16


加上 -x选项查看执行过程:



sh -x read.sh

  • read -p ‘Please input a number:’ x
    Please input a number:5
  • read -p ‘Please input another number:’ y
    Please input another number:11
  • sum=16
  • echo ‘The sum of two numbers is:16’
    The sum of two numbers is:16



---


#### shell脚本预设变量


我们之前有使用过类似`/etc/init.d/iptables restart`这样的命令,前面的`/etc/init.d/iptables`文件其实就是一个shell脚本。那为什么后面可以跟一个restart字符串呢?这就涉及到了shell脚本的预设变量。


实际上,shell脚本在执行时,后面可以跟一个或者多个参数。


下面自定义一个脚本:



vim option.sh #写入下面内容



#!/bin/bash

测试预设变量

Writen by lzx 2018-7-27

sum=$[$1+ 2 ] e c h o " s u m = 2] echo "sum= 2]echo"sum=sum"



sh option.sh #后面不加参数时,脚本会出错

option.sh:行5: +: 语法错误: 期待操作数 (错误符号是 “+”)

sh option.sh 5 11 #后面加5和11,5和11就是预设变量$1和$2

sum=16


**`$1`表示脚本的第一个参数,`$2`表示脚本的第二个参数,以此类推**。


**一个脚本的预设变量数量是没有限制的,另外还有`$0`,它表示脚本本身的名字**。



vim option1.sh #写入下面内容



#!/bin/bash
echo “$1 $2 $0”

sh option1.sh 1 2

1 2 option1.sh #可以看到,显示出$0为option1.sh,即脚本名字




---


### 20.3 Shell脚本中的逻辑判断


如果你学过其它编程语言,那么就不会对if感到陌生。在shell脚本中,我们同样可以使用if逻辑判断。




---


#### 不带else


具体格式如下:



if 判断语句;then
command
fi


下面自定义一个脚本:



vim if1.sh #写入下面内容



#!/bin/bash

read -p "Please input your score: " a
if ((a<60));then
echo “You didn’t pass the exam.”
fi


**上面出现了`((a<60))`这样的形式,`(( ))`这是shell脚本特有的格式,只用一个小括号或不用括号都会报错**,要记住这个格式。


查看上面脚本的执行结果:



sh if1.sh

Please input your score: 90 #没有设置else条件,所以没有结果

sh if1.sh

Please input your score: 50
You didn’t pass the exam.




---


#### 带有else


具体格式如下:



if 判断语句
then
command
else
command
fi


下面做if1.sh脚本做修改:



vim if1.sh #修改为下面内容



#!/bin/bash

read -p "Please input your score: " a
if ((a<60));then
echo “You didn’t pass the exam.”
else
echo “Good!You passed the exam.”
fi


执行上面脚本:



sh if1.sh

Please input your score: 90
Good!You passed the exam. #设置了else条件之后,这里返回了结果

sh if1.sh

Please input your score: 50
You didn’t pass the exam.




---


#### 带有elif


具体格式如下:



if 判断语句1;then
command
elif 判断语句2;then
command
else
command
fi


下面自定义一个脚本:



vim if2.sh #写入下面内容



#!/bin/bash

read -p "Please input you score: " a
if ((a<60));then
echo “You didn’t pass the exam.”
elif ((a>=60)) && ((a<90));then # elif相对if,再做一次判断
echo “Good!You passed the exam.”
else
echo “Very good!Your score is very high.”
fi


上面的 **`&&`表示“并且”,另外还有`||`表示“或者”**。


执行上面脚本:



sh if2.sh

Please input you score: 40
You didn’t pass the exam.

sh if2.sh

Please input you score: 85
Good!You passed the exam.

sh if2.sh

Please input you score: 95
Very good!Your score is very high.


**判断数值大小除了可以使用`(( ))`的形式外,还可以使用`[]`,但不能使用`>`、`<`、`=`这样的符号了,要使用`-lt`(小于)、`-gt`(大于)、`-le`(小于或等于)、`-ge`(大于或等于)、`-eq`(等于)、`-ne`(不等于)**。


示例如下:



a=10; if [ $a -lt 1 ] || [ $a -gt 5 ]; then echo ok; fi #注意[]空格,否则会有语法错误

ok

a=5; if [ $a -lt 10 ] || [ $a -gt 1 ]; then echo ok; fi

ok




---


#### 和文档相关的判断


shell脚本中if还经常用于判断文档的属性,比如判断是普通文件还是目录,判断文件是否可读、写、执行权限等。


if 常用的选项有以下几个:



> 
> -e :判断文件或目录是否存在  
>  -d :判断是不是目录以及是否存在  
>  -f :判断是不是普通文件以及是否存在  
>  -r :判断是否有读权限  
>  -w :判断是否有写权限  
>  -x :判断是否有执行权限
> 
> 
> 


具体格式如下:



if [ -e filename ]
then
command
fi


示例如下:



if [ -d /home/ ]; then echo ok; fi

ok

if [ -f /home/ ]; then echo ok; fi


`/home/`是目录而非文件,所以在判断它是否为文件时并不会显示ok。



if [ -f /root/test.txt ]; then echo ok; fi

ok

if [ -r /root/test.txt ]; then echo ok; fi

ok

if [ -w /root/test.txt ]; then echo ok; fi

ok

if [ -x /root/test.txt ]; then echo ok; fi

if [ -e /root/test123.txt ]; then echo ok; fi




---


#### case逻辑判断


在shell脚本中,除了使用if来判断逻辑外,还可以使用case。


具体格式如下:



case 变量 in
value1)
command
;;
value2)
command
;;
value3)
command
;;
*)
command
;;
esac


**上面的结构中,不限制value的个数,`*`代表其它任意值**。


下面自定义一个脚本:



vim case.sh



#!/bin/bash

read -p "Input a number: " n
a=$[n%2]
case $a in
1)
echo “The number is odd.” # odd:奇数
;;
0)
echo “The number is even.” # even:偶数
;;
*)
echo “It’s not a number!”
esac


脚本中的$a的值为1或0,执行结果如下:



sh case.sh

Input a number: 100
The number is even.

sh case.sh

Input a number: 13
The number is odd.


**`case`脚本常用于编写系统服务的启动脚本**。




---


### 20.4 Shell脚本中的循环


shell脚本算是一门简易的编程语言了,脚本中的循环也是不能缺少的。常用的循环有for循环和while循环。




---


#### for循环


for循环结构是在日常运维工作中使用很频繁的循环结构。


具体格式如下:



for 变量名 in 循环的条件
do
command
done


下面自定义一个脚本:



vim for.sh #写入下面内容



#!/bin/bash

for i in seq 1 5; do # seq 1 5 表示从1到5的一个序列
echo $i
done


查看执行结果:



sh for.sh

1
2
3
4
5


上面,**`循环的条件`是引用系统命令的执行结果`seq 1 5`,但必须用反引号括起来**。


示例如下:



for file in ls; do echo $file; done

case.sh
check_ng.sh
first.sh
for.sh
if1.sh
if2.sh
option1.sh
option.sh
read.sh
sum.sh
variable.sh


另外`循环的条件`还可以是一组字符或者数字(用一个或多个空格隔开),也可以是一条命令的执行结果。


示例如下:



for i in 1 2 3 a b c; do echo $i; done

1
2
3
a
b
c




---


#### while循环


日常运维工作中,也会经常用while循环来编写死循环的脚本,用于监控某项服务。


具体格式如下:



while 条件
do
command
done


下面自定义一个脚本:



#!/bin/bash

a=6
while [ $a -ge 1]
do
echo a a = a a= aa=[$a-1]
done


查看执行结果:



sh while.sh

6
5
4
3
2
1


另外,**可以用一个冒号`:`代替循环条件,这样就可以做到死循环**。




---


### 20.5 Shell脚本中的函数


shell脚本中的函数就是先把一段代码整理到一个小单元中,并给这个小单元命名,当用到这段代码时,直接调用这个小单元的名字即可。




---


下面自定义一个脚本:



vim func.sh



#!/bin/bash

function sum()
{
sum=$[$1+$2]
echo $sum
}
sum $1 $2 #预设变量$1 $2


查看执行结果:



sh func.sh 1 2

3


上面func.sh中的`sum()`为自定义的函数。


在shell脚本中函数的格式如下:



function 函数名()
{
command1
command2
}


需要注意的是,**在shell脚本中,被调用的函数一定要写在最前面,不能出现在中间或最后**。因为函数是要被调用的,如果还没有出现就被调用,那当然会出错。




---



![img](https://img-blog.csdnimg.cn/img_convert/8132417c877825feda27d4e0b44e7524.png)
![img](https://img-blog.csdnimg.cn/img_convert/02c2259fe0cfe40d964050d9eb46da05.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618631832)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

5 Shell脚本中的函数


shell脚本中的函数就是先把一段代码整理到一个小单元中,并给这个小单元命名,当用到这段代码时,直接调用这个小单元的名字即可。




---


下面自定义一个脚本:



vim func.sh



#!/bin/bash

function sum()
{
sum=$[$1+$2]
echo $sum
}
sum $1 $2 #预设变量$1 $2


查看执行结果:



sh func.sh 1 2

3


上面func.sh中的`sum()`为自定义的函数。


在shell脚本中函数的格式如下:



function 函数名()
{
command1
command2
}


需要注意的是,**在shell脚本中,被调用的函数一定要写在最前面,不能出现在中间或最后**。因为函数是要被调用的,如果还没有出现就被调用,那当然会出错。




---



[外链图片转存中...(img-H8IUal7q-1715876888358)]
[外链图片转存中...(img-YZnkWUAl-1715876888359)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618631832)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值