操作系统实践
week1(2020.9.8)
课程介绍和linux的一些背景知识介绍
课程目的:
熟悉linux指令
熟悉shell编程
知道怎么使用linux操作系统
加深对操作系统的了解
(说实话我就是被这个课程目的吸引来的,一直很想上这门课,感觉对我帮助比较大。毕竟日后在实验室需用到linux、shell比较多,借这个机会学点皮毛也是好事情。)
推荐课程:
鸟哥的Linux私房菜
课程信息:
期末 40%(机试,根据给的要求完成指令)
平时 60% (考勤+作业)
Linux发展历史:
分时操作系统的“元始天尊”是来自大名鼎鼎的贝尔实验室的UNIX,后续各种都是由他衍生而来。后来由于他们要收费了,就某种程度上催生了新的操作系统的产生。如BSD家族里的SUN操作系统(现在不存在了,已经被甲骨文收购),包括著名的Mac操作系统也是从BSD里边慢慢演变出来的哦。
除了BSD家族外,其中值得一提的是Minux和GNU项目,GNU项目创始人本着免费的基本底线,开创了自由的软件和代码开源的新纪元。根据GPL(GNU项目的通用公共许可证),你可以随时随地的获取原码,可以对他进行复制、修改、重新分配等多重操作。
(整理他们的发展历史真的感觉像古代后宫一样啊,剪不断理还乱的。以上都是我个人理解,如有错误恳请指正!!)
Linux发行版本:
熟悉的有Ubuntu, Debian, Centos, Redhat等等。
Linux标准化:
POSIX:Portable Operation System Interface(可移植操作系统接口)
LSB: Linux Standard Base(Linux标准基础)
(说实话这里没听懂是啥意思……)
Linux认证:
红帽(RHCE):含金量高
LPI
※Linux软件架构:

安装Linux系统的步骤:
①选择合适的linux版本
②计划磁盘分区
③下载引导加载程序(Grub)
④选择安装包
week2(2020.9.15)
Unix & Shells(1)
1、修改密码:passwd
2、退出当前登录的账号:
exit或者ctrl+d
3、关机:poweroff或者halt,shutdown -h
4、重启:reboot或者shutdown -h
其中shutdown还可以设置多长时间以后重启,比如shutdown -r +30表示30秒后重启。
5、修改shell的启动文件:nano .bash.profile或者nano .bash.rc

week3(2020.9.22)
Unix & Shells(2)







week5(2020.10.6)
file system(2)



week6(2020.10.13)
file system(3)+文件处理

week7 (2020.10.20)
cut,paste,grep


week8 (2020.10.27)
sed,awk

week9 (2020.11.3)
sed awk作业讲解 + 重定向、管道

