Linux | Shell脚本从入门到实战
之前在工作中经常有涉及到shell脚本的使用,也有在捞日志等场景下使用,但一直感觉知识不太系统化,遂在B站上找来尚硅谷的课程恶补一下~;
以下内容均来自尚硅谷视频课程笔记
一、Shell脚本入门
1. 脚本格式
指定解析器:脚本以 #!/bin/bash
开头
2. 脚本内容
创建shell脚本文件
[root@VM-0-3-centos LearnSource]# touch helloworld.sh
[root@VM-0-3-centos LearnSource]# vi helloworld.sh
脚本文件内容(输出helloworld)
#!/bin/bashecho "helloworld"
3. 运行脚本
3.1 采用 bash / sh + 路径执行(不需要+x权限)
[root@VM-0-3-centos LearnSource]# sh helloworld.sh
helloworld
[root@VM-0-3-centos LearnSource]# bash helloworld.sh
helloworld
3.2 采用 ./路径执行 (需要可执行+x权限)
Linux的每个文件一般都有三个权限 r–读,w–写,x–执行,其分别对应的数值为4,2,1
777权限中的三个数字分别代表不同使用对象:User、Group、Other
修改权限使用 chmod xxx filename
- 赋予 helloworld.sh 脚本 +x 权限(777:可执行权限)
- 直接 ./ 执行脚本
[root@VM-0-3-centos LearnSource]# ./helloworld.sh
-bash: ./helloworld.sh: Permission denied
[root@VM-0-3-centos LearnSource]# chmod 777 helloworld.sh
[root@VM-0-3-centos LearnSource]# ./helloworld.sh
helloworld
二、Shell中的变量与运算符
1. 系统变量
- echo $HOME
- echo $PWD
- echo $SHELL
- echo $USER
[root@VM-0-3-centos LearnSource]# echo $HOME
/root
[root@VM-0-3-centos LearnSource]# echo $PWD
/data/LearnSource
[root@VM-0-3-centos LearnSource]# echo $USER
root
[root@VM-0-3-centos LearnSource]# echo $SHELL
/bin/bash
2. 自定义变量
2.1 基本语法
- 定义变量:变量=值
- 撤销变量:unset 变量
- 声明静态变量:readonly变量,注意:不能unset
2.2 变量定义规则
- 变量名称可以由字母、数字和下划线组成,但是不能以数字开头,环境变量名建议大写。
- 等号两侧不能有空格
- 在bash中,变量默认类型都是字符串类型,无法直接进行数值运算。
- 变量的值如果有空格,需要使用双引号或单引号括起来。
特殊的:可把变量提升为全局环境变量,可供其他Shell程序使用
升级为全局变量: export 变量名
[root@VM-0-3-centos LearnSource]# cat helloworld.sh
#!/bin/bashecho "helloworld"
echo $B
[root@VM-0-3-centos LearnSource]# bash helloworld.sh
helloworld[root@VM-0-3-centos LearnSource]# export B
[root@VM-0-3-centos LearnSource]# bash helloworld.sh
helloworld
0328
3. 特殊变量
3.1 $n
功能描述:
n为数字,$0
代表该脚本名称,$1-$9
代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如${10}
[root@VM-0-3-centos test]# bash parameter.sh zhang
parameter.sh zhang
[root@VM-0-3-centos test]# bash parameter.sh zhang love
parameter.sh zhang love
[root@VM-0-3-centos test]# bash parameter.sh zhang love you
parameter.sh zhang love you
[root@VM-0-3-centos test]# bash parameter.sh zhang love you hi
parameter.sh zhang love you
[root@VM-0-3-centos test]# cat parameter.sh
#!/bin/bashecho "$0 $1 $2 $3"
3.2 $#
功能描述:获取所有输入参数个数,常用于循环
[root@VM-0-3-centos test]# bash parameter.sh zhang love
parameter.sh zhang love
2
[root@VM-0-3-centos test]# cat parameter.sh
#!/bin/bashecho "$0 $1 $2 $3"echo $#
3.3 $* $@
$*
(功能描述:这个变量代表命令行中所有的参数, $*
把所有的参数看成一个整体)
$@
(功能描述:这个变量也代表命令行中所有的参数,不过 $@
把每个参数区分对待)
3.4 $?
功能描述:
最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了。
4. 运算符
$((运算式))
或$[运算式]
- expr + , - , *, /, % 加,减,乘,除,取余
注意:expr运算符间要有空格
三、条件判断与流程控制
1. 条件判断
1.1 基本语法
[ condition ]
(condition前后要有空格)
1.2 常用判断条件
两整数之间比较
= 字符串比较
-lt 小于(less than) -le 小于等于(less equal)
-eq 等于(equal) -gt 大于(greater than)
-ge 大于等于(greater equal) -ne 不等于(Not equal)按文件权限判断
-r 有读的权限(read) -w 有写的权限(write)
-x 有执行的权限(execute)按文件类型判断
-f 文件存在并且是一个常规的文件(file)
-e 文件存在(existence) -d 文件存在并是一个目录(directory)
1.3 实操
# 1. 判断大小
[root@VM-0-3-centos test]# [ 23 -ge 33 ]
[root@VM-0-3-centos test]# echo $?
1
# 2. 判断文件是否有写权限
[root@VM-0-3-centos LearnSource]# [ -w helloworld.sh ]
[root@VM-0-3-centos LearnSource]# echo $?
0
# 3. 是否存在文件路径
[root@VM-0-3-centos LearnSource]# [ -e /data/LearnSource/test ]
[root@VM-0-3-centos LearnSource]# echo $?
0
2. 流程控制
2.1 if
基本语法:
# 写法1
if [ 条件判断式 ];then 程序
elif [ 条件判断式 ];then 程序
fi # 写法2
if [ 条件判断式 ]
then 程序
elif [ 条件判断式 ]
then 程序
fi
注意:if后要有空格
实操
[root@VM-0-3-centos test]# cat if.sh
#!/bin/bashif [ $1 -eq "1" ]
thenecho "first parament is 1"
elif [ $1 -eq "2" ]
thenecho "first parament is 2"
fi[root@VM-0-3-centos test]# bash if.sh 1
first parament is 1
[root@VM-0-3-centos test]# bash if.sh 2
first parament is 2
2.2 case语句
基本语法:
case $变量名 in "值1") 如果变量的值等于值1,则执行程序1 ;; "值2") 如果变量的值等于值2,则执行程序2 ;; …省略其他分支… *) 如果变量的值都不是以上的值,则执行此程序 ;;
esac
注意:
- case行尾必须为单词“in”,每一个模式匹配必须以右括号“)”结束
- 双分号“;;”表示命令序列结束,相当于java中的break
- 最后的“*)”表示默认模式,相当于java中的default
2.3 for
基本语法
# 写法1
for (( 初始值;循环控制条件;变量变化 )) do 程序 done
# 写法2
for 变量 in 值1 值2 值3… do 程序 done
实操
# 写法1. 从1加到100
[root@VM-0-3-centos test]# cat for1.sh
#!/bin/bashs=0
for ((i=0;i<=100;i++))
dos=$[$s+$i]
doneecho $s[root@VM-0-3-centos test]# bash for1.sh
5050# 写法2. 打印所有输入参数
[root@VM-0-3-centos test]# cat for2.sh
#!/bin/bashfor i in $*doecho "param is $i"done
[root@VM-0-3-centos test]# bash for2.sh param1 param 2
param is param1
param is param
param is 2
2.4 while
基本语法
while [ 条件判断式 ] do 程序done
四、Shell工具(重点)
1. cut
剪切数据
1.1. 基本语法
cut [选项参数] filename
-f : 列号,提取第几列
-d : 分隔符,按照既定分隔符分割列
1.2. 实操
[root@VM-0-3-centos test]# cat cut.txt
line1,content1,matrix1.3
line2,content2,matrix2.3
line3,content3,matrix3.3
line4,content4,matrix4.3
line5,content5,matrix5.3# 根据 , 进行分割 取其中的第2列
[root@VM-0-3-centos test]# cut -d "," -f 2 cut.txt
content1
content2
content3
content4
content5
2. sed
流编辑器,每次处理一行内容
基本语法
sed [选项参数] ‘command’ filename
选项参数:
-e : 直接在指令行模式上进行sed的动作编辑
命令功能:
a 新增,a的后面可以接字串,在下一行出现
d 删除
s 查找并替换
[root@VM-0-3-centos test]# cat sed.txt
dong shen
guan zhen
wo wo
lai laile le# 在第2行后插入 test txt 文本
[root@VM-0-3-centos test]# sed "2a test txt" sed.txt
dong shen
guan zhen
test txt
wo wo
lai laile le# 删除 sed.txt 文件中包含 wo 的行
[root@VM-0-3-centos test]# sed '/wo/d' sed.txt
dong shen
guan zhen
lai laile le
3. awk(重点)
该部分内容主要参考:https://www.cnblogs.com/ginvip/p/6352157.html
awk对输入文件中的每一行都执行该脚本
3.1 基本用法
awk [选项参数] '{pattern + action} {pattern2 + action2} ...' {filenames}
pattern:表示AWK在数据中查找的内容,就是匹配模式
action:在找到匹配内容时所执行的一系列命令
选项参数:
-F :指定输入文件分隔符
-v :赋值一个用户定义变量
3.2 内置变量
变量 | 说明 |
---|---|
FILENAME | 文件名 |
NR | 已读的记录数 |
NF | 浏览记录的域的个数(切割后,列的个数) |
3.3 实操
示例一、查看test.txt文件(100行)内第20到第30行的内容
[root]# awk '{if(NR>=20 && NR<=30) print $1}' test.txt
示例二、筛选内容
# 从该文件中过滤出'Poe'字符串与33794712,最后输出的结果为:Poe 33794712
[root@Gin scripts]# cat test.txt
I am Poe,my qq is 33794712
[root@Gin scripts]# awk -F '[ ,]+' '{print $3" "$7}' test.txt
Poe 33794712
3.4 BEGIN 和 END 模块
BEGIN模块:只在初始化时执行一次,初始化其它在程序中以后会引用的全局变量
END模块:只在结束时执行一次,用于执行最终计算或打印应该出现在输出流结尾的摘要信息
示例、统计 /etc/passwd 的账户人数
[root@Gin scripts]# awk 'BEGIN {count=0;print "[start] user count is ",count} {count=count+1;print $0} END{print "[end] user count is ",count}' passwd
[start] user count is 0
root:x:0:0:root:/root:/bin/bash
...................................................................
[end] user count is 27
4. sort(重要)
将数据排序
4.1 基本语法
sort 选项 参数
选项列表
选项 | 说明 |
---|---|
-n | 依照数值的大小排序 |
-r | 以相反的顺序来排序 |
-t | 设置排序时所用的分隔字符 |
-k | 指定需要排序的列 |
参数
指定待排序的文件列表
4.2 实操
按照 “ :” 分割后的第三列倒序排序
$ sort -t : -nrk 3 sort.sh
bb:40:5.4
bd:20:4.2
cls:10:3.5
xz:50:2.3
ss:30:1.6
参考资料:
Linux三剑客之awk表达式
【尚硅谷】Shell脚本从入门到实战
Linux | Shell脚本从入门到实战相关推荐
- linux shell脚本,入门到实战详解(二)
三.shell 编程流程控制语句 (点击进入Linux实战课程↑↑) 3.1 if 条件语句介绍 3.1.1 常用的单/双分支 if 条件语句: # If条件判断语句,通常以if开头,fi结尾.也可加 ...
- 【shell笔记】Linux Shell脚本编程入门知识点全面涵盖
本文是我对白树明老师shell课程笔记的总结,课程链接:https://www.bilibili.com/video/BV1j541157Sr?from=search&seid=9757674 ...
- Shell脚本从入门到实战
Shell 文章目录 Shell shell概述 Shell脚本入门 1. 脚本格式 2. 第一个Shell脚本:helloworld 3. 第二个Shell脚本:多命令处理 Shell中的变量 系统 ...
- Linux Shell 脚本编程 入门
查看当系统有什么shell解释器:cat /etc/shells CentOs默认用的是 bash : echo $SHELL Shell 脚本入门 1.脚本格式: 脚本要以 #!/bin ...
- Shell 脚本 快速入门到实战 -02
7 流程控制 (重点) 7.1 case 语句 7.2 case语句 7.3 for 循环 7.4 while循环 8 read 读取控制台输入 9 函数 9.1 系统函数 9 ...
- Linux Shell脚本编程入门
Shell Script Shell 脚本是包含一系列命令的文本本当:读取执行该文本文档就可以执行文档里面的命令: 常用于执行批量事务处理.重复性操作.自动化运维.定时执行任务等 编写一个简单 She ...
- Linux shell 脚本之shift 命令实战
位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1. 非常 ...
- Linux shell脚本入门到实战详解(一)
Linux shell脚本入门到实战详解 一.shell 入门简介 1.1 什么是 shell # 为啥介绍shell 上次出了一篇linux 命令详解,得到了很多小伙伴的认可,有部分粉丝私信我,让我 ...
- linux运维实战练习及linux shell脚本、awk、sed工具命令学习总结
一.linux shell 脚本 1.描述shell程序的运行原理(可附带必要的图形说明): Linux系统的shell作为操纵系统的外壳,为用户提供使用操纵系统的接口.它是命令语言.命令解释程序及程 ...
- Linux Shell脚本入门--wget 命令用法详解
Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...
最新文章
- CSS之常用选择器(元素、id、类、通配选择器)
- gscatter in matlab Scatter plot by group machine learning and statics tool box required
- Spring MVC错误处理示例
- matlab铁路平板车装货问题,两辆铁路平板车的装货问题最终论文
- 但自去年封测后的cqbgbbs
- callback的实现
- Log4J 日志的异步类解读(lAsyncAppender)
- 为什么普通红包自己不能领_为什么行驶证和驾驶证不能放在车上?别给自己找麻烦...
- Jeecg Boot 2.2.1 版本发布,基于SpringBoot的低代码平台
- SqList *L 和 SqList * L的一些理解以及多重指针的简单应用
- Linux命令行设置行数,Linux设置history命令显示行数以及时间
- 计算机中寄存器的定义,寄存器,寄存器是什么意思
- 看完《长安十二时辰》惊呆了!难道唐朝就已经有大数据和云计算?
- 台式机通过笔记本的WIFI连接上网,并实现笔记本对台式机的远程连接
- 视频教程-深度学习与TensorFlow 2入门实战-深度学习
- Hive DDL操作命令实验
- 数博会”为何落户贵州?
- 多串口接收数据并显示-自定义串口通信类-调用委托显示数据-保存数据-实例:10串口接收数据并显示加保存
- 台达B3伺服电机485通讯的调试过程
- 使用 Visual Basic 通过 32 位 地址访问内存(中英对照)