Shell编程
目录
一、简介
1.1 Shell介绍
1.2 shell分类
二、常用命令
前提:
2.1 head
2.1.1 -n
2.1.2 -c
2.1.3 打印中间行的数据(面试经常问)
2.2 tail
2.2.1 -c
2.2.2 -n
2.2.3 -f (常用)
2.3 cut
2.3.1 -f 指定获取的列号
2.3.2 -d 指定分割符
2.4 sort 对文本进行排序
2.5 uniq
2.6 wc(word count)
三、变量
3.1 分类
3.2 定义变量
3.3 全局变量
3.4 查看变量
3.5 内置变量
四、数值运算
4.1 支持的运算
4.2 方式一
4.3 方式二
五、条件表达式
5.1 返回值
5.2 文件表达式
5.3 数值操作符
5.4 字符串比较
5.5 逻辑表达式
六、shell脚本格式
一、简介
1.1 Shell介绍
1.2 shell分类
windows系统
cmd.exe 命令提示符
linux 系统
sh/bash/zsh/...
配置环境变量等。
二、常用命令
快捷键
ctrl+a 将光标定位到第一个位
ctrl+e 将光标定位到最后一位
ctrl+l 清屏
前提:
学习命令的使用方式,最好的方式就是查看帮助信息。
xx --help (推荐这种方式)
如查看head命令的使用方式
head --help
想要查看更加详细的帮助文档,使用 man xx
如
man head
查看再详细一点的帮助文档,使用 info xx
help帮助信息分析:
head --help
1、主要查看Usage信息。
Usage: head [OPTION]... [FILE]...
命令中,[]方括号中的信息,是选填的,可加可不加。
2、Print 打印 这个命令的描述
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.
----------打印每个文件的前10行,打印到标准输出(当前屏幕)
2.1 head
默认获取文件前10行
使用方式:
head miao.sql
查看miao.sql文件的前10行数据。
2.1.1 -n
-n, --lines=[-]K print the first K lines instead of the first 10;
with the leading '-', print all but the last
K lines of each file
(以行为单位)可以指定打印前xx行。
打印miao.sql的前5行数据
head -n 5 miao.sql
省略n,直接-数字也生效。如下
head -5 miao.sql
2.1.2 -c
-c, --bytes=[-]K print the first K bytes of each file;
with the leading '-', print all but the last
K bytes of each file
(以字节为单位)打印前xx个字节。
打印cs.sql文件的前3个字节。(一个字符一个字节)
head -c 3 cs.sql
2.1.3 打印中间行的数据(面试经常问)
需求:打印第9行--15行的数据。
head -n 15 cs.sql | tail -n 6
先取出cs.sql 文件中,前15行的内容 head -n 15 cs.sql
通过管道符|,将上一个命令拿到的结果,在取这个结果中的后6行(9-15行)数据
| tail -n 6
2.2 tail
查看帮忙信息
[root@ecs-39233 chenshuai]# tail --help
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.Mandatory arguments to long options are mandatory for short options too.-c, --bytes=K output the last K bytes; or use -c +K to outputbytes starting with the Kth of each file-f, --follow[={name|descriptor}]output appended data as the file grows;an absent option argument means 'descriptor'-F same as --follow=name --retry-n, --lines=K output the last K lines, instead of the last 10;or use -n +K to output starting with the Kth--max-unchanged-stats=N
默认获取文件后10行。
tail cs.sql
默认打印cs.sql文件的最后10行数据
2.2.1 -c
(以字节为单位)打印最后 x个字节。
举例:
打印最后5个字节
tail -c 5 cs.sql
2.2.2 -n
(以行为单位)打印最后 x行的内容。
打印最后 x行的内容。
tail -n 5 cs.sql
打印最后5行的内容。
2.2.3 -f (常用)
动态查看文件数据。
应用场景:在查看日志文件的时候。
tail -f cs.log
2.3 cut
取出文件指定的列。默认以空格或者tab键进行分割(不支持不规则空格)
查看帮助信息 cut --help
[root@ecs-39233 chenshuai]# cut --help
Usage: cut OPTION... [FILE]...
Print selected parts of lines from each FILE to standard output.Mandatory arguments to long options are mandatory for short options too.-b, --bytes=LIST select only these bytes-c, --characters=LIST select only these characters
Usage: cut OPTION... [FILE]...
OPTION ,没有方括号,为必填项。
常用:
-d 指定分隔符
-f 指定获取的列号
举例:
新建test1文件
vim test1
添加以下内容。(tab键进行分割的)
name age address
aaa1 18 beijing
aaa2 20 shanghai
aaa3 22 shenzhen
2.3.1 -f 指定获取的列号
查看第1列的数据
cut -f 1 test1
查看第2列的数据
cut -f 2 test1
同时打印第1列,第2列的数据。
- 是连续区间
,是取单个的列
方式一
cut -f 1,2 test1
方式二
cut -f 1-2 test1
2.3.2 -d 指定分割符
以/etc/passwd文件为例。查看/etc/passwd
cat /etc/passwd
该文件是以冒号:进行分割的。
我们要打印(以冒号:分割)第一列的数据
cut -d":" -f1 /etc/passwd
2.4 sort 对文本进行排序
sort对文本进行排序
2.4.1 默认排序规则:
取每一行的第一个字符,按照ASCII 进行排序的。
sort --help
举例
新建sort.txt文件
vim sort.txt
写入内容
1
22
2
12
11
23
3
12
31
32
对sort.txt文件进行排序
sort srot.txt
查看排序结果,这里不是按照数字大小排序的。而是取每一行的第一个字符,按照ASCII 进行排序的。
2.4.2 -n 按照数字大小排序
sort -n sort.txt
2.4.3 指定某一列进行排序
-k 指定某一列进行排序。(列的划分,默认按照空格划分)
-t 指定分隔符
举例:
sort -t":" -k3 /etc/passwd
先按照:进行列的分割。然后取第3列数据进行排序(排序规则ASCII)
按照数值大小排序 -n
sort -t":" -k3 -n /etc/passwd
2.4.5 倒序排序 -r
举例:
按照数值大小,倒序排序
sort -t":" -k3 -n /etc/passwd
2.5 uniq 去重
uniq 只能对有顺序的文本进行去重