项目七 熟练使用Vim程序编辑器与shell
#职业能力目标和要求
1,学会使用vim编辑器。
2,了解shell的强大功能和shell的命令解释过程。
3,学会使用重定向和管道的方法。
4,掌握正则表达式的使用方法。
7.1 熟悉使用vim编辑器
7.1.1 启动与退出vim
3,编译时的命令
:w #保存
:w filename #另存为filename
:wq! #保存退出
:x #保存退出
:wq! #注:以filename为文件名保存后退出
:q! #不保存退出
7.1.2 熟悉掌握vim的工作模式.
3,命令模式
- (如果是插入模式,先按”Esc” 键进人编辑模式)在编辑模式下,按“:”键即可进入命令模式;此时左下角有”:“提示符,可以输入命令;执行完自动回到编辑模式。
- 若改变主意,按退格键全删即可回到编辑模式。
7.1.3 使用vim命令
1,编辑模式
2,命令模式
7.2 熟悉掌握shell环境变量
shell是用与操作写入内核之间的接口,起着交互作用;shell支持具有字符串值的变量。
变量赋值格式:name=string
#name是变量名,=是赋值符号,string是值
2,shell变量的作用域
#举例:
#定义两个变量
[root@localhost ~]# var1=Linux
[root@localhost ~]# var2=unix
#用export命令改成全局变量
[root@localhost ~]# export var2
#显示引用的变量
[root@localhost ~]# echo $var1
Linux
[root@localhost ~]# echo $var2
unix
#显示当前shell的pid
[root@localhost ~]# echo $$
2972
#调用子shell
[root@localhost ~]# bash
#查看子shell的pid
[root@localhost ~]# echo $$
66239
#引用子shell的变量,可以看出var1,没有显示,说明var1为局部变量
[root@localhost ~]# echo $var1
[root@localhost ~]# echo $var2
unix
#退出子shell,并重新引用变量
[root@localhost ~]# exit
exit
[root@localhost ~]# echo $$
2972
[root@localhost ~]# echo $var1
Linux
[root@localhost ~]# echo $var2
unix
3,环境变量
**注意:**两个&之间是没有空格的,“1”则是按“Shift+” 组合键的结果。
#例题1:如果不管/tmp/abc存在与否,都要创建/tmp/abc/hehe文件,怎么办呢?
[root@localhost ~]# ls /tmp/abc || mkdir /tmp/abc && touch /tmp/abc/hehe
#例题2:以ls测试/tmp/bobbying是否存在:若存在,则显示“exist"; 若不存在,则显示“not exist”
[root@localhost ~]# ls /tmp/bobbying && echo "exist" || echo "not exist"
5,工作环境设置文件
环境变量文件有两种
- 系统中的用户环境变量设置文件
- 用户设置的环境变量设置文件
注:只有在特定的情况下才读取profile文件,确切地说是在用户登录的时候读取。当运行shell 脚本以后,就无须再读profile文件。
7.3 熟悉掌握正则表达法
正则表示法就是处理字符串的方法,它以“行”为单位来进行字符串的处理。
7.3.1 掌握grep的高级使用
格式: grep [-A] [-B] [–color=auto] ‘查找字符串’ filename
- -A:后面可加数字,为after的意思,除了列出该行外,后续的n行也列出来。
- -B:后面可加数字,为befor的意思,除了列出该行外,前面的n行也列出来。
- –coloreauto.可将搏寻出的正确数掘用特殊颜色标记。
#例1:用demsg列出核心信息,再以grep找出内含IPv6的那行
[root@localhost ~]# dmesg | grep 'IPv6'
[ 6.769531] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[ 6.778049] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[ 6.778875] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[ 11.978750] IPv6: ADDRCONF(NETDEV_UP): virbr0: link is not ready
[ 87.408060] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[ 87.409345] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[ 117.501771] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[ 117.503142] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[ 128.247837] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[ 128.249073] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[51085.622677] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[51085.626092] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
#例2:承上賄要将本取到的关键字显色,日加上行号(-n)来表示
[root@localhost ~]# dmesg | grep -n --color=auto 'IPv6'
1831:[ 6.769531] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1833:[ 6.778049] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1834:[ 6.778875] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
1846:[ 11.978750] IPv6: ADDRCONF(NETDEV_UP): virbr0: link is not ready
1852:[ 87.408060] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1854:[ 87.409345] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
1855:[ 117.501771] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1857:[ 117.503142] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
1858:[ 128.247837] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1860:[ 128.249073] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
1863:[51085.622677] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1865:[51085.626092] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
#例3:
[root@localhost ~]# dmesg | grep -n -A1 -B1 --color=auto 'IPv6'
1830-[ 6.559585] NET: Registered protocol family 40
1831:[ 6.769531] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1832-[ 6.773124] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
1833:[ 6.778049] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1834:[ 6.778875] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
1835-[ 11.501269] ip6_tables: (C) 2000-2006 Netfilter Core Team
--
1845-[ 11.978664] virbr0: port 1(virbr0-nic) entered listening state
1846:[ 11.978750] IPv6: ADDRCONF(NETDEV_UP): virbr0: link is not ready
1847-[ 12.022448] virbr0: port 1(virbr0-nic) entered disabled state
--
1851-[ 33.946364] TCP: lp registered
1852:[ 87.408060] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1853-[ 87.408574] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
1854:[ 87.409345] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
1855:[ 117.501771] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1856-[ 117.502326] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
1857:[ 117.503142] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
1858:[ 128.247837] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1859-[ 128.248338] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
1860:[ 128.249073] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
1861-[22294.767165] e1000: ens33 NIC Link is Down
1862-[22304.806220] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
1863:[51085.622677] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
1864-[51085.625314] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
1865:[51085.626092] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
7.3.2 练习基础正则表达式
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.^M
GNU is free air not free beer.^M
Her hair is very beauty.^M
I can't finish the test.^M
Oh! The soup taste good.^M
motorcycle is cheap thancar.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.^M
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
#I am Bo
1,查找特定字符串
#查找“the”字符串
[root@localhost ~]# grep -n 'the' /root/sample.txt
8:I can't finish the test.^M
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
#反向选择(就是除去the字符串)
[root@localhost ~]# grep -vn 'the' /root/sample.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
5:However, this dress is about $ 3183 dollars.^M
6:GNU is free air not free beer.^M
7:Her hair is very beauty.^M
9:Oh! The soup taste good.^M
10:motorcycle is cheap thancar.
11:This window is clear.
13:Oh! My god!
14:The gd software is a library for drafting programs.^M
17:I like dog.
19:goooooogle yes!
20:go! go! Let's go.
21:#I am Bo
[root@localhost ~]# grep -in 'the' /root/sample.txt
8:I can't finish the test.^M
9:Oh! The soup taste good.^M
12:the symbol '*' is represented as start.
14:The gd software is a library for drafting programs.^M
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
#如何查找含“test”,“teste”?
[root@localhost ~]# grep -n 't[ae]st' /root/sample.txt
8:I can't finish the test.^M
9:Oh! The soup taste good.^M
#[]里面无论几个字符,都只代表一个字符。
#如何查找含“oo”的字符?
[root@localhost ~]# grep -n 'oo' /root/sample.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.^M
18:google is the best tools for search keyword.
19:goooooogle yes!
#例如大写英文/小写英文/数字等,可以用[a-z][A-Z][0-9]
#查询含数字的行
[root@localhost ~]# grep -n '[0-9]' /root/sample.txt
5:However, this dress is about $ 3183 dollars.^M
12:the symbol '*' is represented as start.
#[:lower:]表示a-z的意思。
[root@localhost ~]# grep -n '[^[:lower:]]oo' /root/sample.txt
3:Football game is not use feet only.
3,首行与行尾字节^$
#显示"the"只在行首
[root@localhost ~]# grep -n '^the' /root/sample.txt
12:the symbol '*' is represented as start.
#显示以小写开头
[root@localhost ~]# grep -n '^[a-z]' /root/sample.txt
2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap thancar.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
#显示不要字母
[root@localhost ~]# grep -n '^[^a-zA-Z]' /root/sample.txt
1:"Open Source" is a good mechanism to develop programs.
21:#I am Bo
^符号在[]之内表示“反向选择”,在[]之外表示定位在行首
#找出行尾结束为小数点的行(用跳转字节)
[root@localhost ~]# grep -n '^[^a-zA-Z]' /root/sample.txt
1:"Open Source" is a good mechanism to develop programs.
21:#I am Bo
[root@localhost ~]# grep -n '.$' /root/sample.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
10:motorcycle is cheap thancar.
11:This window is clear.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
20:go! go! Let's go.
#利用cat显示不打印字符,行尾显示“$”
[root@localhost ~]# cat -An /root/sample.txt | head -n 10 |tail -n 6
5 However, this dress is about $ 3183 dollars.^M$
6 GNU is free air not free beer.^M$
7 Her hair is very beauty.^M$
8 I can't finish the test.^M$
9 Oh! The soup taste good.^M$
10 motorcycle is cheap thancar.$
#不要空行的打印(-v '^$')
[root@localhost ~]# grep -v '^$' /etc/rsyslog.conf |grep -v '^$'
4,任意一个字符“.“与重复字节”*“
- “*”:代表0个或多个字符。
- “.”:代表绝对有一个任意字符。
#找“g??d”的字符串
root@localhost ~]# grep -n 'g..d' /root/sample.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.^M
16:The world <Happy> is the same with "glad".
#查找两个“oo”以上的字符串(两个oo以后可以是0个或无数个)
[root@localhost ~]# grep -n 'ooo*' /root/sample.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.^M
18:google is the best tools for search keyword.
19:goooooogle yes!
#开头查找开头结尾都是g的字符串,之间有无字节皆可
[root@localhost ~]# grep -n 'g*g' /root/sample.txt
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
9:Oh! The soup taste good.^M
13:Oh! My god!
14:The gd software is a library for drafting programs.^M
16:The world <Happy> is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
#改结果有问题,我们重新定义一下
[root@localhost ~]# grep -n 'g.*g' /root/sample.txt
1:"Open Source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.^M
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
#“.*”代表任意字符;RE:正则表达式
#显示仅有数字,用RE法
[root@localhost ~]# grep -n '[0-9][0-9]*' /root/sample.txt
5:However, this dress is about $ 3183 dollars.^M
15:You are the best is mean you are the no. 1.
5,限定连续RE字符范围
#查找含有两个o的字符串的行
[root@localhost ~]# grep -n 'o{2}' /root/sample.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.^M
18:google is the best tools for search keyword.
19:goooooogle yes!
#再假设要找后面接2~5个o,然后再接一个g的字符串
[root@localhost ~]# grep -n 'go{2,5}g' /root/sample.txt
18:google is the best tools for search keyword.
#再假设查两个以上o的
[root@localhost ~]# grep -n 'go{2,}g' /root/sample.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
7.3.3 基础正则表达式的特殊字符汇总
7.4 掌握输入输出重定向与管道命令的应用
7.4.1 使用重定向
1,重定向就是不用系统的标准,而进行重新指定
3,在执行命令前,命令解释程序会自动打开(如果文件不存在,则自动创建)且清空该文件(文中已存在的数据将被删除)实际环境要慎重使用输入重定向。当执行完成时,命令解释程序会正确地关闭该文件,而命令在执行时并不知道它的输出已被重定向。
#1,将ls命令生成的/tmp目录的一个清单存到当前目录中的dir文件中
[root@localhost ~]# ls -l /tmp/ >dir
[root@localhost ~]# cat dir
total 0
drwx------. 2 lzl lzl 24 Jun 17 2022 ssh-J3eKGQfk53Ap
.....
#2,将ls命令生成的/etc目录的一个清单(就是显示出的内容)以追加的方式存到当前目录中。
[root@localhost ~]# ls -l /etc/ >>dir
[root@localhost ~]# cat dir
total 0
drwx------. 2 lzl lzl 24 Jun 17 2022 ssh-J3eKGQfk53Ap
.....
#3,将命令myprogram的错误信息保存在当前目录下的err file文件中
[root@localhost ~]# myprogram &>err_file
#4,将命令myprogram的输出信息和错误信息保存在当前目录下的output file 文件中。
[root@localhost ~]# myprogram &>output_file
#5,将命令ls的错误信息保存在当前目录下的err file文件中。
[root@localhost ~]# ls -l 2>err_file
total 4669476
.....
#该命令并没有产生错误信息,但err file文件中的原文件内容会被清空。
#6,Bash输人输出重定向可以通过使用下面选项设置为不覆盖已存在文件:
[root@localhost ~]# set -o noclobber
#这个选项仅用于对当前命令解释程序输人输出进行重定向,而其他程序仍可能覆盖已存
#7,/dev/null 空设备(相当于垃圾桶)
#典型用法是丢弃从find或grep等命令送来的错误信息:
[root@localhost ~]# grep delegate /etc/* 2>/dev/null
/etc/sudoers:## of related commands, which can then be delegated out to particular
**4,wc命令:**用来计算数字,可以计算文件的byte数,字数或是列数,若不指定文件名称,或是所给予的文件名为“_”,则wc指令会从标准输入设备读取数据。
[root@localhost ~]# wc</etc/passwd
43 87 2256
7.4.2 使用管道
1,定义:将这些命令前后衔接在一起,形成一个管道,格式命令1|命令2|…|命令n
2,每条命令都是单独的进程运行,每条命令的输出作为下条命令的输入。
#1,以长格式递归的方式分屏显示/etc目录下的文件和目录列表。
[root@localhost ~]# ls -Rl /etc | /more
#2,分屏显示文本文件/etc/passwd的内容。
[root@localhost ~]# cat /etc/passwd | more
#3,统计文本文件/et/passwd的行数、字数和字符数。期出
[root@localhost ~]# cat /etc/passwd | wc
#4,查看是否存在john用户账号。
[root@localhost ~]# cat /etc/passwd | grep john
#5,查看系统是否安装了ssh软件包。
[root@localhost ~]# rpm -qa |grep ssh
#6,显示文本文件中的若干行。
[root@localhost ~]# tail -15 myfile |head -3
4,管道仅能操控命令的标准输出流。如果标准错误输出未重新定向,那么任何写入其中的信息都会在终端显示屏幕上显示。
[root@localhost ~]# who | grep root | wc -l
**。
```shell
#1,以长格式递归的方式分屏显示/etc目录下的文件和目录列表。
[root@localhost ~]# ls -Rl /etc | /more
#2,分屏显示文本文件/etc/passwd的内容。
[root@localhost ~]# cat /etc/passwd | more
#3,统计文本文件/et/passwd的行数、字数和字符数。期出
[root@localhost ~]# cat /etc/passwd | wc
#4,查看是否存在john用户账号。
[root@localhost ~]# cat /etc/passwd | grep john
#5,查看系统是否安装了ssh软件包。
[root@localhost ~]# rpm -qa |grep ssh
#6,显示文本文件中的若干行。
[root@localhost ~]# tail -15 myfile |head -3
4,管道仅能操控命令的标准输出流。如果标准错误输出未重新定向,那么任何写入其中的信息都会在终端显示屏幕上显示。
[root@localhost ~]# who | grep root | wc -l
原文地址:https://blog.csdn.net/weixin_57789471/article/details/134630934
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_3992.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!