shell
特点
they allow you to run programs, give them input, and inspect their output in a semi-structured way
使用
date # 显示时间echo hello # 输入参数‘hello’并输出echo "hello world"
echo $PATH # 输出环境变量¥PATH# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin当我们执行 echo命令时,shell 了解到需要执行 echo这个程序,随后它便会在 $PATH中搜索由 :所分割的一系列目录,基于名字搜索该程序。当找到该程序时便执行。确定某个程序名代表的是哪个具体的程序,可以使用 which程序。我们也可以绕过 $PATH,通过直接指定需要执行的程序的路径来执行该程序。
shell中导航
shell 中的路径是一组被分割的目录,在 Linux 和 macOS 上使用 /分割,而在Windows上是 \\。/为根目录。
pwd # 获取当前路径cd /home # 根目录下的home文件夹cd ./home # 当前目录下的home文件夹cd .. # 上级目录一般来说,当我们运行一个程序时,如果我们没有指定路径,则该程序会在当前目录下执行。
ls # 查看目录文件ls --helpls -l /home# drwxr-xr-x 4 root root 4096 11月 30 21:01 data首先,本行第一个字符 d表示 data是一个目录。然后接下来的九个字符,每三个字符构成一组。 (rwx). 它们分别代表了文件所有者(root),用户组(root) 以及其他所有人具有的权限。其中 -表示该用户不具备相应的权限。
从上面的信息来看,只有文件所有者可以修改(w),data 文件夹 (例如,添加或删除文件夹中的文件)。
为了进入某个文件夹,用户需要具备该文件夹以及其父文件夹的“搜索”权限(以“可执行”:x)权限表示。为了列出它的包含的内容,用户必须对该文件夹具备读权限(r)。对于文件来说,权限的意义也是类似的。注意,/bin 目录下的程序在最后一组,即表示所有人的用户组中,均包含 x 权限,也就是说任何人都可以执行这些程序。
mv test ./data/test.txtcp test.c test01.cmkdir testman ls程序中创建连接
在 shell 中,程序有两个主要的“流”:它们的输入流和输出流。 当程序尝试读取信息时,它们会从输入流中进行读取,当程序打印信息时,它们会将信息输出到输出流中。 通常,一个程序的输入输出流都是您的终端。也就是,您的键盘作为输入,显示器作为输出。 但是,我们也可以重定向这些流!
最简单的重定向是 < file 和 > file。这两个命令可以将程序的输入输出流分别重定向到文件:
echo hello > hello.txtcat hello.txtcat < hello.txtcat < hello.txt > hello2.txtcat hello2.txt
ls -l / | tail -n1curl --head --silent baidu.com | grep --ignore-case content-length | cut --delimiter=' ' -f2根用户root
拒绝访问(permission denied)
sudo
课后习题
# 第二题cd /tmpmkdir missingls | grep missing
# 第三题man touch
# 第四题touch ./missing/semester
# 第五题echo '#! /bin/sh' > ./missing/semesterecho 'curl --head --silent <https://baidu.com>' | tee -a ./missing/semestercat ./missing/semester
# 第六题./missing/semesterls -l ./missing# -rw-r--r-- 1 root root 62 1月 11 21:46 semester# 该文件无x 运行权限
# 第七题man chmod
# 第八题chmod +x ./missing/semester
# 第九题./semester | grep Date > ./last-modified.txtcat last-modified.txt
shell
Features
They allow you to run programs, give them input, and inspect their output in a semi-structured way
Usage
date # 显示时间echo hello # 输入参数‘hello’并输出echo "hello world"
echo $PATH # 输出环境变量¥PATH# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/binWhen we execute the echo command, the shell realizes that it needs to run the echo program; it will then search for that program in the directories separated by : in the $PATH, performing a name-based search for the program. When the program is found, it is executed. To determine exactly which program a given program name refers to, you can use the which command. We can also bypass the $PATH by directly specifying the path to the program we want to run.
Navigating in the shell
The paths in the shell are a set of directories separated by / on Linux and macOS, and by \\ on Windows. / is the root directory.
pwd # 获取当前路径cd /home # 根目录下的home文件夹cd ./home # 当前目录下的home文件夹cd .. # 上级目录Generally speaking, when we run a program, if we do not specify a path, the program will be executed from the current directory.
ls # 查看目录文件ls --helpls -l /home# drwxr-xr-x 4 root root 4096 11月 30 21:01 dataFirst, the line’s first character d indicates that data is a directory. Then the next nine characters, three characters at a time, form a group. (rwx). They represent the permissions that the file owner (root), user group (root), and everyone else have. A - indicates that the corresponding permission is not granted.
From the above information, only the file owner can modify (w), the data folder (for example, adding or removing files within the folder).
To enter a folder, a user needs the folder and its parent folders’ “execute” permission (represented by x). To list its contents, the user must have read permission (r) on that folder. For files, the meaning of the permissions is similar. Note that the programs in the last group of the /bin directory have x permission for everyone, meaning anyone can execute these programs.
mv test ./data/test.txtcp test.c test01.cmkdir testman lsRedirection of input/output in Programs
In the shell, a program has two main “streams”: its input stream and its output stream. When a program tries to read information, it reads from the input stream; when a program prints information, it outputs to the output stream. Typically, a program’s input/output streams are your terminal. That is, your keyboard as input and the display as output. However, we can also redirect these streams!
The simplest redirection is < file and > file. These two forms can redirect the program’s input and output streams respectively to a file:
echo hello > hello.txtcat hello.txtcat < hello.txtcat < hello.txt > hello2.txtcat hello2.txt
ls -l / | tail -n1curl --head --silent baidu.com | grep --ignore-case content-length | cut --delimiter=' ' -f2Root user
Permission denied
sudo
Exercises
# 第二题cd /tmpmkdir missingls | grep missing
# 第三题man touch
# 第四题touch ./missing/semester
# 第五题echo '#! /bin/sh' > ./missing/semesterecho 'curl --head --silent <https://baidu.com>' | tee -a ./missing/semestercat ./missing/semester
# 第六题./missing/semesterls -l ./missing# -rw-r--r-- 1 root root 62 1月 11 21:46 semester# 该文件无x 运行权限
# 第七题man chmod
# 第八题chmod +x ./missing/semester
# 第九题./semester | grep Date > ./last-modified.txtcat last-modified.txt
shell
特徴
それらは、プログラムを実行し、入力を与え、出力を半構造化された方法で確認できるようにします。
使い方
date # 显示时间echo hello # 输入参数‘hello’并输出echo "hello world"
echo $PATH # 输出环境变量¥PATH# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin私たちが echo コマンドを実行すると、シェルは echo というプログラムを実行する必要があると理解し、次いで $PATH によって「:」で区切られた一連のディレクトリの中から名前でそのプログラムを検索します。プログラムが見つかると、それを実行します。どのプログラム名が具体的にどのプログラムを指しているかを確認するには which コマンドを使用します。私たちは $PATH を回避し、実行したいプログラムのパスを直接指定して実行することもできます。
シェルでのナビゲーション
シェルでのパスは、区切られたディレクトリの集合です。Linux と macOS では / で区切りますが、Windows では \\ です。/ はルートディレクトリです。
pwd # 現在のディレクトリを取得cd /home # ルートディレクトリ直下の home ディレクトリへcd ./home # 現在のディレクトリ内の home ディレクトリへcd .. # 一つ上のディレクトリ一般的には、プログラムを実行する際、パスを指定しなければ、そのプログラムは現在のディレクトリで実行されます。
ls # ディレクトリ内のファイルを表示ls --helpls -l /home# drwxr-xr-x 4 root root 4096 11月 30 21:01 dataまず、この行の最初の文字 d は、data がディレクトリであることを示します。続く9文字は、それぞれ3文字ずつグループを形成します(rwx)。それぞれが、ファイルの所有者(root)、グループ(root)およびその他の人 が持つ権限を表します。- は、そのユーザーが該当する権限を持っていないことを意味します。
上記の情報から、ファイルの所有者だけが変更できる(w)、data ディレクトリ(例:ディレクトリ内のファイルを追加・削除すること)です。
あるディレクトリに入るには、そのディレクトリとその親ディレクトリの「検索」権限(実行権限:x)が必要です。含まれる内容を一覧表示するには、そのディレクトリに対して読み取り権限(r)が必要です。ファイルについても、権限の意味は同様です。注意として、/bin ディレクトリ内のプログラムは、最後のグループにあるものとして、すべてのユーザーのグループにも x 権限が含まれており、つまり誰でもこれらのプログラムを実行できるということです。
mv test ./data/test.txtcp test.c test01.cmkdir testman lsプロセスにおける入出力リダイレクト
シェルでは、プログラムには2つの主な「ストリーム」すなわち入力ストリームと出力ストリームがあります。プログラムが情報を読み取ろうとする場合、入力ストリームから読み取り、情報を表示する場合は出力ストリームへ出力します。通常、プログラムの入出力ストリームは端末です。つまり、キーボードを入力、ディスプレイを出力として使います。しかし、これらのストリームをリダイレクトすることもできます!
最も簡単なリダイレクトは < file と > file です。これらのリダイレクトは、プログラムの入力ストリームと出力ストリームを、それぞれファイルへリダイレクトします:
echo hello > hello.txtcat hello.txtcat < hello.txtcat < hello.txt > hello2.txtcat hello2.txt
ls -l / | tail -n1curl --head --silent baidu.com | grep --ignore-case content-length | cut --delimiter=' ' -f2ルートユーザー root
アクセスが拒否されました(permission denied)
sudo
演習問題
# 第二题cd /tmpmkdir missingls | grep missing
# 第三题man touch
# 第四题touch ./missing/semester
# 第五题echo '#! /bin/sh' > ./missing/semesterecho 'curl --head --silent <https://baidu.com>' | tee -a ./missing/semestercat ./missing/semester
# 第六题./missing/semesterls -l ./missing# -rw-r--r-- 1 root root 62 1月 11 21:46 semester# 该文件无x 运行权限
# 第七题man chmod
# 第八题chmod +x ./missing/semester
# 第九题./semester | grep Date > ./last-modified.txtcat last-modified.txt部分信息可能已经过时









