当前位置:网站首页>Shell script quick start to actual combat -02
Shell script quick start to actual combat -02
2022-07-31 19:03:00 【51CTO】
7 流程控制 (重点)
7.1 case 语句
7.2 case语句
7.3 for 循环
7.4 while循环
8 read 读取控制台输入
9 函数
9.1 系统函数
9.2 自定义函数
10 Shell 工具(重点)
10.1 cut
10.2 sed
10.3 awk
11 企业真实面试题
11.1 京东
11.2 搜狐&和讯网
11.3 新浪
11.4 金和网络
7 流程控制 (重点)
7.1 case 语句
- 基本语法
- 注意:
- [ 条件判断式 ],中括号和条件判断式之间必须有空格
- if后要有空格
- 实例
7.2 case语句
- 基本语法:可以类比java的switch case break deflaut那一套
- 案例
- 输入一个数字,如果是1,则输出banzhang,如果是2,则输出fubanzhang,如果是其他,output classmates
7.3 for 循环
- 基本语法:可以类比java的for循环,Change the parentheses to do,done
- 案例
- 从1加到100
- 第二种for语法:Works with discrete values
- 案例
打印输入的所有参数
加个“”就不同了
7.4 while循环
- 基本语法
- 实例
- 1+100
8 read 读取控制台输入
- 基本语法
- 实例
- 提示7 秒内,Read the name entered in the console
9 函数
9.1 系统函数
- basename基本语法
- 选项:suffix为后缀,如果suffix被指定了,basename会将pathname去掉suffix
- 功能
- Remove all suffixes including the last one(‘/’)字符,然后将字符串显示出来
- 干嘛用的啊:After getting the path or file name,can be assigned to variables,Do more follow up,实现自动化
- 实例
- dirname 基本语法
功能:从给定的包含绝对路径的文件名中出去文件名(非目录的部分),然后返回剩下的路径(目录的部分)
使用:basename和dirname一起使用
- 实例
9.2 自定义函数
- 基本语法
- 注意点:
- 必须在调用函数地方之前,先声明函数.shellThe script will only run line by line.
- Function feedback value,只能通过$?系统变量获得,可以显示加return,如果不加,Just run the result with the last command,作为返回值.
- 实例
- 计算两个输入参数的和
10 Shell 工具(重点)
10.1 cut
- 功能:剪,Specifically, it is responsible for cutting data in the file.Trimmed for ease of output
- 基本用法:
- 实例
- Take the first column out
- Take the first2,3列
- Just one wide(guang)字呢
- 选取系统PATH变量值,第五个":"开始后的所有路径
- 切个ip地址玩玩
eth0的ip的cut过程
10.2 sed
- 功能:sed是一种流编辑器,它一次处理一行内容.The currently processed line is stored in a temporary buffer,称为“模式空间”,Push the screen after processing.
- 基本语法
- 选项参数说明
-e:Do it directly in command line modesed的动作编辑
- 命令功能描述
a:新增,followed by the string,在下一行出现
d:删除
s:查找并替换
- 案例
- 将”believe“这个单词插入到sed.txt第二行下,并打印,But the original file has not changed
- 删除sed.txt中所有包含wo的行
- 将sed.txt中的wo替换成ni
- 将sed.txtThe second line in the file is removed and replaced with ini
10.3 awk
Regular expression content added: (73条消息) 正则表达式全解析+常用示例_Moyao's blog-CSDN博客_Regular expression line endings
正则表达式测试网站Regex101的使用方法: 正则表达式测试网站Regex101的使用方法 - 知乎 (zhihu.com)
check again,No more learning: 【韩顺平讲Java】Java 正则表达式专题 -正则 正则表达式 元字符 限定符 Pattern Matcher 分组 捕获 反向引用等_哔哩哔哩_bilibili
功能:一个强大的文本分析工具,把文件逐行的读入,以空格为默认分隔符将每行切片, 切开的部分在进行分析处理
基本语法
pattern:表示AWK在数据中查找的内容,匹配模式,Equivalent to regular matching
action:The command to execute when a match is found
- 选项参数说明
-F:指定输入文件分隔符 F大写
-v:赋值一个用户定义变量 v小写
内置变量
FILENAME:文件名
NR:已读的记录数
NF:浏览记录的域的个数
- 案例
- 搜索passwd文件以root关键字开头的所有行,并输出改行的第7列
- /^Regular start
- 搜索passwd文件以root关键字开头的所有行,并输出改行的第1列和第7列,中间以”,“号分割
- 显示passwd文件的第1列和第7列,以逗号分隔, 且在所有行前面添加列名user,shell在最后一行添加”fenfen,/bin/zuibang“
- 将passwd文件中的用户id增加1并输出
- 内置变量:统计passwd文件名,每行的行号,每行的列数
- 切割ip
eth0版本
- 查询sed .txt中空行所在的行号
- /^$正则:There is no content at the beginning and the end is a space
10.4 sort
- 功能:sort命令在Linux中非常有用,He sorts the files,并将排序结果输出
- 基本语法
- 实例
- Sort by the second column value
11 企业真实面试题
11.1 京东
- 使用Linux命令查询file1中空行所在的行号
- 有文件chengji.txt内容如下
张三 40
李四 50
王五 60
使用Linux命令计算第二列的和并输出
11.2 搜狐&和讯网
- Shell脚本中如何检查一个文件是否存在?If there is no change how to deal with it
11.3 新浪
- 问题:用Shell写一个脚本,Sorts an unordered column of numbers and adds up the sum
11.4 金和网络
- 问题:请用shell脚本写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符”shen“的文件名称
边栏推荐
- 【码蹄集新手村600题】不通过字符数组来合并俩个数字
- SiC MOSFET的短路特性及保护
- How programmers learn open source projects, this article tells you
- leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
- Handling Write Conflicts under Multi-Master Replication (1)-Synchronous and Asynchronous Conflict Detection and Conflict Avoidance
- Carbon教程之 基本语法入门大全 (教程)
- MySQL---基本的select语句
- 【AcWing】The 62nd Weekly Match 【2022.07.30】
- The new telecom "routine", my dad was tricked!
- Mobile web development 02
猜你喜欢
全平台GPU通用AI视频补帧超分教程
【PIMF】OpenHarmony 啃论文俱乐部—盘点开源鸿蒙三方库【3】
1161. 最大层内元素和 : 层序遍历运用题
35道MySQL面试必问题图解,这样也太好理解了吧
请问我的这段sql中sql语法哪里出了错
Introduction of Jerry voice chip ic toy chip ic_AD14NAD15N full series development
AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
九齐ny3p系列语音芯片替代国产方案KT148A性价比更高420秒长度
MySQL---多表查询
随机推荐
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
idea中搜索具体的字符内容的快捷方式
How can we improve the real yourself, become an excellent architect?
如何才能真正的提高自己,成为一名出色的架构师?
京东获取商品历史价格信息 API
如何识别假爬虫?
华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
go基础部分学习笔记记录
京东按关键字搜索商品 API
npm 更改为淘宝镜像的方法[通俗易懂]
Chinese encoding Settings and action methods return values
【AcWing】第 62 场周赛 【2022.07.30】
35道MySQL面试必问题图解,这样也太好理解了吧
Handling Write Conflicts under Multi-Master Replication (1)-Synchronous and Asynchronous Conflict Detection and Conflict Avoidance
go记录之——slice
How programmers learn open source projects, this article tells you
Routing interception of WeChat applet
抖音根据关键词取视频列表 API
After Effects tutorial, How to adjust overexposed snapshots in After Effects?
Kotlin coroutines: continuation, continuation interceptor, scheduler