2020.11.3 开小灶:
作业:cmd1
databook的形式:
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 3755:11/12/56:20300
①Sort data in /tmp/databook using salary as the sort key in reverse order. Show your session.
解析:
从大到小的顺序排列:sort -nr(n是按数值从小到大排序,r是逆转)。用salary作为排序的标准:从databook的形式上看,该数据是以冒号作为每一个字段的分隔符,故需要指定分隔符为冒号:sort -t :。salary是第5个字段,所以需要指定按照第五个字段排序:sort -k 5。综上所述,结合起来的命令为:sort -nr -t : -k 5 /tmp/databook
②Display all the user name and user id in the system
解析:
这题要明白本系统存储的学生信息都在etc/passwd里面(背下来背下来!)passwd形式如下:zq:x:1540:1540::/home/zq:/bin/bash
只需要展示用户名和id,这里需要用到查看记录内的部分内容。思路有很多,第一个利用今天才学的管道。管道的方便之处在于把竖线前面的输出作为竖线后面的输入。观察这里的分隔符还是冒号,但是这里和sort的指定分隔符的字母不一样,这里是sort -d: 。sort -f 与sort -d一起使用,指定显示哪个区域。我们这里的姓名和学号分别是第一个、第三个字段,所以这里的命令为:cat /etc/passwd | cut -d: -f 1,3。
2020.11.4 开小灶:
今天来复习复习grep和正则表达式
数据:/tmp/databook
1.Print all lines containing the string San.
sed 'San' /tmp/databook
2.Print all lines where the person's first name starts with J.
sed '^J' /tmp/databook
解析:^表示以……开头的一行
3.Print lines where the last name begins with K or k.
解析:这一题的思路有两种,第一个是切分为段,然后指定第二段就是我们要找的lastname。但是这里不太合适,因为我们面临怎么切分以后还保留完整的一行这一问题。
于是转向另一个思路:描述前后的形式再匹配。我们发现lastname前面是一串字母(就是名字),中间隔一个空格。这种特征可以用正则表达式描述为‘[A-Za-z]+ [Kk]'。但是要注意一下+只能用于egrep里面,所以本题的命令为:egrep '[A-Za-z]+ [Kk]' /tmp/databook
4.Print all lines ending in 700.
解析:正则表达式中……\>表示以……结尾,同理\<……表示用……开头。故这题的命令为:grep '700\>' /tmp/databook
5. Print all lines that don't contain 834.
解析:本题需要用到grep-v取反。命令为grep '834' /tmp/databook
6.Print all lines where birthdays are in December.
解析:本题用到观察法,生日的月前面是冒号,后面是斜杠。故命令为grep ':12/' /tmp/databook
7.Print all lines where the phone number is in the 408 area code.
解析:同上题,408区号前面是冒号,后面是横杠。故命令为grep ':408-' /tmp/databook
8.Print all lines containing an uppercase letter, followed by four lowercase letters, a comma, a space, and one uppercase letter.
解析:本题需要用到重复几遍,XX\{n\}表示把XX重复n次。本题指令为grep '[A-Z][a-z]\{4\}, [A-Z]' /tmp/databook
9.Print lines preceded by a line number where the salary is a six-figure number.
解析:grep-n可以显示行号,这里有两种方式表示一行的结尾,可以用$,也可以用\>。就是注意不要自己乱加一些空格什么的,不然会匹配不到。本题命令为:grep -n '[0-9]\{6\}\>' /tmp/databook或者grep -n '[0-9]\{6\}$' /tmp/databook。
10.Print lines containing Lincoln or lincoln (remember that grep is insensitive to case).
解析:题目提示grep是大小写不敏感的,所以我们直接使用grep '[Ll]incoln' /tmp/databook是不能区分出Lincoln和lincoln的,所以这里需要用grep -e选项来把选项扩展为正则表达式,后面应该使用 | 来分割多个pattern,以此实现OR操作。故本题的命令为grep -e '[L|l]incoln' /tmp/databook。
2020.11.6 开小灶:
今天复习sed
1、Change Jon's name to Joanthan
解析:sed 's/X/Y/' 表示把X替换为Y,注意用三个正斜杠夹住他们。
sed 's/Jon/Joanthan/' /tmp/databook
2、Delete the first three lines
解析:sed 'n1,n2d' 表示删除n1到n2行。
sed '1,3d' /tmp/databook
3、Print lines 5 through 10
解析:sed 'n1,n2p' 表示打印n1到n2行。注意一定要加一个-n,不然会打印两遍。
sed -n '5,10p' /tmp/databook
4、Delete lines containing Lane
解析:sed '/X/d'表示删除包含X的行
sed '/Lane/ d' /tmp/databook
5、Print all lines where the birthdays are in November or December
解析:类似上一题,用sed '/X/p'表示打印包含X的行。但本题还有几个正则的考点,第一个是如何描述生日在11和12月?这个借助之前在grep里面用到的方法,只需要描述包含生日月份的那一块的特征,我们可以看到生日的月前面是冒号,后面是一个斜杠,而11和12月可以合并写成1[12],意思就是第一个数字是1,第二个数字从1和2中选择。还有一个需要注意的点是转义,我们需要的用来描述特征的斜杠和sed外面包裹他们的斜杠起冲突了,如果我不标识一下,会引起结构的混乱,所以在我们用来描述特征的斜杠前面需要加一个反斜杠。故本题的命令为:
sed -n '/:1[12]\//p' /tmp/databook
6、Replace the line containing Jose with JOSE HAS RETIRED.
解析:一大错误的方法就是sed 's/Jose/JOSE HAS RETIRED/' /tmp/databook,本题是要把包含Jose的整行都换掉,用上面的命令只替换了Jose,后面还留在那儿。正确的做法应该是在Jose前后分别加.*,意思就是这一行Jose前后的所有都包含进去了。
sed 's/.*Jose.*/JOSE HAS RETIRED/' /tmp/databook
7、Change Popeye's birthday to 11/14/46
解析:把本题拆解为几个部分,第一个是匹配Popeye,第二个是修改生日。匹配的命令为sed -n '/Popeye/p' /tmp/databook,但是这里我们还要结合修改生日,所以修改的就直接在Popeye后面加了。修改的结构还是s/X/Y/,这里的X是我们要描述的生日,Y是11/14/46,但是注意这里的斜杠都要转义,所以得在这些斜杠的前面加上反斜杠。X具体的描述方法为月份/日期/年,月份有一个数字,也有两个数字的,如何描述呢?可以使用重复的正则,即N{1,2}来表示数字N出现一次或两次。所以X的描述方法为[0-9]\{1,2\}\/[0-9][0-9],故本指令为:
sed '/Popeye/s/[0-9]\{1,2\}\/[0-9][0-9]/11\/14\/46/' /tmp/databook
8、Delete all blank lines
解析:本题难点在于如何描述空行。
sed '/^[\t]*$/d' /tmp/databook
2020.11.7 开小灶:
awk复习1
1.Print all the phone numbers
解析:awk -F:类似于cut,用-F表示指定分隔符为:,注意这里必须是大写的F才行。awk后面跟'{command}',中间直接填写需要用到的命令。这里需要打印第二个字段里面的内容,故命令为print $2。整个的命令为:
awk -F: '{print $2}' /tmp/donors
2.Print Dan's phone number
解析:这里类似于sed里面,需要查找某人的某一个值的时候,可以用/XX/表示这个人,后面再跟命令。
awk -F: '/Dan/{print $2}' /tmp/donors
3.Print Susan's name and phone number
awk -F: '/Susan/{print $1,$2}' /tmp/donors
4.Print all last names beginning with D
解析:这里使用'[ :]'指派空格和冒号都可以作为分隔符,~那个指的是后面描述的是第二个字段的特征,也就是第二个字段是以D开头的。
awk -F'[ :]' '$2~/^D/{print $2}' /tmp/donors
5.Print all first names beginning with either a C or E.
awk '$1~/^[CE]/{print $1}' /tmp/donors
6.Print all first names containing only four characters.
awk '$1~/^....$/{print $1}' /tmp/donors
7.Print the first names of all those in the 916 area code.
awk '$2~/916/{print $1}' /tmp/donors
2020.11.8 开小灶:
awk复习2 + 重定向和管道。
8.Print Main’s campaign contributions. Each value should be printed with a leading dollar sign; e.g., $250 $100 $175.
解析:本题的特点是要求你在输出的某个字段前面还要加上一个$符号,必须要用双引号包括起来放在要加的字段的前面,而且中间不要加逗号(加了会产生一个空格)。所以本题的命令为:
awk -F: '$1~/Main/{print "$"$3,"$"$4,"$"$5}' /tmp/donors
9.Print second name followed with a comma and first name
awk -F'[ :]' '{print $2","$1}' /tmp/donors
10.Print the first and last names of those who contributed more than $100 in the second month.
解析:这一题不同于前面的匹配,这里需要比较大小,所以用字段>n来表示比n大。
awk -F: '$4>100{print $1}' /tmp/donors
11.Print the names and phone numbers of those who contributed less than $85 in the last month.
awk -F: '$5<85{print $1,$2}' /tmp/donors
12.Print the names of those who contributed between $75 and $150 in the first month.
awk -F: '$3 >= 75 && $3 <= 150 {print $1}' /tmp/donors
13.Print the names of those who contributed less than $800 over the three-month period.
awk -F: '$3+$4+$5 < 800 {print $1}' /tmp/donors
14.Print the names and addresses of those with an average monthly contribution greater than $200.
awk -F: '($3+$4+$5)/3>200 {print $1,$2}' /tmp/donors
15.Print the first name of those not in the 916 area code.
awk '$2!~/916/{print $1}' /tmp/donors
16.Print each record preceded by the number of the record.
解析:这里的NR指的是每一行的行号,$0表示整行。
awk '{print NR,$0}' /tmp/donors
17.Print the name and total contribution of each person.
awk -F: '{print $1,$3+$4+$5}' /tmp/donors
18.Add $10 to Chet's second contribution.
awk -F: '$1~/Chet/' /tmp/donors | sed 's/95/105/'
19.Change Nancy McNeil's name to Louise McInnes.
sed 's/Nancy McNeil/Louise Mclnnes/' /tmp/donors
**********
重定向和管道:
前面的grep,sed,awk其实各有侧重,grep和awk更偏向于按照条件显示内容,而sed在修改这一方面更方便。但有的时候单凭某一种方法不能实现目的,现在学习的管道就是讲可以很好的结合这几个方法。
重定向更多用在把结果写到某个文件,或者以某些文件的内容为输入这样的任务中。具体而言,用command > output-file 来重写,用command >> output-file来附加。
重写又分为command 1> output-file和command 2> output-file,后面那个是专门用于错误信息的,比如/dev/null和error.log。
这里有一个比较实用的命令,可能会考:忽略错误信息,也就是把错误信息重定向到/dev/null里面,可以想象这个文件是一个垃圾桶,把错误信息扔进去就相当于忽略它了。该命令为:
指令 2>/dev/null
管道其实在前面一些题已经用到过了,他用竖线隔开,竖线前面的输出作为竖线后面的输入。
1、Locate the foobar file in your home directory and save its absolute pathname in the foobar.path file. Append error messages to /dev/null. Show your session.
解析:本题需要做到以下几点:①显示foobar文件的绝对路径,这个可以结合pwd命令和awk命令,pwd可以得知目前所在的工作目录的绝对路径名称。由于目前我们的foobar文件是放在工作目录下的,所以在后面再加上/foobar就是这个文件的绝对路径了。利用管道把pwd的结果作为awk的输入,然后使用awk在后面添加即可。②把结果放到foobar.path文件里面。这个可以使用重定向的“重写”③忽略错误信息,也就是把错误添加到/dev/null里面。综上所述,本题的指令为:
pwd|awk '{print $1 "/foobar"}' >> foobar.path > /dev/null
2、Combine data in the following files (in this order) and append it to the all.labs file: lab1, lab2, lab3, and lab4. Any errors should be redirected to the error.log file. Show your session.
解析:本题需要把数据合并然后添加到all.labs,这里考虑使用paste命令,最终命令为:
paste lab1 lab2 lab3 1> all.labs 2> error.log
3、Use a command line to display the records in the file student_record for the top five students in ascending (sorted) order, i.e., with the highest GPA student’s record displayed first. Show your session.
sort -nr -k 4 /tmp/student_record | head -n 5
4、How many directory are there in the your home directory. Show the command.
解析:wc命令用于统计文件中的字节数、字符数或列数。-c:统计文件的字符数。-w:统计文件中的词语的数量,即被空格以及换行等分隔的字符串。-l:统计文件的列数。这里我们的文件名都是用空格隔开的,所以这里使用wc -w命令。整个命令为:
ls | wc -w