MIT-Missing-Semester
MIT-Missing-Semester lecture1 课程概览与 shell
Note: 本文约定,shell程序块中的每行第一字符为$
,表示$
后面是在终端输入的shell命令,第一个字符不为$
表示命令的输出。例如,下面,第一行表示在shell中执行echo hello
这条命令,第二行hello
为第一行命令的输出。第三行命令echo hello > hello.txt
在终端上没有输出。#
跟在命令后面是解释,单独的一行中是shell的输出。abc
代表一个用户名。
$ echo hello
hello
$ echo hello > hello.txt # 输出重定向,屏幕上没有输出
课程概览与Shell
使用shell
-
rmdir
只用于移除空目录 -
cd -
返回上一个目录$ cd ./test/src # 假定开始在~/MIT-Missing-Semester,cd后在~/MIT-Missing-Semester/test/src $ cd - # ~/MIT-Missing-Semester
-
mv
实现文件或文件夹的重命名
mv [olddirectory/oldfile] [newdirectory/newfile] # 可以看成一个重命名的方法
-
rm -r [directory]
递归删除[directory]中文件,并且删除[directory]文件夹 -
Ctrl+l
清空当前终端,等于命令clear
-
cat
打印一个文件的内容 -
xdg-open [file]
以一个适当的程序打开文件 -
查看当前系统的所有shell
$ cat /etc/shells # /etc/shells: valid login shells /bin/sh /bin/bash /usr/bin/bash /bin/rbash /usr/bin/rbash /bin/dash /usr/bin/dash /usr/bin/screen /usr/bin/tmux /bin/zsh /usr/bin/zsh
-
pwd
打印当前路径$ pwd /home/abc/DATA/7788/MIT-Missing-Semester
-
shell的内置命令会在环境变量
PATH
定义的路径中寻找$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
-
which
可以查看命令所在位置$ which echo echo: shell built-in command $ which python3 /usr/bin/python3
重定向与管道
- 重定向
<
和>
在shell中,程序有两个主要的“流”:输入和输出流。当程序尝试读取时,会从输入流中读取,输出时会将信息输出到输出流中。通常,输入输出流都是终端(Terminal)。也就是说,键盘作为输入,屏幕作为输出。
< [file] # 将文件file的内容作为输入
> [file] # 将程序的输出输出到文件file中
例子
$ echo hello > hello.txt # 无输出
$ cat hello.txt
hello
$ cat < hello.txt
hello
$ cat < hello.txt > hello2.txt
$ cat hello2.txt
hello
$ cat hello.txt >> hello2.txt
$ cat hello2.txt
hello
hello
使用>>
来向一个文件追加内容,也就是文件已经存在的内容保留,向文件尾追加内容。
- 管道(pipes)
|
使用管道,我们能够更好的利用文件重定向,|
操作符允许我们将一个程序的输出和另一个程序的输入链接起来。
[程序1] | [程序2] # 将程序1的输出作为程序2的输入
$ ls -l # 输出当前文件夹下的文件,按照long格式输出
total 8
-rw-rw-r-- 1 abc abc 6 11月 19 21:42 hello2.txt
-rw-rw-r-- 1 abc abc 6 11月 19 21:38 hello.txt
$ ls -l | tail -n 1 # 最终将输出|左侧程序的最后一行
-rw-rw-r-- 1 abc abc 6 11月 19 21:38 hello.txt
多个管道连起来,反正是左侧的程序输出作为右侧程序的输入,从左到右运行程序
$ curl --head --silent google.com
HTTP/1.1 301 Moved Permanently
Content-Length: 219
Cache-Control: public, max-age=2592000
Connection: keep-alive
Content-Type: text/html; charset=UTF-8
Date: Sun, 20 Nov 2022 02:55:14 GMT
Expires: Tue, 20 Dec 2022 02:55:14 GMT
Keep-Alive: timeout=4
Location: http://www.google.com/
Proxy-Connection: keep-alive
Server: gws
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0
$ curl --head --silent google.com | grep --ignore-case content-length # 将http的头,作为grep的输入,匹配"content-length"字符串,忽略大小写,最终结果打印出匹配成功的行
Content-Length: 219
$ curl --head --silent google.com | grep --ignore-case content-length | cut --delimiter=' ' -f2 # 将上面匹配成功的行作为cut的输入,按照delimiter分割字符串(这里是空格),输出分割后的第二个字符串
219
$ curl --head --silent google.com | grep --ignore-case content-length | cut --delimiter=' ' -f1 # 输出分割后的第一个字符串
Content-Length:
超级用户权限
对于大多数的类 Unix 系统,有一类用户是非常特殊的,那就是:根用户(root user)。根用户几乎不受任何限制,他可以创建、读取、更新和删除系统中的任何文件。 通常在我们并不会以根用户的身份直接登录系统,因为这样可能会因为某些错误的操作而破坏系统。 取而代之的是我们会在需要的时候使用 sudo
命令。顾名思义,它的作用是让您可以以**do as su(super user 或 root 的简写)**的身份执行一些操作。 当您遇到拒绝访问(permission denied)的错误时,通常是因为此时您必须是根用户才能操作。然而,请再次确认您是真的要执行此操作。
使用重定向或者管道时,如果右侧程序需要超级用户权限运行,那么如果将sudo
写在最前面,依然会发生错误,而应该将sudo
写在右侧程序的前面。
[程序1,program2] > [超级用户权限程序2,program2]
$ sudo program1 > program2 # 会产生错误,第二个程序权限不够
$ program1 > sudo program2 # 不会产生权限错误
关于 shell,有件事我们必须要知道。|
、>
、和 <
是通过 shell 执行的,而不是被各个程序单独执行。 echo
等程序并不知道 |
的存在,它们只知道从自己的输入输出流中进行读写。
课后练习
-
本课程需要使用类Unix shell,例如 Bash 或 ZSH。如果您在 Linux 或者 MacOS 上面完成本课程的练习,则不需要做任何特殊的操作。如果您使用的是 Windows,则您不应该使用 cmd 或是 Powershell;您可以使用Windows Subsystem for Linux或者是 Linux 虚拟机。使用
echo $SHELL
命令可以查看您的 shell 是否满足要求。如果打印结果为/bin/bash
或/usr/bin/zsh
则是可以的。Answer:
$ echo $SHELL /usr/bin/zsh
-
在
/tmp
下新建一个名为missing
的文件夹。Answer:
$ cd /tmp $ mkdir missing
-
用
man
查看程序touch
的使用手册。Answer:
q
退出手册阅读。touch
命令一般用来修改文件时间戳,或者新建一个不存在的文件$ man touch 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. Mandatory arguments to long options are mandatory for short options too. -a change only the access time -c, --no-create do not create any files -d, --date=STRING Manual page touch(1) line 1 (press h for help or q to quit)
-
用
touch
在missing
文件夹中新建一个叫semester
的文件。Answer:
$ cd /tmp/missing/ $ touch semester $ ls -l total 0 -rw-rw-r-- 1 zhj zhj 0 11月 20 11:39 semerster
-
将以下内容一行一行地写入
semester
文件:#!/bin/sh curl --head --silent https://missing.csail.mit.edu
第一行可能有点棘手, 在Bash中表示注释,而 即使被双引号()包裹也具有特殊的含义。 单引号()则不一样,此处利用这一点解决输入问题。更多信息请参考
-
尝试执行这个文件。例如,将该脚本的路径(
./semester
)输入到您的shell中并回车。如果程序无法执行,请使用ls
命令来获取信息并理解其不能执行的原因。Answer:
$ ./semester sh: permission denied: ./semerster $ ls -l # 看出semester是没有可执行的权限 total 4 -rw-rw-r-- 1 zhj zhj 61 11月 20 11:43 semerster $ chmod +x semester $ ls -l total 4 -rwxrwxr-x 1 zhj zhj 61 11月 20 11:43 semerster $ ./semester HTTP/1.1 200 Connection established HTTP/2 200 server: GitHub.com content-type: text/html; charset=utf-8 last-modified: Fri, 28 Oct 2022 00:33:31 GMT access-control-allow-origin: * etag: "635b235b-1f37" expires: Sun, 20 Nov 2022 02:45:42 GMT cache-control: max-age=600 x-proxy-cache: MISS x-github-request-id: CE6C:262C:DA313C:FFE11C:6379927E accept-ranges: bytes date: Sun, 20 Nov 2022 03:48:08 GMT via: 1.1 varnish age: 313 x-served-by: cache-bfi-krnt7300054-BFI x-cache: HIT x-cache-hits: 1 x-timer: S1668916089.594123,VS0,VE5 vary: Accept-Encoding x-fastly-request-id: 19e6ff41fdd20173e5e86b085ceadb8f7a4b9bcf content-length: 7991
-
查看
chmod
的手册(例如,使用man chmod
命令)Answer:
$ 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 ei‐ ther 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 af‐ Manual page chmod(1) line 1 (press h for help or q to quit)
-
使用 命令改变权限,使
./semester
能够成功执行,不要使用sh semester
来执行该程序。您的shell 是如何知晓这个文件需要使用sh
来解析呢?更多信息请参考:shebangAnswer:第一行说明了用哪个程序来解释这个文件。
-
使用
|
和>
,将semester
文件输出的最后更改日期信息,写入主目录下的last-modified.txt
的文件中Answer:
$ ./semerster | grep last-modified > ~/last-modified.txt $ cat ~/last-modified.txt last-modified: Fri, 28 Oct 2022 00:33:31 GMT
-
写一段命令来从
/sys
中获取笔记本的电量信息,或者台式机 CPU 的温度。注意:macOS 并没有 sysfs,所以 Mac 用户可以跳过这一题。Answer:我的是台式机,好像连温度都没有,不管这题了。网上得到笔记本电量信息的命令。
$ cat /sys/class/power_supply/battery/capacit