0%

OS Lab0

1 思考题

Thinking 0.1

三次运行 git status 时README.txt位置的对比:

  • Untracked:此时位于“未跟踪的文件”中
  • Stage:此时位于“要提交的变更:新文件:”中
  • Modified:此时位于“尚未暂存以备提交的变更”中

分别对应文件在Git中的三种状态,其中第一次和第三次不一样,区别在于第一次时未跟踪文件,第三次时已跟踪文件,虽然加入暂存区都使用git add命令,但是实际上处于两种不同状态(未跟踪和已修改)

Thinking 0.2

  • Add the file = “git add {filename}”
  • Stage the file = “git add {filename}”
  • Commit = “git commit”

Thinking 0.3

  1. “git checkout – print.c”
  2. “git reset HEAD print.c” & “git checkout – print.c”
  3. “git rm --cached hello.txt”

Thinking 0.4

  • 回退到HEAD^后,"git log"返回了到2为止的提交(提交1和提交2)
  • 按提交1的哈希值回退后,"git log"返回了到1为止的提交(仅提交1)
  • 输入之前提交3的哈希值reset后,"git log"回到了未回退的状态(提交1、2和3)

Thinking 0.5

以下是执行命令和结果:

1
2
3
4
5
6
7
8
9
10
11
12
$ echo first
first
$ echo second > output.txt
$ cat output.txt
second
$ echo third > output.txt
$ cat output.txt
third
$ echo forth >> output.txt
$ cat output.txt
third
forth

Thinking 0.6

以下是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..." > 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文件的内容:

1
2
3
3
2
1

解释说明(按照test中的运行情况描述):

  • 首先为a、b赋值,再根据a+b给c赋值,得到a=1, b=2, c=3
  • 然后将c, b, a分别写入到file1, file2, file3, 三个文件内容分别为3, 2, 1
  • 然后按file1, file2, file3的顺序依次将内容写入file4,再将file4写入result

因此result中分别三行为3,2,1。

echo echo Shell Start 与 echo `echo Shell Start`是否有区别

有区别。前者输出"echo Shell Start",后者输出"Shell Start"。

echo echo $c>file1 与 echo`echo $c>file1`是否有区别

有区别。前者会写入"echo ${c}“,后者只会写入”${c}“,且前者在shell中没有输出,后者会输出一个空换行,其中”${c}"指shell中变量c的值。

2 难点分析

本次课下实验中大致可以按知识点分为C部分、Makefile部分和Shell部分。其中C部分均为大一基础知识,不涉及难点。下面主要分析Makefile和Shell涉及的难点。

Shell 取第x行内容

使用 “cat {filename} | tail -n +{x} | head -n 1” 取filename文件的第x行。具体来说是用cat取全文后管道传入tail命令由于带"+",因此会输出文件从第x行开始一直到结尾的内容进入head,而head会输出所收到内容的第一行,因此得到所需的第x行内容。如果需要第x行开始的共y行内容,将1改为y即可。

Shell 变量自增

要写成"a=$[$a+1]“或者"a=$(($a+1))”

grep

-n可以输出行号在最前,而-o可以只输出正则匹配到的部分。

Makefile

.SUFFIXES可以指定处理某种固定后缀名文件的方式

gcc

多文件编译时先用-c编译到.o,再一同链接

如果include了一些.h文件,需要加-I告诉gcc具体的include目录。

实验体会

Lab0作为引子性质的Lab,在经过CO的洗礼之后倒显得并没有想象的高压力。期待后续的内容!