LOADING

加载过慢请开启缓存 浏览器默认开启

OS Lab0实验报告

Thinking 0.1

  • 执行git status > Untracked.txt,查询当前工作区的状态。此时工作区的状态是修改了README.txt文件但未添加。>表示输出重定向,将当前工作区的状态输出到目标文件Untracked.txt中。

  • add命令表示将工作区的修改添加到暂存区,此时工作区的状态是修改了README.txt文件且已添加到暂存区。>表示输出重定向,将当前工作区的状态输出到目标文件Stage.txt中。

  • 提交命令为git commit -m "23373517"

  • cat Untracked.txt,第二行显示Untracked files:,说明在README.txt新建的时候,其处于为未跟踪状态 (untracked)。cat Stage.txt,第二行显示Changes to be committed:,说明文件处于暂存区,等待提交。

  • 修改README.txt 并将工作区状态保存到Modified.txt后,输出的状态为Changes not staged for commit:,与第一次add前的状态不同,说明在修改README.txt文件后其处于被修改状态(modified)

Thinking 0.2

  • add the filegit add
  • stage the filegit add
  • commitgit commit

Thinking 0.3

  • git checkout --print.c
  • git checkout --print.c
  • git rm --cached print.c

Thinking 0.4

  • 第一次git log

    image-20250314224346696

  • 第二次git log

    image-20250314224443674

  • 第三次git log

    image-20250314224209354

  • 第四次git logimage-20250314224251666

Thinking 0.5

  • echo first

    image-20250314225038470

  • echo second > output.txt

    image-20250314225120755

  • echo third > output.txt

    image-20250314225200504

  • echo forth >> output.txt

    image-20250314225246436

Thinking 0.6

  • command:

    echo命令后加单引号会将内容视为字面量字符串,不做任何扩展或替换,保留所有特殊字符和空格。

    #!/bin/bash                                                  
    touch test
    echo 'echo Shell Start...' > test
    echo 'echo set a = 1' >> test
    echo 'a=1' >> test
    echo 'echo set b = 2' >> test
    echo 'b=2' >> test
    echo 'echo set c = a+b' >> test
    echo 'c=$[$a+$b]' >> test
    echo 'echo c = $c' >> test
    echo 'echo save c to ./file1' >> test
    echo 'echo $c>file1' >> test
    echo 'echo save b to ./file2' >> test
    echo 'echo $b>file2' >> test
    echo 'echo save a to ./file3' >> test
    echo 'echo $a>file3' >> test
    echo 'echo save file1 file2 file3 to file4' >> test
    echo 'cat file1>file4' >> test
    echo 'cat file2>>file4' >> test
    echo 'cat file3>>file4' >> test
    echo 'echo save file4 to ./result' >> test
    echo 'cat file4>>result' >> test
    
  • result:

    3
    2
    1
    
  • echo echo Shell Start直接把“echo Shell Start”作为字符串输出

    如果在后面的内容加上反顿号,是将内部echo命令的输出作为外层echo的输出,会输出”Shell Start”

  • echo echo $c>file1 直接把“echo $c>file1”作为字符串输出

    如果在后面的内容加上反顿号,是将内部echo命令的输出作为外层echo的输出,会输出 变量c的具体内容>file1

难点分析

Exercise 0.1

  • 注意复制文件的命令cp和移动文件的命令mv不同,mv以后源目录下就不存在该文件了
  • 提取AAA文件的第8行到BBB文件中:sed -n ‘8p’ AAA > BBB (安静模式-n,且仅输出不编辑,不写-i)

Exercise 0.2

  • rmdir 命令只能删除空目录,如果想要递归删除目录及其内容,需要使用rm -r ./"file$a"。注意要使用双引号才能得到变量的值。

Exercise 0.3

  • 寻找文件file中所有含有sign字符串的行号使用grep命令

    grep -n "$int" "$file" | awk -F: '{print $1}' > $result
    

    awk命令用来分割答案,只输出行号

Exercise 0.4

  • 执行make命令时,默认执行当前目录下的makefile文件中的第一个命令

  • makefile文件中可以执行另一个makefile文件的内容

    $(MAKE) -C ./code
    

    $(MAKE) 是一个特殊的变量,它在 Makefile 中指向当前的 make 命令

    -Cmake 的一个选项,表示改变目录执行 make

    本题文件树如下

    csc/
    │
    ├── Makefile
    └── code/
        └── Makefile
        └── fibo.c
        └── main.c
    

    可以在csc目录下执行make,执行csc/code中Makefile文件的内容,省去一些麻烦

  • 注意头文件的依赖。csc/code中的fibo.cmain.c均引入头文件#include <fibo.h>,但是csc/code中没有这个头文件,因此在编译时要加上头文件的目录,否则会提示找不到头文件

    csc/Makefile如下:

    1 CFLAGS = -I./include # -I用来指定头文件的搜索路径,为当前目录下的include
    2 all:
    3         $(MAKE) -C ./code # 执行csc/code的Makefile
    4         gcc $(CFLAGS) ./code/main.o ./code/fibo.o -o fibo # 在编译时加入头文件的环境变量
    5 
    6 clean:
    7         rm -f ./code/main.o
    8         rm -f ./code/fibo.o
    

实验体会

写下实验过程中的体会。

lab0课下实验主要是练习一些指令,并不复杂,不过如果不够熟练的话就要经常翻看指导书或者是上网查找,比较浪费时间。

另外,实现同一个效果的途径可能很多,应该权衡最简便的方式。比如Exercise 0.4中csc/Makefile的编写可能有很多种方式,可以像我现在写的,也可以先cd到csc/code,再执行内部的Makefile文件。我个人认为一切从简比较好。

以及,在生成可执行文件时,可以先看看.c文件中的头文件在当前目录下是否存在,不过就算不存在,当你编译时它也会提醒你,再去添加头文件就好喽。

继续努力。

致谢

在撰写过程中,我参考了 YannaZhang 张杨 学姐的博客,感觉学到了很多,在此致谢。