You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Although Windows has advantages over Linux in terms of graphical interfaces, due to Windows' limited support for terminal scripts, I still prefer Linux. Using terminal may, at first, seem counterintuitive, but with more familiarity, it can become a timer saver over graphical interfaces.
6
6
7
-
2、在后台运行命令在退出终端后也全部退出了。
7
+
**Instead of demonstrating the usage of Linux terminal, the post focuses on the basic yet confusing gotchas:**
8
8
9
-
3、单引号和双引号表示字符串的区别。
9
+
1. The difference between standard input and variables.
10
10
11
-
4、有的命令和`sudo`一起用就 command not found。
11
+
2. Why processes running on the background exit upon terminal termination?
### 1. The difference between standard input and variables
18
+
19
+
The difference between standard input and variables boils down to the question of when to use pipe `|` and redirecting `>`, `<` vs. when to use variables `$`.
20
+
21
+
For example, if a shell script to automate ethernet connection locates in my home directory:
18
22
19
23
```shell
20
24
$ where connect.sh
21
25
/home/fdl/bin/connect.sh
22
26
```
23
27
24
-
如果我想删除这个脚本,而且想少敲几次键盘,应该怎么操作呢?我曾经这样尝试过:
28
+
To remove the script with minimal effort, I tried:
25
29
26
30
```shell
27
31
$ where connect.sh | rm
28
32
```
29
33
30
-
实际上,这样操作是错误的,正确的做法应该是这样的:
34
+
However, the command above is incorrect. The proper way is:
31
35
32
36
```shell
33
37
$ rm $(where connect.sh)
34
38
```
35
39
36
-
前者试图将`where`的结果连接到`rm`的标准输入,后者试图将结果作为命令行参数传入。
40
+
The former attempts to pipe the output from `where` into the standard input of `rm`, whereas the latter passes it in as an variable.
**Typically standard inputs appear in programming languages as `scanf` and `readline`; Variables refer to the literal arguments, `args`, the `main` program consumes.**
As mentioned in「Linux file descriptor」, Pipe and redirecting aim to use data as standard input. By contrast, `$(cmd)` reads the output from `cmd` as variables.
Revisiting the previous example, the source code of `rm` will certainly prefer receiving variable arguments over standard input to remove a file. In comparison, the `cat` command accepts both standard input and variables.
**If a command can clog the terminal, then it accepts standard input and vice versa.** For example, running "cat" without arguments will suspend (intentionally clog) the terminal to wait for user input and print back the same content.
56
60
57
-
### 二、后台运行程序
61
+
### 2. Why processes running on the background exit upon terminal termination?
58
62
59
-
比如说你远程登录到服务器上,运行一个 Django web 程序:
63
+
For example, we want to spin up a Django web server on a remote server:
With the server up and running, we can test it through the server's IP address. However, at the same time, the terminal will suspend, not responding to any input, until it detects `Ctrl-C`/`Ctrl-/` and kills the Python process.
With a tailing `&`, the command won't clog the terminal and will continue to respond to incoming commands. However, the website becomes unavailable once you log out of the server.
69
73
70
-
如果你想在退出服务器之后仍然能够访问 web 服务,应该这样写命令 `(cmd &)`:
74
+
To keep the web service available after logging out of the server, consider using this command `(cmd &)`:
Every terminal is a shell process, and it forks itself to provide child processes to execute commands. Usually, the shell process clogs while waiting for the child processes to exit, not accepting new commands. With a tailing `&`, the shell process allows issuing new commands. However, when the shell process exits upon the termination of the terminal window, all its child processes will exit.
Nevertheless, commands like `(cmd &)` move the process under `systemd`, an OS guarded process that prevents the process from exiting when we close the current terminal.
84
88
85
-
类似的,还有一种后台运行常用的做法是这样:
89
+
An alternative approach to background execution is:
86
90
87
91
```shell
88
92
$ nohub some_cmd &
89
93
```
90
94
91
-
`nohub`命令也是类似的原理,不过通过我的测试,还是`(cmd &)`这种形式更加稳定。
95
+
`nohub` functions similarly, but with extensive testing, `(cmd &)` appears to be more stable.
Shells with different flavors behave differently, but with one invariant: **for `$`,`(`,`)`, single-quote won't trigger evaluation, but double-quote will.**
**When prefixing `sudo`, we tell the OS that the root user is executing the command, so the OS will search the environment variables of the root user** where the `connect.sh` script doesn't exist.
125
129
126
-
解决方法是使用脚本文件的路径,而不是仅仅通过脚本名称:
130
+
The solution is to locate the script with a path instead of a name:
0 commit comments