网站制作代理加盟,网站出现乱码怎么办,wordpress后台主题无法管理,用WordPress建什么站好项目七 熟练使用Vim程序编辑器与shell
#职业能力目标和要求
1#xff0c;学会使用vim编辑器。
2#xff0c;了解shell的强大功能和shell的命令解释过程。
3#xff0c;学会使用重定向和管道的方法。
4#xff0c;掌握正则表达式的使用方法。7.1 熟悉使用vim编辑器
7.1.1 …项目七 熟练使用Vim程序编辑器与shell
#职业能力目标和要求
1学会使用vim编辑器。
2了解shell的强大功能和shell的命令解释过程。
3学会使用重定向和管道的方法。
4掌握正则表达式的使用方法。7.1 熟悉使用vim编辑器
7.1.1 启动与退出vim
1vim命令格式vim 创建/编辑文件
2vim编辑环境 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编辑模式 2命令模式 7.2 熟悉掌握shell环境变量
shell是用与操作写入内核之间的接口起着交互作用shell支持具有字符串值的变量。
1变量的定义和引用
变量赋值格式namestring
#name是变量名是赋值符号string是值变量名是以字母或下划线开头的字母数字和下划线字符序列组成的。 通过在变量名( name)前加 字符 ( 如 字符(如 字符(如name )引用变量的值引用的结果就是用字符串string代替 $name此过程也称为变量替换。 若包含空格制表符和换行符则string必须用单引号/双引号括起来双引号内运行变量替换而单引号内则不行。
2shell变量的作用域
shell变量分为局部变量和全局变量。
局部变量的作用范围仅限制在其命令行所在的shell或shell脚本文件中。全局变量的作用范围则包括本shell进程及其所有子进程。可以使用export内置命令将局部变量设置为全局变量。
#举例
#定义两个变量
[rootlocalhost ~]# var1Linux
[rootlocalhost ~]# var2unix
#用export命令改成全局变量
[rootlocalhost ~]# export var2
#显示引用的变量
[rootlocalhost ~]# echo $var1
Linux
[rootlocalhost ~]# echo $var2
unix
#显示当前shell的pid
[rootlocalhost ~]# echo $$
2972
#调用子shell
[rootlocalhost ~]# bash
#查看子shell的pid
[rootlocalhost ~]# echo $$
66239
#引用子shell的变量可以看出var1没有显示说明var1为局部变量
[rootlocalhost ~]# echo $var1[rootlocalhost ~]# echo $var2
unix
#退出子shell并重新引用变量
[rootlocalhost ~]# exit
exit
[rootlocalhost ~]# echo $$
2972
[rootlocalhost ~]# echo $var1
Linux
[rootlocalhost ~]# echo $var2
unix3环境变量 环境变量是指由shell定义和赋初值的shell变量。shell 用环境变量来确定查找路径、注册目录、终端类型、终端名称、用户名等。所有环境变量都是全局变量并可以由用户重新设置。 在bash中设置环境变量用set命令。格式set 环境变量变量的值。 不加参数的set命令可以显示当前所有环境变量的设置。 环境变量前加$符号表示引用 当修改环境变量时 #例如将一个路径/tmp加到PATH变量前
[rootlocalhost ~]# PATH/tmp:$PATHunset命令将环境变量重新设置为系统默认值。
4命令运行的判读依据||
cmdcmd不考虑命令相关性的连续命令执行$命令回传值与”“或“||” **注意**两个之间是没有空格的“1”则是按“Shift\” 组合键的结果。
#例题1如果不管/tmp/abc存在与否都要创建/tmp/abc/hehe文件怎么办呢?
[rootlocalhost ~]# ls /tmp/abc || mkdir /tmp/abc touch /tmp/abc/hehe#例题2以ls测试/tmp/bobbying是否存在:若存在则显示“exist; 若不存在则显示“not exist”
[rootlocalhost ~]# ls /tmp/bobbying echo exist || echo not exist5工作环境设置文件
环境变量文件有两种
系统中的用户环境变量设置文件 登录环境设置文件/etc/profile非登录环境设置文件/etc/profile 用户设置的环境变量设置文件 登录环境设置文件$HOME/.Bash_profile非登录环境设置文件$HOME/.bashrc
注只有在特定的情况下才读取profile文件确切地说是在用户登录的时候读取。当运行shell 脚本以后就无须再读profile文件。
用户可以修改自己的用户环境设置文件来覆盖系统环境设置文件中的全局设置。
7.3 熟悉掌握正则表达法
正则表示法就是处理字符串的方法它以“行”为单位来进行字符串的处理。
7.3.1 掌握grep的高级使用
格式: grep [-A] [-B] [–colorauto] ‘查找字符串’ filename
-A:后面可加数字为after的意思除了列出该行外后续的n行也列出来。-B:后面可加数字为befor的意思除了列出该行外前面的n行也列出来。–coloreauto.可将搏寻出的正确数掘用特殊颜色标记。
#例1用demsg列出核心信息再以grep找出内含IPv6的那行
[rootlocalhost ~]# 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)来表示
[rootlocalhost ~]# dmesg | grep -n --colorauto 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
[rootlocalhost ~]# dmesg | grep -n -A1 -B1 --colorauto 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 ready7.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 doesnt 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 cant 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! Lets go.
#I am Bo1查找特定字符串
#查找“the”字符串
[rootlocalhost ~]# grep -n the /root/sample.txt
8:I cant 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字符串
[rootlocalhost ~]# 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 doesnt 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! Lets go.
21:#I am Bo
[rootlocalhost ~]# grep -in the /root/sample.txt
8:I cant 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”
[rootlocalhost ~]# grep -n t[ae]st /root/sample.txt
8:I cant finish the test.^M
9:Oh! The soup taste good.^M
#[]里面无论几个字符都只代表一个字符。
#如何查找含“oo”的字符
[rootlocalhost ~]# 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]
#查询含数字的行
[rootlocalhost ~]# 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的意思。
[rootlocalhost ~]# grep -n [^[:lower:]]oo /root/sample.txt
3:Football game is not use feet only.3首行与行尾字节^$
#显示the只在行首
[rootlocalhost ~]# grep -n ^the /root/sample.txt
12:the symbol * is represented as start.#显示以小写开头
[rootlocalhost ~]# grep -n ^[a-z] /root/sample.txt
2:apple is my favorite food.
4:this dress doesnt 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! Lets go.#显示不要字母
[rootlocalhost ~]# grep -n ^[^a-zA-Z] /root/sample.txt
1:Open Source is a good mechanism to develop programs.
21:#I am Bo^符号在[]之内表示“反向选择”在[]之外表示定位在行首#找出行尾结束为小数点的行用跳转字节\
[rootlocalhost ~]# grep -n ^[^a-zA-Z] /root/sample.txt
1:Open Source is a good mechanism to develop programs.
21:#I am Bo
[rootlocalhost ~]# 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 doesnt 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! Lets go.#利用cat显示不打印字符行尾显示“$”
[rootlocalhost ~]# cat -An /root/sample.txt | head -n 10 |tail -n 65 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 cant finish the test.^M$9 Oh! The soup taste good.^M$10 motorcycle is cheap thancar.$#不要空行的打印-v ^$
[rootlocalhost ~]# grep -v ^$ /etc/rsyslog.conf |grep -v ^$4任意一个字符“.“与重复字节”*“
“*”代表0个或多个字符。“.”代表绝对有一个任意字符。
#找“gd”的字符串
rootlocalhost ~]# 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个或无数个
[rootlocalhost ~]# 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的字符串之间有无字节皆可
[rootlocalhost ~]# 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! Lets go.
#改结果有问题我们重新定义一下
[rootlocalhost ~]# 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! Lets go.#“.*”代表任意字符RE正则表达式#显示仅有数字用RE法
[rootlocalhost ~]# 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的字符串的行
[rootlocalhost ~]# 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的字符串
[rootlocalhost ~]# grep -n go\{2,5\}g /root/sample.txt
18:google is the best tools for search keyword.
#再假设查两个以上o的
[rootlocalhost ~]# 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重定向就是不用系统的标准而进行重新指定
2重定向分为输入重定向输出重定向和错误重定向 3在执行命令前命令解释程序会自动打开如果文件不存在则自动创建且清空该文件文中已存在的数据将被删除实际环境要慎重使用输入重定向。当执行完成时命令解释程序会正确地关闭该文件而命令在执行时并不知道它的输出已被重定向。
#1将ls命令生成的/tmp目录的一个清单存到当前目录中的dir文件中
[rootlocalhost ~]# ls -l /tmp/ dir
[rootlocalhost ~]# cat dir
total 0
drwx------. 2 lzl lzl 24 Jun 17 2022 ssh-J3eKGQfk53Ap
.....#2,将ls命令生成的/etc目录的一个清单就是显示出的内容以追加的方式存到当前目录中。
[rootlocalhost ~]# ls -l /etc/ dir
[rootlocalhost ~]# cat dir
total 0
drwx------. 2 lzl lzl 24 Jun 17 2022 ssh-J3eKGQfk53Ap
.....#3,将命令myprogram的错误信息保存在当前目录下的err file文件中
[rootlocalhost ~]# myprogram err_file#4,将命令myprogram的输出信息和错误信息保存在当前目录下的output file 文件中。
[rootlocalhost ~]# myprogram output_file#5,将命令ls的错误信息保存在当前目录下的err file文件中。
[rootlocalhost ~]# ls -l 2err_file
total 4669476
.....
#该命令并没有产生错误信息但err file文件中的原文件内容会被清空。#6Bash输人输出重定向可以通过使用下面选项设置为不覆盖已存在文件:
[rootlocalhost ~]# set -o noclobber
#这个选项仅用于对当前命令解释程序输人输出进行重定向而其他程序仍可能覆盖已存#7/dev/null 空设备相当于垃圾桶
#典型用法是丢弃从find或grep等命令送来的错误信息:
[rootlocalhost ~]# grep delegate /etc/* 2/dev/null
/etc/sudoers:## of related commands, which can then be delegated out to particular**4wc命令**用来计算数字可以计算文件的byte数字数或是列数若不指定文件名称或是所给予的文件名为“_”则wc指令会从标准输入设备读取数据。
[rootlocalhost ~]# wc/etc/passwd43 87 22567.4.2 使用管道
1定义将这些命令前后衔接在一起形成一个管道格式命令1|命令2|…|命令n
2每条命令都是单独的进程运行每条命令的输出作为下条命令的输入。
3由于命令的是从左到右顺序执行的所以管道线是单向的。
#1以长格式递归的方式分屏显示/etc目录下的文件和目录列表。
[rootlocalhost ~]# ls -Rl /etc | /more#2分屏显示文本文件/etc/passwd的内容。
[rootlocalhost ~]# cat /etc/passwd | more #3统计文本文件/et/passwd的行数、字数和字符数。期出
[rootlocalhost ~]# cat /etc/passwd | wc#4查看是否存在john用户账号。
[rootlocalhost ~]# cat /etc/passwd | grep john#5查看系统是否安装了ssh软件包。
[rootlocalhost ~]# rpm -qa |grep ssh#6显示文本文件中的若干行。
[rootlocalhost ~]# tail -15 myfile |head -34管道仅能操控命令的标准输出流。如果标准错误输出未重新定向那么任何写入其中的信息都会在终端显示屏幕上显示。
[rootlocalhost ~]# who | grep root | wc -l
**。shell
#1以长格式递归的方式分屏显示/etc目录下的文件和目录列表。
[rootlocalhost ~]# ls -Rl /etc | /more#2分屏显示文本文件/etc/passwd的内容。
[rootlocalhost ~]# cat /etc/passwd | more #3统计文本文件/et/passwd的行数、字数和字符数。期出
[rootlocalhost ~]# cat /etc/passwd | wc#4查看是否存在john用户账号。
[rootlocalhost ~]# cat /etc/passwd | grep john#5查看系统是否安装了ssh软件包。
[rootlocalhost ~]# rpm -qa |grep ssh#6显示文本文件中的若干行。
[rootlocalhost ~]# tail -15 myfile |head -34管道仅能操控命令的标准输出流。如果标准错误输出未重新定向那么任何写入其中的信息都会在终端显示屏幕上显示。
[rootlocalhost ~]# who | grep root | wc -l