Shell脚本基础知识

什么是shell

Shell概述:

shell是一个命令的解释器
![image.png](https://img-blog.csdnimg.cn/img_convert/b5ca5ab48c3b7f381310120d6ba1ec26.png#averageHue=#eec096&clientId=u307a07a9-7d1b-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=502&id=uf4fc102b&margin=[object Object]&name=image.png&originHeight=628&originWidth=1026&originalType=binary&ratio=1&rotation=0&showTitle=false&size=445122&status=done&style=none&taskId=ue01813f8-9e08-49d4-bd39-cc5a49aaf52&title=&width=820.8)
shell是一门功能强大的脚本语言,易编写,易调试,灵活性强。需要注意的是shell是一门解释执行的脚本语言,并且可以使用shell调用Linux命令。

Shell的分类,主要分为两个家族,这两个家族的语法互不兼容:
Bourne Shell:从1979年开始Unix使用Bourne Shell,Bourne Shell的主文件名为sh。主要包括sh,zsh,psh,ksh,Bash
C Shell:主要运用在BSD版本的Unix中,其语法与C语言类似所以叫做C
shell主要包括csh和tcsh

查看当前的系统支持的Shell类型:

//查看当前系统中支持的shell
vi /etc/shell
//切换当前系统中的shell
直接输入shell的名字就可以切换,使用exit可以退出

shell编写Hello World

//①创建一个.sh文件,这里需要后缀名,目的是在使用vim编辑器的时候告诉它我们编写的是一个shell脚本,可以给一些提示
vi hello.sh
//正式开始编写一个shell
#②在shell的开头如下的声明,声明这是一个shell脚本
#!/bin/bash
//③正式开始编写自己的shell脚本
#在shell中可以直接调用Linux命令
echo "Hello World"

//④执行一个shell脚本有两种方式,方式一是给文件一定的权限并且使用绝对路劲或者相对路劲去执行,第二种方式是使用bash
方式一:
#给定权限
chmod 755 hello.sh
接着就可以执行了
方式二:
bash hello.sh

Bash的基本功能

历史命令以及命令补全

历史命令:
history:用于查看历史执行的命令
参数w用于将缓存中的命令写道~/bash_history中去
参数c用于删除缓存中的历史命令以及~/bash_history中的历史命令
history中保存的命令条数可以在/etc/profile中的history参数中进行配置

一些好用的功能:
关于历史命令的:

!!:用于执行上一条命令
!n:用于执行指定编号的命令
!字符串:用于执行以相关字符开头的历史命令
上下键用于选中历史命令

关于命令补全:

使用tab可以进行命令和路径的补全
别名和快捷键

LInux执行命令的优先级:
![image.png](https://img-blog.csdnimg.cn/img_convert/d52ef32d949b5c88032f4e6ce11751c0.png#averageHue=#eaeae9&clientId=u427e0ab6-bd05-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=351&id=u9f159e91&margin=[object Object]&name=image.png&originHeight=439&originWidth=1021&originalType=binary&ratio=1&rotation=0&showTitle=false&size=217089&status=done&style=none&taskId=u7ddba4d9-37d9-4893-a6cc-d3526823e75&title=&width=816.8)

可以使用如下命令来定义一个命令的别名,但是只要退出登录了就会失效
alias 别名 = '元命令'
去除别名:
unalias 别名

别名永久保存:
/用户/bashrc

Linux中常用的快捷键:
![image.png](https://img-blog.csdnimg.cn/img_convert/4a976b61ebd746b8aa11c9e1c8476810.png#averageHue=#e3e3e2&clientId=u427e0ab6-bd05-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=510&id=u825777ba&margin=[object Object]&name=image.png&originHeight=637&originWidth=1067&originalType=binary&ratio=1&rotation=0&showTitle=false&size=391510&status=done&style=none&taskId=u40543010-0cf4-49f9-8134-9f98db79727&title=&width=853.6)

输入输出重定向

标准输入输出错误
![image.png](https://img-blog.csdnimg.cn/img_convert/29f40ae075127a6bff1c02b5773e97e1.png#averageHue=#ededec&clientId=u427e0ab6-bd05-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=389&id=u762cd3bf&margin=[object Object]&name=image.png&originHeight=486&originWidth=1082&originalType=binary&ratio=1&rotation=0&showTitle=false&size=143252&status=done&style=none&taskId=u790f7a06-f59d-488c-8e81-9b379cc8ba3&title=&width=865.6)
输出重定向:标准输出是将输出打印到屏幕上,输出重定向的意思就是输出不再打印到屏幕上而是输出到文件中。
![image.png](https://img-blog.csdnimg.cn/img_convert/f80d0738e0a065970ea2fe47fba1e64e.png#averageHue=#ebebeb&clientId=ufeaeda3c-617f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=423&id=u6d43c4cd&margin=[object Object]&name=image.png&originHeight=529&originWidth=1067&originalType=binary&ratio=1&rotation=0&showTitle=false&size=233051&status=done&style=none&taskId=u958bb181-0417-4c49-95c0-449336a5b38&title=&width=853.6)
一个特殊的例子,将输出丢到垃圾箱中, >> /dev/null
![image.png](https://img-blog.csdnimg.cn/img_convert/519c5e4510226bc193dc948300c21634.png#averageHue=#ededed&clientId=ufeaeda3c-617f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=474&id=u6561c604&margin=[object Object]&name=image.png&originHeight=592&originWidth=1065&originalType=binary&ratio=1&rotation=0&showTitle=false&size=262942&status=done&style=none&taskId=u69ae104c-43c5-4af2-bc77-bfd574e7e1f&title=&width=852)
输入重定向:标准输入其实是从键盘获取的重定向,输入重定向的意思就是不再从键盘接收输入而是从文件中接受重定向
使用wc命令进行演示,wc是一个统计的命令,用于统计输入内容的字符数,单词数和行数,c参数代表字符数,w参数代表单词数,l参数代表行数。
输入重定向使用的是 “<”表示,后面跟着的是文件,意思是将文件中的内容作为命令的输入,还有一种用法就是"<<",它后面的一个标志会被作为输入的开始和结束符号。

多命令的顺序执行和管道

多命令顺序执行:
![image.png](https://img-blog.csdnimg.cn/img_convert/245263a9551334c334e21cac36257ad8.png#averageHue=#f0f0f0&clientId=ufeaeda3c-617f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=384&id=u5f1c55fc&margin=[object Object]&name=image.png&originHeight=480&originWidth=1030&originalType=binary&ratio=1&rotation=0&showTitle=false&size=201171&status=done&style=none&taskId=uf4e81495-e7b8-43c4-bb0f-8204d08b106&title=&width=824)
";"的作用
第一个作用就是可以简化操作,一次执行多条命令
第二个作用就是配合dd命令来执行统计复制一个硬盘或者文件所需要的时间
![image.png](https://img-blog.csdnimg.cn/img_convert/17950304fbd832f72f4429558db0dc15.png#averageHue=#f6f6f6&clientId=ufeaeda3c-617f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=462&id=u222e7cd2&margin=[object Object]&name=image.png&originHeight=577&originWidth=1073&originalType=binary&ratio=1&rotation=0&showTitle=false&size=208250&status=done&style=none&taskId=ucec44d86-e615-4f23-8bab-3cf24f0f3e0&title=&width=858.4)

![image.png](https://img-blog.csdnimg.cn/img_convert/e7ec6dcd8ef35cd7e5768b00c183bd74.png#averageHue=#eeeeee&clientId=ufeaeda3c-617f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=331&id=u7127bfc9&margin=[object Object]&name=image.png&originHeight=414&originWidth=790&originalType=binary&ratio=1&rotation=0&showTitle=false&size=86291&status=done&style=none&taskId=u6ebd96d7-9858-4367-921a-9437c9ae10c&title=&width=632)
管道符的常用方式,配合more用于输出内容的分页显示,配合grep用于搜索显示
![image.png](https://img-blog.csdnimg.cn/img_convert/58b79d2dc446b5f39f5de65e84fcd3ea.png#averageHue=#f3f3f3&clientId=ufeaeda3c-617f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=407&id=u6d97d0c2&margin=[object Object]&name=image.png&originHeight=509&originWidth=1047&originalType=binary&ratio=1&rotation=0&showTitle=false&size=172429&status=done&style=none&taskId=uf5a6d8d3-6368-4a02-a6be-c57cd4cae4e&title=&width=837.6)

通用符和其他特殊符号

通配符:
![image.png](https://img-blog.csdnimg.cn/img_convert/1f1a1c50878fca278d817b4b6c7dd68a.png#averageHue=#ebebeb&clientId=ufeaeda3c-617f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=429&id=ubde57c62&margin=[object Object]&name=image.png&originHeight=536&originWidth=1054&originalType=binary&ratio=1&rotation=0&showTitle=false&size=201023&status=done&style=none&taskId=u81bdff9a-4b10-4b04-ac84-618db0a4a56&title=&width=843.2)
特殊符号:
![image.png](https://img-blog.csdnimg.cn/img_convert/bcd3f2eba7a180576524bf1f015844d6.png#averageHue=#e4e4e4&clientId=ufeaeda3c-617f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=507&id=ud1536b12&margin=[object Object]&name=image.png&originHeight=634&originWidth=1101&originalType=binary&ratio=1&rotation=0&showTitle=false&size=363791&status=done&style=none&taskId=ue428abf7-1a91-4b27-8874-44597b1f124&title=&width=880.8)

shell中的变量

变量是什么

变量是计算机内存的单元,其值可以改变。shell脚本中使用它来存放一些数字或者文件名信息,我们可以使用一个变量名来引用这个变量。变量主要用于系统感知用户的相关设置,或者暂时存储一些信息。

变量定义的一些规则

变量类型默认为字符串类型,如果要对变量进行数值运算需要转换类型。变量名的命名规则是由字母、数字、下划线组成,并且不能以数字开头。在定义变量的时候等号两边不能存在空格,如果变量本身中包含有空格的话需要使用单引号或者双引号括起来。变量的值中可以使用“\”定义转义字符,并且可以在变量中使用“ n a m e ” 或 者 name”或者 name{name}的形式进行变量的叠加(就是在变量原有的内容上增加新的内容)。变量如果要保存命令返回的结果需要使用$()或者``符号进行反引用。

变量的分类

用户自定义变量(本地变量):
用户自定义的变量比较自由,变量名和值都可以自定义。
系统变量(环境变量)
环境变量的特点是,系统中存在一些已经定义好的环境变量,对于这些环境变量我们只能够修改其值,不能去修改其变量名;另外我们还可以自己定义一些系统变量,自定义的系统变量我们可以指定其变量名和值,环境变量的变量名我们一般为大写。
位置参数变量
位置参数变量是用于给脚本传递数据或者参数的变量,其变量名和用途都是相对固定的,不能够新建。
预定义变量
预定义变量是bash中已经定义好的变量,其变量名和用途是相对固定的,不能够新建。

变量的基本用法
用户自定义变量
用户自定义变量:
//变量的定义
变量名=变量值
//变量的引用
$变量名
//变量的查看
set
//变量的删除
unset 变量名
系统变量
环境变量和用户自定义变量(本地变量)的区别:
环境变量和本地变量的最大区别就在于,本地变量只能够在当前的shell中生效,环境变量可以在当前的shell和当前shell的所有子shell中生效。如果将环境变量写入到相应的配置文件中那么这个环境变量就会在所有的shell中生效。

如何定义新的环境变量:
定义:
①export 变量名=变量值
②变量名=变量值 export 变量名

引用变量:
$变量名

查看变量:
可以使用set命令来查看所有的变量,也可以使用env命令来查看环境变量

删除变量:
unset 变量名


如何使用系统自带的环境变量:
PATH变量,linux所有的程序都需要使用相对路劲或者绝对路径来执行,命令也是对应的程序脚本,之所以它可以不使用路劲来指定的,是因为它定义在了系统默认的程序搜索路径中
而这些路劲保存在PATH环境变量中。可以通过查看PATH环境变量来查找系统的默认路径,我们可以通过将程序脚本放在系统默认路径下实现和命令一样调用,但是这种方式会破坏系统默认
脚本的结构。我们还可以在PATH中添加自己的路径来实现脚本如同命令一样执行。
PS1:环境变量,它用于定义终端输出的格式,我们可以通过修改PS1变量来改变终端输出的格式。	
位置参数变量

![image.png](https://img-blog.csdnimg.cn/img_convert/6b38cd25460bf9261a44a0941ee623a4.png#averageHue=#e9e9e9&clientId=uc9ef5a7f-b6a8-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=449&id=udee2ee4b&margin=[object Object]&name=image.png&originHeight=561&originWidth=1052&originalType=binary&ratio=1&rotation=0&showTitle=false&size=229480&status=done&style=none&taskId=u9a7213b4-4fc6-4292-8a12-3f621be7f67&title=&width=841.6)

预定义变量

![image.png](https://img-blog.csdnimg.cn/img_convert/68f509242312c8235196c13914d225af.png#averageHue=#ebebeb&clientId=uc9ef5a7f-b6a8-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=418&id=u571c9df7&margin=[object Object]&name=image.png&originHeight=522&originWidth=1053&originalType=binary&ratio=1&rotation=0&showTitle=false&size=240993&status=done&style=none&taskId=u5bcb0108-6647-4691-b51c-e70e319875b&title=&width=842.4)
在脚本中接收键盘的输入,并且保存到变量中
![image.png](https://img-blog.csdnimg.cn/img_convert/26b9ec965bec02d9eda6c4985698e00c.png#averageHue=#f1f1f1&clientId=uc9ef5a7f-b6a8-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=498&id=u5343ae7d&margin=[object Object]&name=image.png&originHeight=623&originWidth=1027&originalType=binary&ratio=1&rotation=0&showTitle=false&size=238086&status=done&style=none&taskId=ued699df6-cda1-4caf-8696-e73fae5e04a&title=&width=821.6)

运算符

数值运算符

![image.png](https://img-blog.csdnimg.cn/img_convert/2f150ec01a7545ac7a96e33c9cb31007.png#averageHue=#ebebeb&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=434&id=ud1b6caa7&margin=[object Object]&name=image.png&originHeight=543&originWidth=814&originalType=binary&ratio=1&rotation=0&showTitle=false&size=201167&status=done&style=none&taskId=ud9fc8875-bf9f-4074-b2ab-3e8ff3d0d06&title=&width=651.2)

//在shell中的数值运算
aa=22
bb=11
//一下的结构会是2211,原因是shell将变量默认为字符串类型处理
cc=aa+bb
//数值运算的方法
//方法1:使用declear做数值运算
declear -i cc=$aa+$bb
//方法2:使用let或者expr运算工具转换
dd=$(expr $aa + $bb)(注意“+”的两边必须有空格)
//使用$[],$(())算数式子
cc=$[$aa+$bb]
cc=$[#aa+$bb]

运算符的优先级:数字越大优先级越高
![image.png](https://img-blog.csdnimg.cn/img_convert/b757fef50b6d47294f6e2615d94ed228.png#averageHue=#ededed&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=466&id=ucd03cf14&margin=[object Object]&name=image.png&originHeight=583&originWidth=1009&originalType=binary&ratio=1&rotation=0&showTitle=false&size=177499&status=done&style=none&taskId=u52dc3a95-0797-450f-9e40-d85b3515ec3&title=&width=807.2)
变量测试和内容替换:
测试:主要用于使用x,判断变量y的值是否存在,是否为空,值为多少
![image.png](https://img-blog.csdnimg.cn/img_convert/a8b999ece97cde6f3c16a9e636cad4e9.png#averageHue=#ebebeb&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=529&id=u691d16a4&margin=[object Object]&name=image.png&originHeight=661&originWidth=1052&originalType=binary&ratio=1&rotation=0&showTitle=false&size=270280&status=done&style=none&taskId=u0a0da758-8027-444c-a2b9-18d1ed1f586&title=&width=841.6)

环境变量配置文件

source命令

让配置文件直接生效,无需重启
![image.png](https://img-blog.csdnimg.cn/img_convert/e6e999ca4e0d86286c988540985f11fd.png#averageHue=#f3f3f3&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=314&id=u9b1058b7&margin=[object Object]&name=image.png&originHeight=393&originWidth=889&originalType=binary&ratio=1&rotation=0&showTitle=false&size=88933&status=done&style=none&taskId=u62f1324d-6cab-48f1-802e-2d867a78402&title=&width=711.2)

环境变量配置文件的简介

概念:
包含哪些:
![image.png](https://img-blog.csdnimg.cn/img_convert/f75b1dc40a5fc3fc6e5f018803184b42.png#averageHue=#efefef&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=319&id=u95668bd1&margin=[object Object]&name=image.png&originHeight=399&originWidth=471&originalType=binary&ratio=1&rotation=0&showTitle=false&size=86277&status=done&style=none&taskId=ue97fa780-2ce1-49d9-b517-5134c579d83&title=&width=376.8)
登录时环境变量配置文件的调用过程(有时间查看源代码)
![image.png](https://img-blog.csdnimg.cn/img_convert/c0fdc29566d1e62a5d41c431ff8ab143.png#averageHue=#f1f1f1&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=495&id=u8c45012f&margin=[object Object]&name=image.png&originHeight=619&originWidth=1102&originalType=binary&ratio=1&rotation=0&showTitle=false&size=117974&status=done&style=none&taskId=u8cbc300f-cf98-4a2e-a899-e98a74561a2&title=&width=881.6)

注销时环境变量配置文件
~/.bash_logout
~/bash_history
![image.png](https://img-blog.csdnimg.cn/img_convert/afd4100a7992bf13cea9f0d2353445e9.png#averageHue=#ececec&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=420&id=u0b14ce60&margin=[object Object]&name=image.png&originHeight=525&originWidth=1066&originalType=binary&ratio=1&rotation=0&showTitle=false&size=214273&status=done&style=none&taskId=ucc7e15be-2b74-4c09-9616-bdd5703bfa7&title=&width=852.8)
![image.png](https://img-blog.csdnimg.cn/img_convert/d773a9e0398a38a91182eb149277de0c.png#averageHue=#efefef&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=336&id=u8625e34a&margin=[object Object]&name=image.png&originHeight=420&originWidth=1062&originalType=binary&ratio=1&rotation=0&showTitle=false&size=212521&status=done&style=none&taskId=u1fdbaa81-9245-40eb-a93e-257dc5050fd&title=&width=849.6)


shell编程

基础正则表达式

正则表达式和通配符的区别

通配符的作用是完全匹配文件名用的,经常和ls,cp,find命令使用;而正则表达式是部分匹配文件中的内容使用的,它经常了配合grep,awk,sed

基础正则表达式

![image.png](https://img-blog.csdnimg.cn/img_convert/d2f9ad74f543add631f1141ce8636204.png#averageHue=#e2e2e2&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=512&id=u5f89c9ae&margin=[object Object]&name=image.png&originHeight=640&originWidth=1059&originalType=binary&ratio=1&rotation=0&showTitle=false&size=381096&status=done&style=none&taskId=ubc5bea88-ef6a-4461-9047-cc7c5020495&title=&width=847.2)

字符截取命令

grep(提取行)
cut命令(提取列)
![image.png](https://img-blog.csdnimg.cn/img_convert/05b084e1829577074f1d9cf8baf37130.png#averageHue=#f2f2f2&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=242&id=u18960193&margin=[object Object]&name=image.png&originHeight=303&originWidth=1026&originalType=binary&ratio=1&rotation=0&showTitle=false&size=145448&status=done&style=none&taskId=u262bfac2-15f0-4458-8c09-14fa4b0de1f&title=&width=820.8)
printf命令(格式化输出命令)
![image.png](https://img-blog.csdnimg.cn/img_convert/ef05b0f29adb0a949575f51329a8cda8.png#averageHue=#eaeaea&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=289&id=udf581aa0&margin=[object Object]&name=image.png&originHeight=361&originWidth=1031&originalType=binary&ratio=1&rotation=0&showTitle=false&size=153709&status=done&style=none&taskId=ud7167638-9ee5-48db-a421-5f745b6581b&title=&width=824.8)
![image.png](https://img-blog.csdnimg.cn/img_convert/5a0bc42f5c19f9c81eb1eaa5c8943a35.png#averageHue=#f1f1f1&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=441&id=u9d449784&margin=[object Object]&name=image.png&originHeight=551&originWidth=1013&originalType=binary&ratio=1&rotation=0&showTitle=false&size=174400&status=done&style=none&taskId=u75240230-abcb-452c-be21-69dbad1138b&title=&width=810.4)
awk命令(截取符合条件的列):
![image.png](https://img-blog.csdnimg.cn/img_convert/33764af94b97e296d321fa5d08337b36.png#averageHue=#f1f1f1&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=369&id=ucd31c506&margin=[object Object]&name=image.png&originHeight=461&originWidth=969&originalType=binary&ratio=1&rotation=0&showTitle=false&size=117490&status=done&style=none&taskId=u1f3117e9-3ae3-42ea-829c-47738dd8158&title=&width=775.2)
条件:可以用于做逻辑判断,也可以是BEGIN,END这样的在处理开始之前或者处理完成之后执行的
动作:大多使用print或者printf
还可以使用{FS=‘.’}来指定分割符

sed命令
sed是一个轻量级的流编辑器,主要用来对数据进行,追加,新增,删除,替换等操作。它与vi最大的不同在于vi只能够对文件惊醒操作,而sed可以配合管道符进行使用。
![image.png](https://img-blog.csdnimg.cn/img_convert/31db44abb1ad5d325f3ead6dbcdb368c.png#averageHue=#efefef&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=380&id=u964ac2fd&margin=[object Object]&name=image.png&originHeight=475&originWidth=988&originalType=binary&ratio=1&rotation=0&showTitle=false&size=214211&status=done&style=none&taskId=u2e89c157-fd3c-4734-b9e4-bf96e686adc&title=&width=790.4)
![image.png](https://img-blog.csdnimg.cn/img_convert/e8d4ddf049a0026686846c61d35ee56e.png#averageHue=#f1f1f1&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=446&id=u6fff3433&margin=[object Object]&name=image.png&originHeight=557&originWidth=1121&originalType=binary&ratio=1&rotation=0&showTitle=false&size=250120&status=done&style=none&taskId=u51596c46-3c53-4c4a-b6f5-02aaedc2d94&title=&width=896.8)

字符处理命令

sort命令

![image.png](https://img-blog.csdnimg.cn/img_convert/d6c68c5028ffdb3e87ae92f4afc16637.png#averageHue=#f1f1f1&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=478&id=udead795f&margin=[object Object]&name=image.png&originHeight=597&originWidth=1013&originalType=binary&ratio=1&rotation=0&showTitle=false&size=209607&status=done&style=none&taskId=u26391189-644a-4935-8986-df57b541cea&title=&width=810.4)

wc命令

![image.png](https://img-blog.csdnimg.cn/img_convert/c5de9bedb21f8066ab50607de0d1437a.png#averageHue=#f5f5f5&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=449&id=u0941ec9b&margin=[object Object]&name=image.png&originHeight=561&originWidth=1014&originalType=binary&ratio=1&rotation=0&showTitle=false&size=129204&status=done&style=none&taskId=u3db9adbf-0338-431d-84da-8e5809782ae&title=&width=811.2)

条件判断

文件类型判断
![image.png](https://img-blog.csdnimg.cn/img_convert/a9b962e8dfddcfd00044cbb7afef1678.png#averageHue=#eaeaea&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=530&id=ua11cbd8b&margin=[object Object]&name=image.png&originHeight=663&originWidth=1128&originalType=binary&ratio=1&rotation=0&showTitle=false&size=483747&status=done&style=none&taskId=u6809ad9c-33cd-4a36-80bb-05c647b9f66&title=&width=902.4)
判断文件的权限
![image.png](https://img-blog.csdnimg.cn/img_convert/67aba993de16c2ec12b64978475003a2.png#averageHue=#e6e5e3&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=443&id=ub7abdcd2&margin=[object Object]&name=image.png&originHeight=554&originWidth=1080&originalType=binary&ratio=1&rotation=0&showTitle=false&size=457334&status=done&style=none&taskId=u8ddc3f03-6c05-4dac-8367-1fb18a053b7&title=&width=864)
两个文件之间的比较
![image.png](https://img-blog.csdnimg.cn/img_convert/b0e1d44e93c8967c0c51c031e796679a.png#averageHue=#ecebeb&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=432&id=u34c13e60&margin=[object Object]&name=image.png&originHeight=540&originWidth=1072&originalType=binary&ratio=1&rotation=0&showTitle=false&size=182088&status=done&style=none&taskId=u22faa4b1-5b9b-48d6-aaa4-58fe31678e9&title=&width=857.6)
两个数值的比较
![image.png](https://img-blog.csdnimg.cn/img_convert/5b3f3aa44c086777e22e6ab007837861.png#averageHue=#e9e9e9&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=422&id=u808b6f67&margin=[object Object]&name=image.png&originHeight=528&originWidth=1046&originalType=binary&ratio=1&rotation=0&showTitle=false&size=270568&status=done&style=none&taskId=uda59fffb-fe0c-4e5e-885c-061527217f7&title=&width=836.8)
字符串的比较
![image.png](https://img-blog.csdnimg.cn/img_convert/09403e55e70c4d28f797fc4b5d5b78c1.png#averageHue=#ededed&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=411&id=u7e334812&margin=[object Object]&name=image.png&originHeight=514&originWidth=1042&originalType=binary&ratio=1&rotation=0&showTitle=false&size=161727&status=done&style=none&taskId=ud7c33b6e-cc89-450c-8ab1-223d02b051f&title=&width=833.6)
多组判断方式
![image.png](https://img-blog.csdnimg.cn/img_convert/5f99a82661efd26e0f18cd14ff449814.png#averageHue=#efefef&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=391&id=u1f843a7c&margin=[object Object]&name=image.png&originHeight=489&originWidth=1031&originalType=binary&ratio=1&rotation=0&showTitle=false&size=163956&status=done&style=none&taskId=uad8a128d-f94e-49b4-9b0f-9ef2df0d958&title=&width=824.8)
判断的方式
![image.png](https://img-blog.csdnimg.cn/img_convert/cb6c70fe74838bec268c32b682764126.png#averageHue=#f5f5f5&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=380&id=u966c7530&margin=[object Object]&name=image.png&originHeight=475&originWidth=1013&originalType=binary&ratio=1&rotation=0&showTitle=false&size=107665&status=done&style=none&taskId=u39ebbd10-4e91-4ff0-9c74-9c5438141ec&title=&width=810.4)

流程控制语句

if

if语句
![image.png](https://img-blog.csdnimg.cn/img_convert/216ead6db5d155c68cbc1a38912ad1a0.png#averageHue=#fbf8f8&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=448&id=u959518dd&margin=[object Object]&name=image.png&originHeight=560&originWidth=825&originalType=binary&ratio=1&rotation=0&showTitle=false&size=94705&status=done&style=none&taskId=u97616284-5c5e-46d1-8be3-a8258eb8786&title=&width=660)
if…else…语句
![image.png](https://img-blog.csdnimg.cn/img_convert/6223144083149ee771094a8c20e634f9.png#averageHue=#f6f6f6&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=382&id=u7e3fcfc3&margin=[object Object]&name=image.png&originHeight=477&originWidth=944&originalType=binary&ratio=1&rotation=0&showTitle=false&size=91997&status=done&style=none&taskId=u01337295-d788-4d2e-ab9a-23c296f9c3d&title=&width=755.2)
多分枝语句
![image.png](https://img-blog.csdnimg.cn/img_convert/b90c9b034dbad6521245ff483695edc8.png#averageHue=#f4f3f2&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=453&id=u6b281ac5&margin=[object Object]&name=image.png&originHeight=566&originWidth=845&originalType=binary&ratio=1&rotation=0&showTitle=false&size=132268&status=done&style=none&taskId=u81acee6e-13c6-40e1-85be-2436fc1ebb8&title=&width=676)

case

![image.png](https://img-blog.csdnimg.cn/img_convert/2a181d3be6a3e4254668db8bb887bb47.png#averageHue=#f7f6f5&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=526&id=u3c366006&margin=[object Object]&name=image.png&originHeight=658&originWidth=936&originalType=binary&ratio=1&rotation=0&showTitle=false&size=172602&status=done&style=none&taskId=u631a12a7-7e81-4261-844d-7b0ab99b9a1&title=&width=748.8)

for循环

![image.png](https://img-blog.csdnimg.cn/img_convert/4b8a2bc6feedd4a49a4edef1f3535f79.png#averageHue=#fafafa&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=320&id=u7eb06972&margin=[object Object]&name=image.png&originHeight=400&originWidth=843&originalType=binary&ratio=1&rotation=0&showTitle=false&size=47366&status=done&style=none&taskId=u36e954ed-5f98-4cc6-a952-b7c05927058&title=&width=674.4)
![image.png](https://img-blog.csdnimg.cn/img_convert/9f0d5d68596e9140999afa7a43aa6288.png#averageHue=#f6f6f6&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=237&id=ufee9c0f1&margin=[object Object]&name=image.png&originHeight=296&originWidth=898&originalType=binary&ratio=1&rotation=0&showTitle=false&size=75676&status=done&style=none&taskId=ue5288cf7-00f5-4e69-8360-1401ff08bd5&title=&width=718.4)

while

![image.png](https://img-blog.csdnimg.cn/img_convert/ddadb3ba4fed029e1cfee18491d9b78b.png#averageHue=#f7f7f7&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=257&id=uf6216f4a&margin=[object Object]&name=image.png&originHeight=321&originWidth=697&originalType=binary&ratio=1&rotation=0&showTitle=false&size=45624&status=done&style=none&taskId=udf8fbd8c-2488-4d46-888b-d3d11e7c47d&title=&width=557.6)

until(只要条件判断式不满足条件就一直循环)

![image.png](https://img-blog.csdnimg.cn/img_convert/5a9e872f6d42db14cc78e87ca67ade6a.png#averageHue=#fafafa&clientId=ud7cbbb2b-830c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=245&id=u837d14ff&margin=[object Object]&name=image.png&originHeight=306&originWidth=623&originalType=binary&ratio=1&rotation=0&showTitle=false&size=44594&status=done&style=none&taskId=u181a39ef-6678-4f3b-98a5-585ea32ceb4&title=&width=498.4)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: b'shell脚本基础知识'是指对b'shell脚本'进行基础掌握的知识,包括b'shell脚本'的语法、变量、流程控制、函数等基础内容。了解b'shell脚本基础知识,可以帮助用户更好地编写和运行b'shell脚本',提高工作效率和准确性。 ### 回答2: Shell是一种可以执行Linux命令的脚本语言,是Linux系统中必不可少的一部分。Shell脚本是一种文本文件,其扩展名通常为.sh或.bashShell脚本可以编写一些自动化的任务,使得Linux操作更加方便和高效。 Shell脚本基础知识包括以下内容: 1. Shell脚本的创建和执行 Shell脚本可以使用vi或其他文本编辑器创建,创建完成后用chmod命令设置为可执行权限。执行脚本有两种方式:直接输入脚本名,或用bash命令执行脚本。例如,执行名为test.sh的脚本,可用以下两种方式: ./test.sh bash test.sh 2. Shebang行 Shell脚本的第一行需要写上Shebang行,指定解释器的路径。在Linux系统中,默认的解释器为/bin/bash。因此,Shebang行通常是: #!/bin/bash 3. 变量和环境变量 Shell脚本中使用的变量需要先定义,然后才能使用。定义变量的方式为: 变量名=变量值 变量名前面必须添加美元符号$,才能获取到变量的值。可以使用echo命令输出变量值,例如: name="tom" echo $name 环境变量是一种全局变量,可以在Shell脚本中使用。Linux系统中有很多预定义的环境变量,例如: $HOME:当前用户的家目录。 $PATH:程序搜索路径。 $USER:当前登录用户的用户名。 4. 控制流语句和函数 Shell脚本支持各种控制流语句,例如if、for、while等。函数的定义和使用也和其他编程语言类似,例如: function_name(){ commands } function_name 5. 输入和输出重定向 在Shell脚本中,可以使用重定向符号>和<来重定向输入和输出。例如,将输出重定向到文件中,可以使用: echo "hello" > output.txt 将输入重定向到文件中,可以使用: read input_var < input.txt Shell脚本基础知识是编写自动化任务的基础,了解和掌握这些知识可以使得Shell脚本更加高效和便捷。 ### 回答3: Shell脚本是一个用来编写命令行脚本脚本语言。脚本语言和编译型语言不同,它的代码不需要被编译成可执行文件,而是直接解释执行。 Shell脚本的特点是读写文本文件,执行系统命令。在Unix/Linux操作系统中,Shell是最基本的界面,与其他操作系统类似,用户可以在Shell界面下输入各种命令来控制和操作计算机系统。 Shell脚本基础知识包括: 1. Shell脚本的文件格式,一般以.sh作为文件扩展名,脚本的执行需要给定执行权限(chmod +x filename.sh)。 2. 开始行(Shebang):开头的两个字符#!(Shebang)指定执行本脚本文件所使用的Shell解释器。一般来说,使用Bash解释器,开始行应该这样写:#!/bin/bash 3. 变量:在Shell脚本中,用$符号表示,变量名和等号之间不能有空格。例如:var=123 4. 数组:在Shell脚本中,用小括号()表示,使用花括号{}和下标来引用数组中的元素。例如:arr=(1 2 3) echo ${arr[1]} 5. 流程控制语句:if、while、case、for等,用于实现分支和循环等控制流程。 6. 命令行参数:在执行脚本时从命令行传入,用$符号表示。例如:在命令行执行:./test.sh arg1 arg2,则脚本中可以用$1表示arg1,$2表示arg2。 7. 读取键盘输入:使用read命令,例如:read name,表示从键盘读取一行文本并存储到变量name中。 8. 函数:用于将一些重复的命令封装起来,实现代码复用。例如: function test(){ echo "hello world"}。 Shell脚本Unix/Linux操作系统的基础,掌握Shell脚本基础知识是很重要的。在实践中不断熟练掌握Shell脚本,可以提高个人的工作效率和工作质量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值