MIT-Missing Semester_Topic 1: The Shell 练习题

2024-01-25 19:44

本文主要是介绍MIT-Missing Semester_Topic 1: The Shell 练习题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

        • 练习一
        • 练习二
        • 练习三
        • 练习四
        • 练习五
        • 练习六
        • 练习七
        • 练习八
        • 练习九
        • 练习十
        • 练习十一

MIT 讲解网页

练习一

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

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

cowbby@LAPTOP-UD6KDLFP:~/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,再建立 missnig,这样肯定可以。但是,我们可以考察 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 就能实现我们目的。实践如下:

cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises$ mkdir -p tmp/missing
cowbby@LAPTOP-UD6KDALFP:~/Missing Semester/1_shell/exercises$ tree
.
└── tmp└── missing2 directories, 0 files
练习三

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

TOUCH(1)                                                                              User Commands                                                                             TOUCH(1)NAMEtouch - change file timestampsSYNOPSIStouch [OPTION]... FILE...DESCRIPTIONUpdate 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└── semester1 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 命令。

CHMOD(1)                                                                              User Commands                                                                             CHMOD(1)NAMEchmod - change file mode bitsSYNOPSISchmod [OPTION]... MODE[,MODE]... FILE...chmod [OPTION]... OCTAL-MODE FILE...chmod [OPTION]... --reference=RFILE FILE...DESCRIPTIONThis  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 ofchanges 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.   Multiplesymbolic 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 thefile'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  causesunmentioned 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 即可。

这篇关于MIT-Missing Semester_Topic 1: The Shell 练习题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/644402

相关文章

dp算法练习题【8】

不同二叉搜索树 96. 不同的二叉搜索树 给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。 示例 1: 输入:n = 3输出:5 示例 2: 输入:n = 1输出:1 class Solution {public int numTrees(int n) {int[] dp = new int

ActiveMQ—Queue与Topic区别

Queue与Topic区别 转自:http://blog.csdn.net/qq_21033663/article/details/52458305 队列(Queue)和主题(Topic)是JMS支持的两种消息传递模型:         1、点对点(point-to-point,简称PTP)Queue消息传递模型:         通过该消息传递模型,一个应用程序(即消息生产者)可以

jenkins 插件执行shell命令时,提示“Command not found”处理方法

首先提示找不到“Command not found,可能我们第一反应是查看目标机器是否已支持该命令,不过如果相信能找到这里来的朋友估计遇到的跟我一样,其实目标机器是没有问题的通过一些远程工具执行shell命令是可以执行。奇怪的就是通过jenkinsSSH插件无法执行,经一番折腾各种搜索发现是jenkins没有加载/etc/profile导致。 【解决办法】: 需要在jenkins调用shell脚

站长常用Shell脚本整理分享(全)

站长常用Shell脚本整理分享 站长常用Shell脚本整理分享1-10 站长常用Shell脚本整理分享11-20 站长常用Shell脚本整理分享21-30 站长常用Shell脚本整理分享31-40 站长常用Shell脚本整理分享41-50 站长常用Shell脚本整理分享51-59 长期更新

Shell脚本实现自动登录服务器

1.登录脚本 login_server.sh #!/bin/bash# ReferenceLink:https://yq.aliyun.com/articles/516347#show all host infos of serverList.txtif [[ -f ./serverList.txt ]]thenhostNum=`cat ./serverList.txt | wc -l`e

[轻笔记]ubuntu shell脚本切换conda环境

source /home/yourhostname/anaconda3/etc/profile.d/conda.sh # 关键!!!conda activate env_name

[轻笔记] ubuntu Shell脚本实现监视指定进程的运行状态,并能在程序崩溃后重启动该程序

根据网上博客实现,发现只能监测进程离线,然后对其进行重启;然而,脚本无法打印程序正常状态的信息。自己通过不断修改测试,发现问题主要在重启程序的命令上(需要让重启的程序在后台运行,不然会影响监视脚本进程,使其无法正常工作)。具体程序如下: #!/bin/bashwhile [ 1 ] ; dosleep 3if [ $(ps -ef|grep exe_name|grep -v grep|

adb shell 执行后台程序后断开adb后台进程被结束的解决办法

环境:Android 版本 Android8 通常让程序后台执行就是在命令 最后加上 &即可,但是在Android 8上实验发现,程序的确后台了,但是拔掉USB线再连接上发现进程已结束。不确定Android早期版本是否存在此问题。 参考网上一些Linux方法,如加nohup 仍然无效,还是会结束。看来Android adb shell 与 Linux shell 还是有一定区别。 后来在网上

shell脚本中变量中字符串替换的测试 /和//的区别

test_char=abbbcbbbf echo "bf:test_char = " $test_char test_char=${test_char/bbb/ddd} echo "af:test_char = " $test_char 输出: bf:test_char =  abbbcbbbf af:test_char =  adddcbbbf 只匹配第一个

shell循环sleep while例子 条件判断

i=1# 小于5等于时候才执行while [ ${i} -le 5 ]doecho ${i}i=`expr ${i} + 1`# 休眠3秒sleep 3doneecho done 参考 http://c.biancheng.net/cpp/view/2736.html