The Shell¶
约 1290 个字 103 行代码 预计阅读时间 6 分钟
前言
临近期末,前方有一大堆的背诵+考试等着我,但我好像不是很慌(这是什么原因...), 感觉我是那种不死到临头不会动的人,闲的无聊决定刷一下这门广受好评的公开课,就当做娱乐放松罢(计划使用英文笔记,锻炼一下写作能力,如果有大量错误请见谅 ----课程链接: 2020 Lectures · Missing Semester
本章节链接: Missing Semester Overview
1. What is the shell¶
All kinds of shell share one common core: they allow you to run programs, give them input, and inspect their output in a semi-structured way
As I use macos, my shell is terminal or namely zsh. (uh... btw, to be honest, I don't quiet know the differences between zsh and terminal and shell, hope I'm right..
2. Using the shell¶
When you launch your terminal, you will see a prompt that often looks a little like this:
Bash | |
---|---|
Down below is what my shell looks like:
Bash | |
---|---|
This is the main textual interface to the shell. It tells you that you are on the machine missing
and that your “current working directory”, or where you currently are, is ~
(short for “home”). The $
tells you that you are not the root user (more on that later). At this prompt you can type a command, which will then be interpreted by the shell.
Some basic command¶
- date
date
prints the current date and time
- echo
echo
simply prints out its arguments.
If you want to provide an argument that contains spaces or other special characters (e.g., a directory named “My Photos”
), you can either quote the argument with '
or "
("My Photos"
), or escape just the relevant characters with \
(My\ Photos
).
But... it seems that my macos doesn't need those quote or back slash, it will also print out the correct arguments
just like ⬇️
If the shell is asked to execute a command that doesn’t match one of its programming keywords, it consults an environment variable called $PATH
that lists which directories the shell should search for programs when it is given a command:
Bash | |
---|---|
3. Navigating in the shell¶
1. pwd
pwd
prints the absolute path of current working directory
2. cd
cd
change your working directory to another
Bash | |
---|---|
3. ls
To see what lives in a given directory, we use the ls
command:
Bash | |
---|---|
4. ls -l
ls -l
use a long listing format:
- First, the
d
at the beginning of the line tells us that missing is a directory. - Then follow three groups of three characters (rwx). These indicate what permissions the owner of the file (missing), the owning group (users), and everyone else respectively have on the relevant item.
- A
-
indicates that the given principal does not have the given permission. - Above, only the owner is allowed to modify (w) the missing directory (i.e., add/remove files in it).
- To enter a directory, a user must have “search” (represented by “execute”: x) permissions on that directory (and its parents).
- To list its contents, a user must have read ® permissions on that directory. For files, the permissions are as you would expect.
- Quick summary:
rwx
represents the permissions,r
for read,w
for modify,x
for execute,-
for don't have the given permission. - Notice that nearly all the files in
/bin
have thex
permission set for the last group, “everyone else”, so that anyone can execute those programs.
5. other else
mv
(to rename/move a file)cp
(to copy a file)mkdir
(to make a new directory).man
( It takes as an argument the name of a program, and shows you its manual page. Pressq
to exit.)
Bash | |
---|---|
4. Connecting programs¶
< file
and > file
. These let you rewire the input and output streams of a program to a file respectively:
Bash | |
---|---|
cat
is a program that concatenates files. When given file names as arguments, it prints the contents of each of the files in sequence to its output stream. But when cat is not given any arguments, it prints contents from its input stream to its output stream (like in the third example above).
You can also use >>
to append to a file
The |
operator lets you “chain” programs such that the output of one is the input of another:
Bash | |
---|---|
The command tail
means print the tail lines of a file, -n
means the index of the line(starting from 1 in the end), -n1
means to print the exactly last one line of the file.
5. A versatile and powerful tool¶
On most Unix-like systems, one user is special: the “root” user.
The root user is above (almost) all access restrictions, and can create, read, update, and delete any file in the system.
using the sudo
command. As its name implies, it lets you “do” something “as su” (short for “super user”, or “root”)
One thing you need to be root in order to do is writing to the sysfs file system mounted under /sys
. sysfs exposes a number of kernel parameters as files, so that you can easily reconfigure the kernel on the fly without specialized tools. Note that sysfs does not exist on Windows or macOS.
For example, the brightness of your laptop’s screen is exposed through a file called brightness under
Bash | |
---|---|
By writing a value into that file, we can change the screen brightness. Your first instinct might be to do something like:
Bash | |
---|---|
Simply speaking, the error occurs because the shell (which is authenticated just as your user) tries to open the brightness file for writing, before setting that as sudo echo’s output, but is prevented from doing so since the shell does not run as root.
Bash | |
---|---|
Since the tee
program is the one to open the /sys
file for writing, and it is running as root, the permissions all work out.
6. exercises¶
Warning
I will write my own solution to the problem, If you want to solve those by yourself, please stop.
1. Create a new directory called missing
under /tmp
.
2. Use touch
to create a new file called semester
in missing
.
5. Write the following into that file, one line at a time:
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 (Bash Reference Manual) manual page for more information.
Bash | |
---|---|
6. execute the file
result:
doesn't work...
using ls -l
to check the permissions:
Bash | |
---|---|
7. Use chmod
to make it possible to run the command ./semester
First, quick dive to chmod
chmod
: change file modes or Access Control Lists
Usage: chmod [选项]... 模式[,模式]... 文件...
mode
define the permissions of the file or directory, usually 3 number. Each number represents the permissions of user, group and other respectively.
Each write, read, and execute permissions have the following number value:
- r (read) = 4
- w (write) = 2
- x (execute) = 1
- no permissions = 0
To find out the file’s permissions in numeric mode simply calculate the totals for all users classes. For example, to give read, write and execute permission to the file’s owner, read and execute permissions to the file’s group and only read permissions to all other users you would do the following:
- Owner: rwx=4+2+1=7
- Group: r-x=4+0+1=5
- Others: r-x=4+0+0=4
Using the above knowledge, the solution:
result
8. Use |
and >
to write the “last modified”
date output by semester
into a file called last-modified.txt
in your home directory.
Bash | |
---|---|
result