MIT-Missing-Semester-01
Topic 1 Shell
Points
- date
- echo
- escape
- echo $PATH
- which
- pwd
- cd
- dot, dot dot
- ls
- /
- - dash: cd -, program -h –help
- permission bits rwx user group others
- mv
- cp
- mkdir rmdir
- quota “ “
- man: manual page
- two streams >, <, >>, <<
- tail -n1
- pipe |
- sudo echo 1 > /sys/** right ? Permission denied ?
- sudo su
- echo 1 | sudo tee /sys/**
- find
Exercises
For this course, you need to be using a Unix shell like Bash or ZSH. If you are on Linux or macOS, you don’t have to do anything special. If you are on Windows, you need to make sure you are not running cmd.exe or PowerShell; you can use Windows Subsystem for Linux or a Linux virtual machine to use Unix-style command-line tools. To make sure you’re running an appropriate shell, you can try the command
echo $SHELL
. If it says something like/bin/bash
or/usr/bin/zsh
, that means you’re running the right program.1
2hadoop@wool-virtual-machine:~$ echo $SHELL
/bin/bash
Create a new directory called
missing
under/tmp
.1
2hadoop@wool-virtual-machine:~$ cd /tmp
hadoop@wool-virtual-machine:/tmp$ mkdir missing
Look up the
touch
program. Theman
program is your friend.1
man touch
Use
touch
to create a new file calledsemester
inmissing
.1
hadoop@wool-virtual-machine:/tmp$ touch missing/semester
Write the following into that file, one line at a time:
1
2!/bin/sh
curl --head --silent https://missing.csail.mit.eduThe 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.1
2
3
4
5
6hadoop@wool-virtual-machine:/tmp$ cd missing
hadoop@wool-virtual-machine:/tmp/missing$ echo \#\!/bin/bash > semester
hadoop@wool-virtual-machine:/tmp/missing$ echo curl --head --silent https://missing.csail.mit.edu >> semester
hadoop@wool-virtual-machine:/tmp/missing$ cat semester
!/bin/bash
curl --head --silent https://missing.csail.mit.edu
Try to execute the file, i.e. type the path to the script (
./semester
) into your shell and press enter. Understand why it doesn’t work by consulting the output ofls
(hint: look at the permission bits of the file).1
2
3
4hadoop@wool-virtual-machine:/tmp/missing$ ./semester
bash: ./semester: permission denied
hadoop@wool-virtual-machine:/tmp/missing$ ls -l semester
-rw-rw-r-- 1 hadoop hadoop 63 3月 29 21:11 semester
Run the command by explicitly starting the
sh
interpreter, and giving it the filesemester
as the first argument, i.e.sh semester
. Why does this work, while./semester
didn’t?1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22hadoop@wool-virtual-machine:/tmp/missing$ sh semester
HTTP/2 200
server: GitHub.com
content-type: text/html; charset=utf-8
last-modified: Fri, 04 Mar 2022 17:03:44 GMT
access-control-allow-origin: *
etag: "62224670-1f37"
expires: Mon, 28 Mar 2022 08:24:18 GMT
cache-control: max-age=600
x-proxy-cache: MISS
x-github-request-id: C87C:7EE2:1B48AE:1F3E2D:62416E5A
accept-ranges: bytes
date: Tue, 29 Mar 2022 13:19:08 GMT
via: 1.1 varnish
age: 0
x-served-by: cache-hnd18742-HND
x-cache: HIT
x-cache-hits: 1
x-timer: S1648559948.926671,VS0,VE390
vary: Accept-Encoding
x-fastly-request-id: 64c590c0fcb701c17b33cd011d2e48c65d877970
content-length: 7991Reason: the permission bits of the semester indicates that everyone could read it and sh just reads the semester
Look up the
chmod
program (e.g. useman chmod
).1
2
3
4
5
6
7NAME
chmod - change file mode bits
SYNOPSIS
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...
Use
chmod
to make it possible to run the command./semester
rather than having to typesh semester
. How does your shell know that the file is supposed to be interpreted usingsh
? See this page on the shebang line for more information.1
2
3
4
5
6
7
8
9hadoop@wool-virtual-machine:/tmp/missing$ chmod a+x semester
hadoop@wool-virtual-machine:/tmp/missing$ ls -l
total 4
-rwxrwxr-x 1 hadoop hadoop 63 3月 29 21:11 semester
hadoop@wool-virtual-machine:/tmp/missing$ ./semester
HTTP/2 200
server: GitHub.com
........Wiki: In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script. It is also called sha-bang, hashbang, pound-bang, or hash-pling.
When a text file with a shebang is used as if it is an executable in a Unix-like operating system, the program loader mechanism parses the rest of the file’s initial line as an interpreter directive.
Use
|
and>
to write the “last modified” date output bysemester
into a file calledlast-modified.txt
in your home directory.1
2hadoop@wool-virtual-machine:/tmp/missing$ ls -l semester | cut --delimiter=' ' -f6,7,8,9 | sudo tee ~/last-modified.txt
3月 29 21:47
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.1
i can't find them.