OS-lab0 实验报告
思考题
Thinking-0.1
Q:执行命令 cat Modified.txt,观察其结果和第一次执行 add 命令之前的 status 是 否一样,并思考原因。
A:这两个 status 不一样,因为第一次 README.txt 是新文件,没有被暂存,因此显示Untracked files,但当最后我们修改之前已经被提交的 README.txt,git 检测到的是修改了已有文件,故显示Changes not staged for commit。
Untracked.txt:
1
2
3
4
5
6
7
8
9
10
|
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.txt
Untracked.txt
nothing added to commit but untracked files present (use "git add" to track)
|
Modified.txt:
1
2
3
4
5
6
7
8
9
10
11
|
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
Modified.txt
Stage.txt
Untracked.txt
|
Thinking-0.2
Q: 仔细看看0.10,思考一下箭头中的 add the file 、stage the file 和 commit 分别对应的是 Git 里的哪些命令呢?
A:分别是git add,git add和git commit,前两个的区别在于添加的是未跟踪文件还是已修改文件。
Thinking-0.3
Q:思考下列问题: 1. 代码文件 print.c 被错误删除时,应当使用什么命令将其恢复? 2. 代码文件 print.c 被错误删除后,执行了 git rm print.c 命令,此时应当 使用什么命令将其恢复? 3. 无关文件 hello.txt 已经被添加到暂存区时,如何在不删除此文件的前提下 将其移出暂存区?
A:
-
-
1
2
|
git restore --staged print.c
git restore print.c
|
-
1
|
git restore --staged hello.txt
|
Thinking-0.4
Q:按照指导书进行若干操作,通过多次git log观察其变化。
A:四次git log输出分别如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 11:11:32 2025 +0800
3
commit a41d443ed1c531f6a4ae09e37ab25a0b0c1ad229
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 11:11:17 2025 +0800
2
commit 0dcbd1a4f693813cfb9212b974a6d161a2696de1
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 11:10:45 2025 +0800
1
commit 439b930fdd6bb1cebebe48bd91237c44cdfe4c15
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 10:46:12 2025 +0800
23371301
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 11:11:17 2025 +0800
2
commit 0dcbd1a4f693813cfb9212b974a6d161a2696de1
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 11:10:45 2025 +0800
1
commit 439b930fdd6bb1cebebe48bd91237c44cdfe4c15
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 10:46:12 2025 +0800
23371301
|
1
2
3
4
5
6
7
8
9
10
|
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 11:10:45 2025 +0800
1
commit 439b930fdd6bb1cebebe48bd91237c44cdfe4c15
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 10:46:12 2025 +0800
23371301
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
commit 3bf0d798caadb39ec68cb203c6a83865b21d19be (HEAD -> main)
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 11:11:32 2025 +0800
3
commit a41d443ed1c531f6a4ae09e37ab25a0b0c1ad229
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 11:11:17 2025 +0800
2
commit 0dcbd1a4f693813cfb9212b974a6d161a2696de1
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 11:10:45 2025 +0800
1
commit 439b930fdd6bb1cebebe48bd91237c44cdfe4c15
Author: Xiao Yihan <23371301@buaa.edu.cn>
Date: Fri Mar 14 10:46:12 2025 +0800
23371301
|
以上结果展现了在git中回退版本的各种操作。
Thinking-0.5
Q:执行如下命令, 并查看结果:
1
2
3
4
|
echo first
echo second > output.txt
echo third > output.txt
echo forth >> output.txt
|
A:结果如下(对于输出到文件的指令展示文件内容):
这主要体现了重定向中覆盖和追加的不同效果。
Thinking-0.6
Q:使用你知道的方法(包括重定向)创建下图内容的文件(文件命名为 test), 将创建该文件的命令序列保存在 command 文件中,并将 test 文件作为批处理文件运行,将 运行结果输出至 result 文件中。给出 command 文件和 result 文件的内容,并对最后的结 果进行解释说明(可以从 test 文件的内容入手). 具体实现的过程中思考下列问题: echo echo Shell Start 与 echo echo Shell Start 效果是否有区别; echo echo $c>file1 与 echo echo $c>file1 效果是否有区别.
A:
command 文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
echo 'echo Shell Start...'
echo 'echo set a = 1'
echo 'a=1'
echo 'echo set b = 2'
echo 'b=2'
echo 'echo set c = a+b'
echo 'c=$[$a+$b]'
echo 'echo c = $c'
echo 'echo save c to ./file1'
echo 'echo $c>file1'
echo 'echo save b to ./file2'
echo 'echo $b>file2'
echo 'echo save a to ./file3'
echo 'echo $a>file3'
echo 'echo save file1 file2 file3 to file4'
echo 'cat file1>>file4'
echo 'cat file2>>file4'
echo 'cat file3>>file4'
echo 'echo save file4 to ./result'
echo 'cat file4>>result'
|
result 文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Shell Start...
set a = 1
set b = 2
set c = a+b
c = 3
save c to ./file1
save b to ./file2
save a to ./file3
save file1 file2 file3 to file4
save file4 to ./result
cat file1
cat file2
cat file3
3
2
1
|
在实现中,echo echo Shell Start 与 echo echo Shell Start没有区别,因为后面的内容会被视为普通字符串,无论加不加引号都不会触发解析。但是 echo echo $c>file1 与 echo echo $c>file1 有区别,因为其中有 $ 的存在,如果不用’‘包裹,就会对其做变量解析。
难点分析
本次实验主要考察对实验环境、Linux 操作系统的了解、以及git 等重要工具的使用。我认为这次作业的主要难点有三个方面:git 的使用、Makefile 的设计以及 shell 脚本的撰写。
实验体会
在本次 lab0 实验中,我初步掌握了 Linux 的常用命令,深入接触了 Git、Makefile 和 Shell 脚本等开发工具。最初接触这些新工具时,我感到有些困惑和不适应,例如Makefile 的嵌套等,而攻克这些问题需要我再次查阅指导书或向互联网寻求帮助。
Shell 脚本部分可能是我钻研最多的环节。文本处理命令如 sed、awk 初看起来很难,实际上也不简单。尤其是理解单双引号和$带来的行为差异和一些特殊参数的用法,这些细节在学习过程中很难吃透,却容易在上机时导致意想不到的问题。
总的来说,这次实验让我认识到,掌握这些基础工具不仅能提高编程效率,更是培养系统思维和工程化思想的重要途径。虽然学习曲线有些陡峭,但这些技能的价值会在今后的项目开发中不断体现出来。