操作系统实践

OS实践

一、期末划重点

第一章 历史

许可证:

image-20240623141922162

GPL:

access source code(访问源代码)

copy(复制)

modify(修改)

不可以redistribute(重新分配)

题目image-20240623145214155
Linux内核:

POSIX(Portable Operation System Interface)便携式操作系统接口:统一Linux和Unix的接口

SUS(Single UNIX Specification)单一UNIX规范

LSB(Linux Standard Base)Linux标准基础

题目

image-20240623144615809


第二章 Shell

/etc/passwd格式:

img

学号:x(密码):序号:用户组序号:主目录路径:Shell路径

/etc/passwd作用:提供系统用户的基本信息,如用户名、UID、GID等,供系统在用户登录和运行进程时进行验证和识别。

/etc/shadow作用:存储用户的加密密码和密码策略信息,通常只有系统管理员可读。它的作用是保护用户密码信息的安全性,确保只有授权的用户能够访问密码数据。

改密码:passwd

题目

image-20240623145152822

启动文件的作用

/etc/profile: 系统环境

$HOME/.bash_profile(.bash_login, .profile): 登陆时执行一次

$HOME/.bashrc:每当fork一个shell时执行一次

题目

image-20240623145026233

环境变量

env, set: 查看所有环境变量

$PATH: 指令搜索路径

$HOME: 用户登陆在的目录

$PS1: command prompt

题目

image-20240623145138625

别名:

创建:alias name=“string”

删除:unalias name
查看所有别名:alias

指令执行顺序:

  1. alias

  2. build-in command

  3. execute file

Shell元字符

img

image-20240623143247224

题目

image-20240623144918679

查看系统信息:

img

题目

image-20240623145044299

第四章 文件系统

磁盘分区

image-20240623144208946

题目image-20240623144518499

字母表示硬盘,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

题目

image-20240623144725079

文件类型
  • simple
    image-20240623150735450
  • directory
    image-20240623150754048
  • 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
文件结构

image-20240623150940261

文件系统相关命令
  • pwd 当前目录(print working directory)
  • parted 分区(disk partition)
  • mkfs 格式化(format the partition)
  • mount 分区挂载(mount the disk partition)
    mount [-t vfstype] [-o options] devicedir
题目

image-20240623145058113

文件相关命令
  • df 获取文件空间大小 (disk file)

  • du 搜索文件计算大小(disk usage)
    https://blog.csdn.net/Rio520/article/details/104370082

    df与du两者区别

    image-20240623145851180

  • mkdir 创建目录

  • rmdir 删除目录

  • ls 查看当前位置的文件:

    ​ -l:显示详细信息(可以看到文件大小)

    ​ -h:文件大小以k/g来显示

  • touch 创建文件

  • cat 查看文件

  • cp 复制文件

  • mv 移动/重命名文件

  • rm 删除文件

    ​ -r:递归删除

    ​ -f:强制删除

题目image-20240623144946961image-20240623145106844

-h:以人类可读的格式显示磁盘空间信息(human)

image-20240623145121153递归地复制dir内的文件,才能完成dir的复制

image-20240623145405991

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:

image-20240623152304707

掩码

umask:查看当前掩码

可执行文件或目录的权限是777-umask,其他文件是666

特殊权限

image-20240623152713516image-20240623152907159

image-20240623152858766

SGID上面的图错误,是 chmod 2755 filename 和 chmod g+s filename

image-20240623153025283

题目

image-20240623153149612

可以创建文件、可以删除自己的、可以创建子目录

不能删除别人的

第六章 文件处理(重点)(实操题,考法和作业小测类似)

(括号内的是巧记,实际操作时忘记了使用 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\},不要掉了\

image-20240623161736434image-20240623161741894

grep——筛选满足要求的行
  • -i 忽略大小写(ignore)
  • -n 显示行号(line)
  • -v 显示不包含匹配文本的行(reverse)
  • -c 显示行数(count)
  • -w 只显示全部符合的列(whole)
  • -l 只打印具有匹配行的文件名(line)

image-20240623162152586

作业 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)
      image-20240623163519359
    • d 删除行(delete)
      image-20240623163527905
    • s 替换字符串(search)
      image-20240623163537621
    • g 全局替换(global)
    • a 下面插入(append)
      image-20240623163724539
    • i 上面插入(insert)
      image-20240623163838202
    • [数字]! 取匹配行之外的所有行
      image-20240623163848208
作业 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 设置分隔符,不设置默认为空格

  • 模式

    image-20240623164309687

作业 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

第七章 管道和重定向

image-20240623165006083

image-20240623165305395

第八章 文件共享/硬链接和软链接

如何创建链接

-s(soft)软链接

image-20240623170309626image-20240623170315197

第九章 进程

查看进程的三个命令
  • 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:运行命令并忽略挂断信号

    • image-20240623172328166

image-20240623172422659

第十一章 脚本编程(两个脚本题,一个if,一个while或者for)

image-20240623172454663

image-20240623172507178

image-20240623172516471

image-20240623172531977

image-20240623172543487

image-20240623172552523

image-20240623172602742

image-20240623172609572

image-20240623172618803

image-20240623172629433

image-20240623172636720

image-20240623172720629
image-20240623172727039

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值