【专业课】操作系统实践(Score:90)
OS实践
一、期末划重点
第一章 历史
许可证:
GPL:
access source code(访问源代码)
copy(复制)
modify(修改)
不可以redistribute(重新分配)
题目
Linux内核:
POSIX(Portable Operation System Interface)便携式操作系统接口:统一Linux和Unix的接口
SUS(Single UNIX Specification)单一UNIX规范
LSB(Linux Standard Base)Linux标准基础
第二章 Shell
/etc/passwd格式:
学号:x(密码):序号:用户组序号:主目录路径:Shell路径
/etc/passwd作用:提供系统用户的基本信息,如用户名、UID、GID等,供系统在用户登录和运行进程时进行验证和识别。
/etc/shadow作用:存储用户的加密密码和密码策略信息,通常只有系统管理员可读。它的作用是保护用户密码信息的安全性,确保只有授权的用户能够访问密码数据。
改密码:passwd
题目
启动文件的作用
/etc/profile: 系统环境
$HOME/.bash_profile(.bash_login, .profile): 登陆时执行一次
$HOME/.bashrc:每当fork一个shell时执行一次
题目
环境变量
env, set: 查看所有环境变量
$PATH: 指令搜索路径
$HOME: 用户登陆在的目录
$PS1: command prompt
题目
别名:
创建:alias name="string"
删除:unalias name
查看所有别名:alias
指令执行顺序:
- alias
- build-in command
- execute file
Shell元字符
题目
查看系统信息:
题目
第四章 文件系统
磁盘分区
题目
字母表示硬盘,a第一个,b第二个
在拓展分区中,逻辑分区的设备名从5开始,前面的1到4用于主分区
挂载
mount 分区挂载(mount the disk partition)
- mount [-t vfstype] [-o options] devicedir
- vfstype :vfat(window fat32 format)、ext3、ext4、iso9660(cdrom format) device:
- hda, hdb:IDE device
- sda, sdb:scsi, sata, usb device
auto mount the partition: /etc/fstab
题目
文件类型
- simple
- directory
- link
A Link File is created by the system when a symbolic link in created to an existing file.
当创建指向现有文件的符号链接时,系统会创建链接文件。 - special
A special File is a means of accessing hardware devices, including the keyboard, hard disk, CD-ROM drive, tape drive and printer.
特殊文件是一种访问硬件设备的方法,包括键盘、硬盘、CD-ROM驱动器、磁带驱动器和打印机。- block e.g., keyboard
- character e.g., a disk
- FIFO
Tools that enable processes to communicate with each other
使进程能够相互通信的工具 - socket
文件结构
文件系统相关命令
- pwd 当前目录(print working directory)
- parted 分区(disk partition)
- mkfs 格式化(format the partition)
- mount 分区挂载(mount the disk partition)
mount [-t vfstype] [-o options] devicedir
题目
文件相关命令
- df 获取文件空间大小 (disk file)
- du 搜索文件计算大小(disk usage)
https://blog.csdn.net/Rio520/article/details/104370082df与du两者区别 - mkdir 创建目录
- rmdir 删除目录
- ls 查看当前位置的文件: -l:显示详细信息(可以看到文件大小)
-h:文件大小以k/g来显示
- touch 创建文件
- cat 查看文件
- cp 复制文件
- mv 移动/重命名文件
- rm 删除文件 -r:递归删除
-f:强制删除
题目

