项目七 熟练使用Vim程序编辑器与shell

项目七 熟练使用Vim程序编辑器与shell

#职业能力目标和要求
1,学会使用vim编辑器。
2,了解shell的强大功能和shell的命令解释过程。
3,学会使用重定向和管道的方法。
4,掌握正则表达式的使用方法。

7.1 熟悉使用vim编辑器

7.1.1 启动与退出vim

1,vim命令格式:vim +创建/编辑文件

2,vim编辑环境:

3,编译时的命令

:w			#保存
:w filename	#另存为filename
:wq!		#保存退出
:x			#保存退出
:wq!		#注:以filename为文件名保存后退出
:q!			#不保存退出

7.1.2 熟悉掌握vim的工作模式.

1,编辑模式

  • 首先进来的就是该模式。
  • 在编辑行是_表示;不在编辑行~表示。
  • 如果左下角是[New File],则表示该文件是新建文件。
  • 该模式下左下角什么都没有。

2,插入模式

  • 进入编辑模式的命令:插入命令i,附加命令a,打开命令o,修改命令c,取代命令r/替换命令s都可以进入该模式
  • 按“esc”键就可以回到编辑模式。
  • 该模式下左下角有-- INSERT –

3,命令模式

  • (如果是插入模式,先按"Esc” 键进人编辑模式)在编辑模式下,按“:”键即可进入命令模式;此时左下角有”:“提示符,可以输入命令;执行完自动回到编辑模式。
  • 若改变主意,按退格键全删即可回到编辑模式。

7.1.3 使用vim命令

1,编辑模式

image.png

image.png

image.png

image.png

2,命令模式

image.png

7.2 熟悉掌握shell环境变量

shell是用与操作写入内核之间的接口,起着交互作用;shell支持具有字符串值的变量。

1,变量的定义和引用

变量赋值格式:name=string
#name是变量名,=是赋值符号,string是值
  • 变量名是以字母或下划线开头的;字母,数字和下划线字符序列组成的。

  • 通过在变量名( name)前加 字符 ( 如 字符(如 字符(name )引用变量的值,引用的结果就是用字符串string代替

    $name,此过程也称为变量替换。

  • 若包含空格,制表符和换行符,则string必须用单引号/双引号括起来;双引号内运行变量替换,而单引号内则不行。

2,shell变量的作用域

shell变量分为局部变量和全局变量。

  • 局部变量的作用范围仅限制在其命令行所在的shell或shell脚本文件中。
  • 全局变量的作用范围则包括本shell进程及其所有子进程。
  • 可以使用export内置命令将局部变量设置为全局变量。
#举例:
#定义两个变量
[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,环境变量

  • 环境变量是指由shell定义和赋初值的shell变量。shell 用环境变量来确定查找路径、注册目录、终端类型、终端名称、用户名等。所有环境变量都是全局变量,并可以由用户重新设置

  • 在bash中,设置环境变量用set命令。格式:set 环境变量=变量的值

image.png

  • 不加参数的set命令,可以显示当前所有环境变量的设置

  • 环境变量前加$符号,表示引用

  • 当修改环境变量时

    #例如:将一个路径/tmp加到PATH变量前
    [root@localhost ~]# PATH=/tmp:$PATH
    
  • unset命令:将环境变量重新设置为系统默认值。

4,命令运行的判读依据:;,&&,||

  • cmd;cmd(不考虑命令相关性的连续命令执行)
  • $?(命令回传值)与”&&“或“||”

image.png

**注意:**两个&之间是没有空格的,“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,工作环境设置文件

环境变量文件有两种

  • 系统中的用户环境变量设置文件
    • 登录环境设置文件:/etc/profile
    • 非登录环境设置文件:/etc/profile
  • 用户设置的环境变量设置文件
    • 登录环境设置文件:$HOME/.Bash_profile
    • 非登录环境设置文件:$HOME/.bashrc

注:只有在特定的情况下才读取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 练习基础正则表达式

在root下编写sample.txt测试文件

"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.

2,利用中括号[]]来搜寻集合字符。

#如何查找含“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 基础正则表达式的特殊字符汇总

image.png

7.4 掌握输入输出重定向与管道命令的应用

7.4.1 使用重定向

1,重定向就是不用系统的标准,而进行重新指定

2,重定向分为输入重定向,输出重定向和错误重定向

image.png

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,每条命令都是单独的进程运行,每条命令的输出作为下条命令的输入

3,由于命令的是从左到右顺序执行的,所以管道线是单向的

#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
  • 41
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值