MIT-Missing Semester_Topic 1: The Shell 练习题

本 Topic 的 MIT 讲解网页(练习题未给解答)

练习一

准备 Unix shell 环境(如 Bash 或 ZSH),注意 Windows 使用 cmd.exe 或 PowerShell 是错误的,应用 WSL子系统 或 Linux 虚拟机。
随后,通过 echo $SHELL 来检查所运行 shell 环境的正确性。

本人使用 Windows 的 Linux 子系统 WSL,这也是计算机系统基础课(ICS)所用。根据要求做以下测试:

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises$ echo $SHELL
/bin/bash

从而环境没问题。

练习二

/tmp 下建立文件夹 missing

注意事实上老师这里没让我们先建立文件夹 tmp,换言之应建立 /tmp/missing,于是一开始很容易这样做:

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises$ mkdir tmp/missing
mkdir: cannot create directory ‘tmp/missing’: No such file or directory

所以这样不对。另一方面,两条命令:先 mkdir 建立 tmp,再建立 missing,这样肯定可以。但是,我们可以考察 mkdir 的帮助性说明:

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises$ mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
  ...

不难发现只要加上 -p 这个 flag 就能实现我们目的(正所谓 “make parent directories as needed”)。实践如下:

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises$ mkdir -p tmp/missing
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises$ tree
.
└── tmp
    └── missing

2 directories, 0 files

另外,mkdir -pTopic 2 中有出现:

后来复习时发现讲座上老师已展示过……

练习三

查阅 touch 命令(即程序)的用法,可以使用 man

TOUCH(1)                                                                              User Commands                                                                             TOUCH(1)

NAME
       touch - change file timestamps

SYNOPSIS
       touch [OPTION]... FILE...

DESCRIPTION
       Update the access and modification times of each FILE to the current time.

       A FILE argument that does not exist is created empty, unless -c or -h is supplied.

       A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output.
       
       ...

因此,可以发现 touch 的名字很形象,可以 “触碰” 文件从而改变其时间戳(timestamp)。特别地,如果文件不存在,则新建一个指定名字的空文件。 这在 lecture 中没提及,同时也无疑非常实用。

练习四

使用 touchmissing 中新建文件 semester

果然老师让学生操练这一功能。

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises$ cd tmp
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp$ touch missing/semester
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp$ tree
.
└── missing
    └── semester

1 directory, 1 file

练习五

依次运行:
#!/bin/sh
curl --head --silent https://missing.csail.mit.edu

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp$ #!/bin/sh
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp$ curl --head --silent http://missing.csail.mit.edu
HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Content-Length: 162
Server: GitHub.com
Content-Type: text/html
Location: https://missing.csail.mit.edu/
X-GitHub-Request-Id: D404:1A36F1:397755:3BD27E:65B22E04
Accept-Ranges: bytes
Date: Thu, 25 Jan 2024 09:48:21 GMT
Via: 1.1 varnish
Age: 96
X-Served-By: cache-tyo11923-TYO
X-Cache: HIT
X-Cache-Hits: 1
X-Timer: S1706176101.210745,VS0,VE1
Vary: Accept-Encoding
...

The first line might be tricky to get working. It’s helpful to know that # starts a comment in Bash, and ! has a special meaning even within double-quoted (") strings. Bash treats single-quoted strings (') differently: they will do the trick in this case. See the Bash quoting manual page for more information.

练习六

尝试运行一个文件,如在 shell 中运行脚本 ./semester。查看其该文件的权限,理解其为何不可运行。

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp$ cd missing/
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ ./semester
bash: ./semester: Permission denied
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ ls -l
total 0
-rw-r--r-- 1 cowbby cowbby 0 Jan 25 17:39 semester

可见,对该文件的运行被拒绝。通过 ls -l 查看详细信息,由 rw-r--r-- 可以看出(三个字符一组,每组第3位),文件所有者(当然其实还有 “群组” 和 “所有人”)没有运行(x, executable)的权限。

练习七

sh开头而启动sh解释器,运行sh semester,分析结果。

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ sh semester
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$

显然其没有报错,应该真的运行起来了。这应该能说明 sh 解释器下可以运行文件。

练习八

man chmod 来查阅 chmood 命令。

输入 man chmod 后回车,便进入以下说明界面:

CHMOD(1)                                                                              User Commands                                                                             CHMOD(1)

NAME
       chmod - change file mode bits

SYNOPSIS
       chmod [OPTION]... MODE[,MODE]... FILE...
       chmod [OPTION]... OCTAL-MODE FILE...
       chmod [OPTION]... --reference=RFILE FILE...

DESCRIPTION
       This  manual  page  documents  the GNU version of chmod.  chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of
       changes to make, or an octal number representing the bit pattern for the new mode bits.

       The format of a symbolic mode is [ugoa...][[-+=][perms...]...], where perms is either zero or more letters from the set rwxXst, or a single letter from the  set  ugo.   Multiple
       symbolic modes can be given, separated by commas.

       A combination of the letters ugoa controls which users' access to the file will be changed: the user who owns it (u), other users in the file's group (g), other users not in the
       file's group (o), or all users (a).  If none of these are given, the effect is as if (a) were given, but bits that are set in the umask are not affected.

       The operator + causes the selected file mode bits to be added to the existing file mode bits of each file; - causes them to be removed; and = causes them to be added and  causes
       unmentioned bits to be removed except that a directory's unmentioned set user and group ID bits are not affected.
		
		...

对于比较初等的用法,应该大致理解上面即可。

练习九

运用 chmod 来更改权限,从而使 ./semester 能被正常运行。

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ chmod u+x semester
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ ls -l
total 0
-rwxr--r-- 1 cowbby cowbby 0 Jan 25 17:39 semester

首先我们发现运用 u+x 确实给所有者(u)增加了运行(x)的权限(初始情况可见上文)。之后尝试运行:

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ ./semester
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$

没有报错!可见真的运行了。此外,自己也根据之前 man 的说明,进一步实践了一下 chmod

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ chmod u-x semester
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ ls -l
total 0
-rw-r--r-- 1 cowbby cowbby 0 Jan 25 17:39 semester
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ chmod ua+x semester
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ ls -l
total 0
-rwxr-xr-x 1 cowbby cowbby 0 Jan 25 17:39 semester
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ chmod =x semester
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ ls -l
total 0
---x--x--x 1 cowbby cowbby 0 Jan 25 17:39 semester

练习十

运用 |>,将 semester 输出的 “上一次修改” 的日期写入 home 文件夹的 last-modified.txt 文件。

其实并没有理解这个练习。难道 ./semester 是有输出的吗……
因此就是运用 lecture 中讲的 tail -n1 做了一些类似的事情。
求教大佬教我一下www

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ ls -l | tail -n1 > /home/cowbby/last-modified.txt
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises/tmp/missing$ cat /home/cowbby/last-modified.txt
-rw-r--r-- 1 cowbby cowbby 0 Jan 25 18:24 tmp.txt

练习十一

Write a command that reads out your laptop battery’s power level or your desktop machine’s CPU temperature from /sys. Note: if you’re a macOS user, your OS doesn’t have sysfs, so you can skip this exercise.

看上去有点逆天hh,不过 lecture 中讲过相关的,是完全可以做到的。

cowbby@LAPTOP-UD6KDALFP:~$ cd /sys
cowbby@LAPTOP-UD6KDALFP:/sys$ ls
block  bus  class  dev  devices  firmware  fs  kernel  module  power

在这些系统文件夹中找到对应的文件之后 cat 即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值