当前位置:网站首页>Day11 shell脚本基础知识
Day11 shell脚本基础知识
2022-08-02 00:06:00 【lookout99】
系列文章目录
Day01 软件测试基础总结
Day02 测试用例知识点总结(上)
Day03 测试用例知识点总结(下)
Day04 禅道-从安装到卸载
Day05 MySql的基础使用
Day06 MySql知识点总结
Day07 MySql知识点再总结与多表查询
Day08 redis的基础知识
Day08 VMware的安装、Linux系统安装和Linux基础命令
Day09 Linux常用命令总结
Day10 Linux环境部署和项目构建
Day11 shell脚本基础知识
文章目录
目录
前言
Shell脚本与Windows/Dos下的批处理相似,也就是用各类命令预先放入到一个文件中,方便一次性执行的一个程序文件,主要是方便管理员进行设置或者管理用的。但是它比Windows下的批处理更强大,比用其他编程程序编辑的程序效率更高,它使用了Linux/Unix下的命令。
Shell脚本多用于维护,但测试人员仍需要了解,并简单掌握。
一、Shell脚本的规则
1.变量的规则
定义变量的规则:
变量名=值
注意:
1.等号两边不能有空格
2.定义特殊的变量需要用单引号或者双引号
3.定义Linux命令需要用反单引号
4.输出时使用单引号与双引号区别与相同点
双引号中有$变量名,输出的是变量名对应的变量值
单引号中有$变量名,输出的是$变量名


2.脚本命名格式
脚本名称使用.sh结尾

3.脚本的起始行格式
第一行必须写# !/bin/bash
4.脚本输入输出格式
1:输入语句用: read -p "描述"
2:Shell输出值: echo $变量名注意:read可以传递多个数据 read –p”name and passwoed”name password
超过的部分都填充在最后一个变量上

![]()
5.脚本的调用
方式1:./脚本名.sh(权限的问题)
方式2: sh 脚本名.s
6.参数传递
参数传递 :向.sh文件中传递数据
格式: sh 脚本名.sh 参数
$1:接收第一个参数~~$n
$n:文件名称
[email protected]:所有参数 s
$#参数个数$0文件名

7.比较运算符和逻辑运算符
1.比较运算符(用于数字比较)
-eq 比较是否相等
-ne 比较不相等
-gt 比较大于
-ge 比较大于等于
-lt 比较小于
-le 比较小于等
2.比较运算符(用于字符串比较)
== 比较是否相等
!= 比较不相
3.逻辑运算符
-a 逻辑与
-o逻辑或
!逻辑非
8.自增与累加
加一操作:
((i++))
((i+=1))
加n操作:s=$(($s+$i))
s=$[$s+$i]
((s=$s+$i))
9.if语句
单条件语句格式:
if [ 条件 ]
then
语句
else
语句
fi

多条件语句格式:
if [ 条件 ]
then
语句
elif [ 条件 ]
then
语句
......
else
语句
fi

注意:
1: if后面有空格
2:条件两边有空格
3:if条件里面存在变量,需要$,而且变量两边需要双引号
4:fi是结束标识
10.case规则
格式:
格式:
注意1: 结束标志done
case $变量名 in
"值")
语句;;
"值")
语句;;
.....
esa


注意:
1.case这第一行以in结尾
2.值可以是字符串也可以是数字, 如果数字范围[0-9]
3.结束语句;;
4.esac结束标志
11.for循环
for 变量 in 列表
do
语句
done

注意:
seq mix step max
注意:mix 开始数据 step:间隔 max 结束数据,如果step为1,可以省略,需要用反单引号
12.while循环
while [条 件 ]
do
语句
done
函数名(){代码 }
调用格式
函数名

注意:
1: while后面有空格2:条件两边有空格
3:while条件里面存在变量,需要$,而且变量两边需要双引号,
4:done是结束标识
二、Shell脚本的函数
1.定义格式
函数名(){代码 }
调用格式
函数名

注意点:必须先定义一个函数,然后再调用
2.函数中的参数
使用$n来接收数据

3.函数中的返回值
Shell中函数中默认存在返回值

注意:
如果没有代码错误默认返回值为0
有代码错误返回1
查看返回值内容:$
总结
边栏推荐
- Short video SEO optimization tutorial Self-media SEO optimization skills and methods
- Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
- ROS dynamic parameters
- 以交易为生是一种什么体验?
- 632. 最小区间
- async/await 原理及执行顺序分析
- 认识USB、Type-C、闪电、雷电接口
- LeetCode_279_完全平方数
- How to design a circular queue?Come and learn~
- [Three sons] C language implements simple three sons
猜你喜欢
![[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core](/img/c8/d7183330ee86c2cae9c21fecb2805b.png)
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core

TCL: Pin Constraints Using the tcl Scripting Language in Quartus

Play NFT summer: this collection of tools is worth collecting

【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core

SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture

How to find new potential projects?Tools recommended

OpenCV DNN blogFromImage() detailed explanation

【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件

如何发现新的潜力项目?工具推荐

利用“栈”快速计算——逆波兰表达式
随机推荐
C language Qixi is coming!It's time to show the romance of programmers!
Is TCP reliable?Why?
QML package management
After an incomplete recovery, the control file has been created or restored, the database must be opened with RESETLOGS, interpreting RESETLOGS.
不就是个TCC分布式事务,有那么难吗?
How does JSP use the page command to make the JSP file support Chinese encoding?
Collection of NFT tools
els block deformation
JSP Taglib指令具有什么功能呢?
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
GIF making - very simple one-click animation tool
SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
JSP out.println()方法具有什么功能呢?
Async/await principle and execution sequence analysis
Arduino 基础语法
回顾历史5次经济衰退时期:这一次可能会有何不同?
How to solve the error when mysql8 installs make
JSP how to obtain the path information in the request object?
JSP out. The write () method has what function?
短视频SEO搜索运营获客系统功能介绍