当前位置:网站首页>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“的文件名称
边栏推荐
ojdbc8 "Recommended Collection"- 浅谈网络安全之算法安全
- 【码蹄集新手村600题】通向公式与程序相结合
- 架构师04-应用服务间加密设计和实践
- Linux环境redis集群搭建「建议收藏」
- Shell 脚本 快速入门到实战 -02
- Taobao/Tmall get Taobao password real url API
- Architect 04 - Application Service Encryption Design and Practice
- 【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
- Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure
猜你喜欢
ThreadLocal
这位985教授火了!当了10年博导,竟无一博士毕业!
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!...
Apache EventMesh distributed event-driven multi-runtime
Three.js入门
go mode tidy出现报错go warning “all“ matched no packages
架构实战营模块八作业
Combinatorics Notes (6) Associative Algebra of Locally Finite Partially Ordered Sets, Möbius Inversion Formula
Introduction of Jerry voice chip ic toy chip ic_AD14NAD15N full series development
MySQL - multi-table query
随机推荐
架构师04-应用服务间加密设计和实践
Tkinter 入门之旅
21.支持向量机—核函数的介绍
leetcode 665. Non-decreasing Array
全平台GPU通用AI视频补帧超分教程
leetcode 665. Non-decreasing Array 非递减数列(中等)
【AcWing】The 62nd Weekly Match 【2022.07.30】
rj45 to the connector Gigabit (Fast Ethernet interface definition)
移动web开发02
Combinatorics Notes (6) Associative Algebra of Locally Finite Partially Ordered Sets, Möbius Inversion Formula
【AcWing】The 62nd Weekly Match 【2022.07.30】
九齐ny3p系列语音芯片替代国产方案KT148A性价比更高420秒长度
Routing interception of WeChat applet
基于WPF重复造轮子,写一款数据库文档管理工具(一)
[TypeScript] OOP
中文编码的设置与action方法的返回值
go mode tidy出现报错go warning “all“ matched no packages
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
返回一个零长度的数组或者空的集合,不要返回null
如何识别假爬虫?