?
本节内容
?
1.shlle简介
2. shell分类
3. 查看shell
4. 第一个shell脚本
5. shell编程常用命令
? 5.1 grep
? 5.2 cut
? 5.3 sort
? 5.4 uniq
? 5.5 seq
? 5.6 tr
6. 课后作业
?
前言
??查看shell
?
Shell?是一个程序,一般都是放在/bin或者/user/bin目录下,当前?Linux?系统可用的?Shell?都记录在/etc/shells文件中。/etc/shells是一个纯文本文件,你可以在图形界面下打开它,也可以使用?cat?命令查看它。
通过?cat?命令来查看当前?Linux?系统的可用?Shell:
$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
?
在现代的?Linux?上,sh?已经被?bash?代替,/bin/sh往往是指向/bin/bash的符号链接。?
如果你希望查看当前?Linux?的默认?Shell,那么可以输出?SHELL?环境变量:
$ echo $SHELL
/bin/bash
?
输出结果表明默认的?Shell?是?bash。?
SHELL是?Linux?系统中的环境变量,它指明了当前使用的?Shell?程序的位置,也就是使用的哪个?Shell。
?
1.4?第一个?Shell 脚本
?
?用?vim?打开?test.sh,编写:
╭─[email?protected] ~
╰─? vim hello_wd.sh
#!/bin/bash
echo "Hello World"
~
?
方法?1:直接用 bash 解释器执行
╭─[email?protected] ~
╰─? bash hello_wd.sh #bash加文件名称
Hello World
?
方法?2:添加可执行权限
╭─[email?protected] ~
╰─? ./hello_wd.sh
zsh: 权限不够: ./hello_wd.sh
╭─[email?protected] ~
╰─? ll 126 ?
总用量 4.0K。
-rw-r--r-- 1 root root 31 5月 29 14:42 hello_wd.sh
╭─[email?protected] ~
╰─? chmod +x hello_wd.sh
╭─[email?protected] ~
╰─? ./hello_wd.sh
Hello World
这种方式默认根据脚本第一行指定的解释器处理,如果没写以当前默认?Shell 解释器执行。?
注意,这里在运行时一定要写成?./test.sh(绝对路径亦可),而不是?test.sh,运行其它二进制的程序也一样,直接写test.sh,Linux 系统会去 PATH(环境变量)?里寻找有没有叫?test.sh 的,而只有 /bin,/sbin,/usr/bin,/usr/sbin 等在 PATH 里,你的当前目录通常不在 PATH 里,所以写成 test.sh 是会找不到命令的,要用 ./test.sh 告诉系统说,就在当前目录找。
?
方法?3:source 命令执行,以当前默认 Shell 解释器执行
?╭─[email?protected] ~
╰─? source hello_wd.sh 127 ?
Hello World
source filename 与 bash filename 及./filename执行脚本的区别
?
? 当shell脚本具有可执行权限时,用bash filename与./filename执行脚本是没有区别得。./filename是因为当前目录没有在PATH中,所以”.”是用来表示当前目录的。
? source filename:这个命令其实只是简单地读取脚本里面的语句依次在当前shell里面执行,没有建立新的子shell。那么脚本里面所有新建、改变变量的语句都会保存在当前shell里面。
? bash filename 重新建立一个子shell,在子shell中执行脚本里面的语句,该子shell继承父shell的环境变量,但子shell新建的、改变的变量不会被带回父shell。
最后一句话什么意思那?
子shell新建变量,在父shell中不会生效:
?
我们可以使用命令pstree查看我们当前所处的位置
需要下载
[[email?protected] ~]# yum search pstree
?[[email?protected] ~]# yum install psmisc -y
[[email?protected] ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─agetty
├─anacron
├─atd
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon
├─gssproxy───5*[{gssproxy}]
├─lvmetad
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd───sshd───bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[{tuned}]
└─vmtoolsd───{vmtoolsd}
?
我们再次执行bash,就会进入到另外一个子shell中
这个时候我们在这个子shell中定义一个变量,发现可以正确打印出来
[[email?protected] ~]# age=25
[[email?protected] ~]# echo $age
25
?
现在我们退出当前的shell,即进入了当前子shell中的父shell中,再次打印我们刚才定义的变量
可以发现现在已经无法获取到我们刚才定义的变量值了。
?
子shell继承父shell的环境变量:
?
我们把环境变量定义到profile的一个子文件中,并使用source执行该文件并生效
打开一个子shell,定义在父shell中的环境变量依然有效
反之,这种操作在子shell中操作,父shell也不能继承
[[email?protected] ~]# vim /etc/profile.d/ken.sh
export name=ken
[[email?protected] ~]# source /etc/profile.d/ken.sh
[[email?protected] ~]# echo $name
ken
[[email?protected] ~]# bash
[[email?protected] ~]# echo $name
Ken
pstree
?
shell编程练习:
?练习1:使用root用户帐号创建并执行test2.sh,实现创建一个shelltest用户,并在其家目录中新建文件try.html。
#!/bin/bash
Useradd shelltest
Touch /home/shelltest/try.html
?练习2:统计当前系统总共有多少用户
#!/bin/bash
Wc -l /etc/passwd
?练习3:统计当前已经安装的软件数量
#!/bin/bash
Rpm -qa | wc -l
Yum list installed | wc -l
?
1.5? ?shell编程几个常用命令
?
grep命令详解
?
过滤来自一个文件或标准输入匹配模式内容。
除了?grep 外,还有 egrep。egrep 是 grep 的扩展,相当于 grep -E。
Usage: grep [OPTION]... PATTERN [FILE]...
?
grep常用选项详解
?
? ? ? ? ? ? ? ? ?选项? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 描述? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
-E,--extended-regexp? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 模式是扩展正则表达式(ERE)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
?-i,--ignore-case 忽略大小写
?-n,--line-number 打印行号
?-o,--only-matching 只打印匹配的内容
?-c,--count 只打印每个文件匹配的行数
?-B,--before-context=NUM 打印匹配的前几行
?-A,--after-context=NUM 打印匹配的后几行
-C,--context=NUM 打印匹配的前后几行
--color[=WHEN],?匹配的字体颜色
-v,--invert-match ?打印不匹配的行
?
?grep案例演示
[[email?protected] ~]# cat test
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/VAR/adm:/sbin/nologin
lp:x:4:7:lp:/VAR/SPOOL/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/VAR/SPOOL/mail:/sbin/nologin
operator:x:11:0:operator:/ROOT:/sbin/nologin
1. -i,忽略大小写
[[email?protected] ~]# grep -i "s" test
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/VAR/adm:/sbin/nologin
lp:x:4:7:lp:/VAR/SPOOL/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/VAR/SPOOL/mail:/sbin/nologin
operator:x:11:0:operator:/ROOT:/sbin/nologin
2. -n搜索的字符并显示行号
[[email?protected] ~]# grep -n "ROOT" test
1:ROOT:x:0:0:ROOT:/ROOT:/bin/bash
10:operator:x:11:0:operator:/ROOT:/sbin/nologin
4. -c,打印出搜索的内容有几行
[[email?protected] ~]# grep -ic "root" test
?2
?3. -o,仅仅打印出搜索的字符
?[[email?protected] ~]# grep -i -o "root" test
ROOT
ROOT
ROOT
ROOT
9. -v,打印出搜索中无root
[[email?protected] ~]# grep -v -i "root" test
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/VAR/adm:/sbin/nologin
lp:x:4:7:lp:/VAR/SPOOL/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/VAR/SPOOL/mail:/sbin/nologin
5. -B,打印匹配的前几行
?[[email?protected] ~]# grep -B 2 "SP" test
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/VAR/adm:/sbin/nologin
lp:x:4:7:lp:/VAR/SPOOL/lpd:/sbin/nologin
--
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/VAR/SPOOL/mail:/sbin/nologin
6.-A,打印匹配的后几行
[[email?protected] ~]# grep -A 2 "SP" test
lp:x:4:7:lp:/VAR/SPOOL/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
--
mail:x:8:12:mail:/VAR/SPOOL/mail:/sbin/nologin
operator:x:11:0:operator:/ROOT:/sbin/nologin
[[email?protected] ~]#
7.-C,打印匹配的前后几行
?[[email?protected] ~]# grep -C 1 "SP" test
adm:x:3:4:adm:/VAR/adm:/sbin/nologin
lp:x:4:7:lp:/VAR/SPOOL/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
--
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/VAR/SPOOL/mail:/sbin/nologin
operator:x:11:0:operator:/ROOT:/sbin/nologin
8. --color,在centos7中已经默认为 grep --color,在centos6中需要加上--color才会显示颜色
[[email?protected] ~]# alias grep
alias grep=‘grep --color=auto‘
?
一、cut命令
?[[email?protected] ~]# echo $RANDOM | md5sum | cut -c 1-6
a5c3a8
语法
cut [-bn] [file]
cut [-c] [file]
cut [-df] [file]
使用说明:
cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。
如果不指定 File 参数,cut?命令将读取标准输入。必须指定?-b、-c?或?-f?标志之一。
?
常用参数:
? -c?:以字符为单位进行分割。
? -d?:自定义分隔符。
? -f?:与-d一起使用,指定显示哪个区域。
?
常用实例演示1: -c:以字符为单位进行分隔
[[email?protected] ~]# cut -c 1-2 test
RO
bi
da
ad
常用实例演示2:-d,-f:自定义分隔符并进行指定显示
1.-d为分隔符-f截取那段
[[email?protected] ~]# head -3 test | cut -d "/" -f 2
ROOT:
bin:
sbin:?
二、sort命令
?
Linux sort命令用于将文本文件内容加以排序。
sort可针对文本文件的内容,以行为单位来排序。
语法:
sort [-bcdfimMnr][-o<输出文件>][-t<分隔字符>][+<起始栏位>-<结束栏位>][--help][--verison][文件]
?
常用参数说明:
? -k:根据切割后的那一段进行排序
? -n 依照数值的大小排序(默认是根据字符进行排序)。
? -r 以相反的顺序来排序。
? -t<分隔字符> 指定排序时所用的栏位分隔字符。
? -u:去除重复的行(只要那个指定的字段重复,就认定是重复的行)
?
实例一:默认排序
在使用sort命令以默认的式对文件的行进行排序,使用的命令如下:
[[email?protected] ~]# sort shu
1
11
2
2
23
321
4
4
45
5
5
-n按从小到大排序 -r降序排序
[[email?protected] ~]# sort -n shu
1
2
2
4
4
5
5
11
23
45
321
?
sort 命令将以默认的方式将文本文件的第一列以ASCII 码的次序排列,并将结果输出到标准输出。
?
实例二:取出排名前三
[[email?protected] ~]# sort -r test | head -3
8 this is ken
6 this is ken
6 this is ken
?
实例三:对文件中的内容按照e分割第二部分进行排序
[[email?protected] ~]# sort -t ‘e‘ -k 2 test
6 this:is:ke1
5 this:is:ke2
6 this:is:ke3
2 this:is:ke4
1 this:is:ke6
8 this:is:ke7
[[email?protected] ~]# sort -t ‘e‘ -k 2 -r test
8 this:is:ke7
1 this:is:ke6
2 this:is:ke4
6 this:is:ke3
5 this:is:ke2
6 this:is:ke1
?
实例四:去除重复的行
[[email?protected] ~]# sort test
1 this:is:ke6
2 this:is:ke4
2 this:is:ke4
2 this:is:ke4
2 this:is:ke4
5 this:is:ke2
6 this:is:ke1
6 this:is:ke3
8 this:is:ke7
[[email?protected] ~]# sort -u test
1 this:is:ke6
2 this:is:ke4
5 this:is:ke2
6 this:is:ke1
6 this:is:ke3
8 this:is:ke7
?
?三、uniq命令
作用:
去除重复的行(相邻且相同,认定为重复)
选项:
-c:在行首用数字表示该行出现了多少次
-u:仅仅显示那些没有出现重复过的行
?
实例一:统计行数
[[email?protected] ~]# sort -r shu | uniq -c
4 5
5 4
4 3
5 2
3 1
[[email?protected] ~]# sort -r shu | uniq -c |sort -n
3 1
4 3
4 5
5 2
5 4
[[email?protected] ~]# sort -r shu | uniq -c |sort -nr
5 4
5 2
4 5
4 3
3 1
、
?
实例二:将文件中相同的行去重
?
[[email?protected] ~]# sort shu |uniq
1
2
3
4
5
?四、seq命令
作用:
生成一个数组序列
格式:
seq [start ?[step]] stop
?
实例:
#终止位5
[[email?protected] ~]# seq 2 5 #起始位2,终止位5
2
3
4
5
[[email?protected] ~]# seq 2 2 10 #起始位2,步长为2,终止位10
2
4
6
8
10
?
五、tr命令
?
作用:
Linux tr 命令用于转换或删除文件中的字符。
tr 指令从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。
?
a-z 任意小写
A-Z 任意大写
0-9 任意数字
?
实例一:替换大小写
[[email?protected] ~]# echo "this is ken" | tr a-z A-Z
THIS IS KEN
[[email?protected] ~]# echo "THIS IS KEN" | tr A-Z a-z
this is ken
?
实例二:删除特定字符串s这个字符串都会被删掉
[[email?protected] ~]# cat test | tr -d ‘s‘
6 thi:i:ke3
2 thi:i:ke4
2 thi:i:ke4
2 thi:i:ke4
2 thi:i:ke4
5 thi:i:ke2
6 thi:i:ke1
1 thi:i:ke6
8 thi:i:ke7
?
课后作业
?
作业1. 获取主机IP地址,获取结果仅显示IP,例如:172.20.10.2(使用尽可能多的方法)
?
作业2. 有如下一个文件,文件内容如下。
请把下方的内容复制到你的一个文件中,并完成如下需求
需求1. 统计出各个网址出现的次数
需求2. 按照出现次数排序(升序)
需求3. 取出出现次数排名前两名的网址
[[email?protected] ~]# cat ken.sh
http://www.baidu.com
http://www.baidu.com
http://www.sina.com
http://www.sina.com
http://www.sina.com
http://www.sina.com
http://www.sina.com
http://www.sina.com
http://www.sina.com
http://www.sina.com
http://www.sina.com
http://www.sina.com
http://www.sina.com
http://www.sina.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
http://www.qq.com
http://www.qq.com
http://www.qq.com
http://www.qq.com
http://www.qq.com
http://www.qq.com
http://www.qq.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
http://www.taobao.com
http://www.taobao.com
http://www.taobao.com
http://www.taobao.com
http://www.taobao.com
http://www.taobao.com
http://www.taobao.com
http://www.taobao.com
http://www.taobao.com
http://www.taobao.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com
?
课后作业答案
?
作业1:
第一种方法:
[[email?protected] ~]# ip a | grep global | cut -d " " -f 6 | cut -d "/" -f1
172.20.10.6
?
第二种方法:
[[email?protected] ~]# ip a | grep global | cut -d "b" -f 1 | tr -d [a-z] | cut -d "/" -f 1 | tr -d " "
172.20.10.6
?
第三种方法:
[[email?protected] ~]# ip a | grep global | tr -d [a-z] | tr -d " " | cut -d "/" -f1
172.20.10.6
方法有很多,大家尽可能的自己多思考哦!
?
作业2:
1.?统计出各个网址出现的次数
[[email?protected] ~]# cat ken.sh | cut -d ‘/‘ -f3 | sort | uniq -c
19 www.baidu.com
7 www.qq.com
12 www.sina.com
10 www.taobao.com
?
2. 按照出现次数排序(升序)
[[email?protected] ~]# cat ken.sh | cut -d ‘/‘ -f3 | sort | uniq -c | sort -n
7 www.qq.com
10 www.taobao.com
12 www.sina.com
19 www.baidu.com
?
3.??取出出现次数排名前两名的网址
[[email?protected] ~]# cat ken.sh | cut -d ‘/‘ -f3 | sort | uniq -c | sort -n -r | head -2
19 www.baidu.com
12 www.sina.com