-h:以人类可读的格式显示磁盘空间信息(human)
递归地复制dir内的文件,才能完成dir的复制
du、df命令都是显示文件系统的磁盘使用情况,但是两者的计算方法不同。Du是一个一个算出来然后累加,而df是通过文件系统获得磁盘使用情况。本题要求显示目录/var的文件系统的使用情况,所以用df。
第五章 文件安全/权限管理
查看文件权限
ls -ld
-l
选项表示以长格式显示文件信息,-d
选项表示显示目录本身的详细信息,而不是显示目录中的内容。
ugo:owner、group,other
rwx:read、write、execute
修改文件权限
chown/chgrp:改变文件所有者
chmod [options] symbolic-mode file-list
options:
掩码
umask:查看当前掩码
可执行文件或目录的权限是777-umask,其他文件是666
特殊权限
SGID上面的图错误,是 chmod 2755 filename 和 chmod g+s filename
题目
可以创建文件、可以删除自己的、可以创建子目录
不能删除别人的
第六章 文件处理(重点)(实操题,考法和作业小测类似)
(括号内的是巧记,实际操作时忘记了使用 man 指令
来看)
查看完整文件
- cat:查看文件所有内容
- -n 显示行号(line)
- -b 显示行号,不算空行(no break)
- nl:查看文件所有内容且标上行号,除了空行
- more:分页查看
- od:以n进制的方式查看文件所有内容
- -c ascii(ascii)
- -h 16进制(hec)
查看文件头尾
- head:从第一行开始,默认显示十行
- -n 显示行号(line)
- -数字 查看前[数字]行
- tail:从最后一行开始,默认显示十行
- -n 显示行号(line)
- -数字 查看前[数字]行
- -+数字 查看从第[数字]行到末尾(+数字,表示正数到第[数字]行)
- -f 实时查看文件新增内容(file)
确认文件大小
- wc:展示文件总行数 单词数 字符数
- -l 只看行数(line)
- -w 只看单词数(word)
- -c 只看字符数(character)
比较文件
- diff:比较两个文件
- 用法:
diff file1.txt file2.txt
- -d 显示删除历史(delete)
- -c 显示修改历史(context)
- -a 显示增加历史(add)
- 用法:
- uniq [options] [input-file] [ouput-file]:去除重复的行
查找文件/命令
- find:查找
- find [目录] [命令]
- -name 以名字查找
- -size 以大小查找
- -exec 对查询的结果执行命令
- -ok CMD对查询的结果执行命令,但会提示是否执行
- which:用于查找特定命令的可执行文件的路径。它会显示系统中找到的第一个匹配的命令的完整路径。
- which passwd:查询passwd文件路径
- whereis:用于查找特定命令的可执行文件、源代码文件和帮助文档的位置。它会搜索特定命令的二进制文件、源代码文件和 man 页面文件。
- whereis ls
whereis
提供了更全面的搜索范围,而which
则专注于查找可执行文件的路径。
排序
- sort
- -t[符号] 设置分隔符为[符号],默认为空格(split)
- 指定多个分隔符?结合管道和awk
awk -F'[:;]' '{print $0}' filename | sort -f - -k[数字] 指定第[数字]个字段为key(key)
- -r 逆序(reverse)
- -f 忽略大小写(大F和小f很像,所以-f是忽略)
- -b 忽略前导空格(blanks)
- -n 数值比较(number)
提取和黏贴
- cut:提取
cut -f2 file.txt
提取第二字段- -d 设置分隔符(delimeter)
- -c 按字符分隔(character)
- paste:黏贴
paste file1.txt file2.txt
把2的每一行对应粘贴到1的每一行
三剑客
正则表达式
贪婪模式:.*
非贪婪模式:.*?
注意匹配a~b次相同字符时,使用{a,b}
,不要掉了
grep——筛选满足要求的行
- -i 忽略大小写(ignore)
- -n 显示行号(line)
- -v 显示不包含匹配文本的行(reverse)
- -c 显示行数(count)
- -w 只显示全部符合的列(whole)
- -l 只打印具有匹配行的文件名(line)
作业 grep.txt
数据格式:Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 4086:7/25/53:85100
1. Print all lines containing the string San.
grep 'San' /tmp/databook
2. Print all lines where the person's first name starts with J.
grep '^J' /tmp/databook
3. Print all lines ending in 700.
grep '700$' /tmp/databook
4. Print all lines that don't contain 834.
grep -v '834' /tmp/databook
5. Print all lines where birthdays are in December.
grep '12/[0-9]{1,2}/[0-9]{1,2}' /tmp/databook
6. Print all lines where the phone number is in the 408 area code.
grep '408-[0-9]{3}-[0-9]{4}' /tmp/databook
7. Print all lines containing an uppercase letter, followed by four lowercase letters, a comma, a space, and one uppercase letter.
grep '[A-Z]{1}[a-z]{4}, [A-Z]{1}' /tmp/databook
8. Print lines where the last name begins with K or k.
egrep ' <K[a-z]*:| <k:[a-z]*:' /tmp/databook
9. Print lines preceded by a line number where the salary is a six-figure number.
grep -n ':[0-9]{6}' /tmp/databook
10. Print lines containing Lincoln or lincoln (remember that grep is insensitive to case).,
egrep -w 'Lincoln|lincoln' /tmp/databook
sed——文本修改
- 后缀
- p 打印内容(print)
- d 删除行(delete)
- s 替换字符串(search)
- g 全局替换(global)
- a 下面插入(append)
- i 上面插入(insert)
- [数字]! 取匹配行之外的所有行
- p 打印内容(print)
作业 sed.txt
数据格式:Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 3745:10/1/35:58900
1. Change Jon's name to Joanthan
sed 's/Jon/Joanthan/' /tmp/databook
2. Delete the first three lines
sed '1,3d' /tmp/databook
3. Print lines 5 through 10
sed -n '5,10p' /tmp/databook
4. Delete lines containing Lane
sed '/Lane/d' /tmp/databook
5. Print all lines where the birthdays are in November or December
sed -n '/:1[1,2]/[0-9]{1,2}/[0-9]{1,2}/p' /tmp/databook
6. Replace the line containing Jose with JOSE HAS RETIRED.
sed 's/^.*Jose.*$/JOSE HAS RETIRED/' /tmp/databook
7. Change Popeye's birthday to 11/14/46
sed '/^Popeye.*/s/[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2}/11/14/46/' /tmp/databook
8. Delete all blank lines
sed '/^$/d' /tmp/databook
awk——筛选满足要求的行或列,主要作用是print
- -F 设置分隔符,不设置默认为空格
- 模式
作业 awk.txt
数据格式:Mike Harrington:(510) 548-1278:250:100:175
With awk(/tmp/donors): contains the names, phone numbers, and money contributions to the party campaign for the past three months
1.Print all the phone numbers
awk -F: '{print $2}' /tmp/donors
2.Print Dan's phone number
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
awk -F'[: ]' '$2~/D/{print $2}' /tmp/donors
5.Print all first names beginning with either a C or E.
awk -F'[: ]' '$1~/C|E/{print $1}' /tmp/donors
6.Print all first names containing only four characters.
awk -F'[: ]' '$1~/^[A-Za-z]{4}$/{print $1}' /tmp/donors
7.Print the first names of all those in the 916 area code.
awk -F'[: ]' '$3~/916/{print $1}' /tmp/donors
8.Print Main’s campaign contributions. Each value should be printed with a leading dollar sign; e.g., $250 $100 $175.
awk -F'[: ]' '$2~/Main/{print "$"$5,"$"$6,"$"$7}' /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.
awk -F'[: ]' '$6>100{print $1" "$2}' /tmp/donors
11.Print the names and phone numbers of those who contributed less than $85 in the last month.
awk -F'[: ]' '$7<85{print $1" "$2" "$3" "$4}' /tmp/donors
12.Print the names of those who contributed between $75 and $150 in the first month.
awk -F'[: ]' '$5>=75&&$5<=150{print $1" "$2}' /tmp/donors
13.Print the names of those who contributed less than $800 over the three-month period.
awk -F'[: ]' '($5+$6+$7)<800{print $1" "$2}' /tmp/donors
14.Print the names and addresses of those with an average monthly contribution greater than $200.
awk -F'[: ]' '($5+$6+$7)/3>200{print $1" "$2" "$3}' /tmp/donors
15.Print the first name of those not in the 916 area code.
awk -F'[: ]' '$3!~/916/{print $1}' /tmp/donors
16.Print each record preceded by the number of the record.
awk '{print NR " " $0}' /tmp/donors
17.Print the name and total contribution of each person.
awk -F'[ :]' '{print $1" "$5+$6+$7}' /tmp/donors
18.Add $10 to Chet's second contribution.
awk -F'[ :]' '$1~/Chet/{$6+=10;print $0}' /tmp/donors
19.Change Nancy McNeil's name to Louise McInnes.
awk -F'[ :]' '$1~/Nancy/{$1="Louise";$2="McInners";print $0}' /tmp/donors
真题
1. Sort the person according to their second month donation.
sort -nk 4 donations.txt
2. Print all the lines containing "Main"
grep “Main” donations.txt
3. Print all the lines starting with "D"
grep “^D” donations.txt
4. Print all the names second month donation is less than 300
awk ‘$4 < 300’ donations.txt
5.Print all the names whose first month donation and second month donation are both less than 100
awk ‘$3 < 100 && $4 < 100’ donations.txt
6. Delete all the blank line.
sed ‘/^$/d’ donations.txt
7. Replace all the string "(306)" to "(506)".
sed ‘s/(306)/(506)/g’
8. Delete all the repeated lines.
awk ‘!seen[$0]++’ donations.txt
9. Print how many person whose fist month donation is greater than 200
awk ‘$3 > 200’ donations.txt | wc -l
10.Save all the lines where the person's last name starts with "D" to file named "result"
grep " <D" donations.txt > result
第七章 管道和重定向
第八章 文件共享/硬链接和软链接
如何创建链接
-s(soft)软链接
第九章 进程
查看进程的三个命令
- ps:报告流程状态
- -a 显示执行的进程信息,会话头除外
- -e 显示运行的所有进程信息
- -l 显示进程状态的长报告
- -u [用户] 显示[用户]的进程
- pstree:进程树
- top:实时看进程信息
如何控制进程
- 前台
- ctrl z:暂停
- ctrl c:终止
- 后台
- jobs 展示工作状态
- jobs -l /tmp/loop ——看文件里前台进程状态
- jobs -l /tmp/loop & ——看文件里后台进程状态
- 挂起
- 进程切换
- 切至后台: bg [%任务号]
- 切至前台: fg [%任务号]
- 例: fg %7 将7号任务切到前台
- kill:终止进程
- 常用信号值:
- 1 挂起
- 2 中断
- 3 退出
- 9 确认杀死(无条件终止)
- 15 软件信号(默认信号值)
- kill -l 返回所有信号和其名字的一个列表
- kill本质
kill
命令用于终止正在运行的进程。其本质是向指定的进程发送信号,告诉该进程终止运行。
kill
命令可以发送不同的信号,其中最常用的信号是SIGTERM
(终止信号),它会请求进程正常终止。如果进程没有响应
SIGTERM
信号,可以使用SIGKILL
信号强制终止进程。 - nohup:运行命令并忽略挂断信号
第十一章 脚本编程(两个脚本题,一个if,一个while或者for)
版权声明:
作者:Zhang, Hongxing
链接:http://zhx.info/archives/498
来源:张鸿兴的学习历程
文章版权归作者所有,未经允许请勿转载。