Skip to content

Commit e4ca086

Browse files
committed
docs: edit grammar
1 parent 9cf246e commit e4ca086

4 files changed

Lines changed: 119 additions & 3 deletions

File tree

docs/grammar.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Bash 的基本语法
22

3+
## 分号
4+
5+
分号`;`是命令的结束符,使得一行可以放置多个命令,上一个命令执行结束后,再执行第二个命令。
6+
7+
```bash
8+
$ clear; ls
9+
```
10+
311
## 命令的分组和合并
412

513
Shell 允许将多个命令分组和合并执行。
@@ -32,7 +40,19 @@ $ mkdir foo || mkdir bar
3240

3341
## if 结构
3442

35-
`if`结构用于条件判断。
43+
`if`结构用于条件判断,符合条件时,才会执行指定的命令。它的语法如下。
44+
45+
```bash
46+
if commands; then
47+
commands
48+
[elif commands; then
49+
commands...]
50+
[else
51+
commands]
52+
fi
53+
```
54+
55+
这个命令分成三个部分:`if``elif``else`。其中,后两个部分是可选的。`if`后面是判断的条件,如果不成立,并且存在`elif`部分,就会进行`elif`判断;如果还不成立,并且存在`else`部分,则会执行`else`代码块。
3656

3757
```bash
3858
if test $USER = "foo"; then
@@ -42,7 +62,52 @@ else
4262
fi
4363
```
4464

45-
`if test -e filename`判断文件是否存在。
65+
上面的例子中,`if test`判断环境变量`$USER`是否等于`foo`,如果等于就输出`Hello foo.`,否则输出其他内容。
66+
67+
`if`结构可以写在一行内。
68+
69+
```bash
70+
$ if true; then echo "It's true."; fi
71+
It's true.
72+
73+
$ if false; then echo "It's true."; fi
74+
```
75+
76+
上面的例子中,`true``false`是两个特殊命令,前者代表操作成功,后者代表操作失败。
77+
78+
`if`结构写成下面两种形式,也是可以的。
79+
80+
```bash
81+
# 形式一
82+
if [ -f .bash_profile ]
83+
then
84+
echo ".bash_profile 文件存在"
85+
else
86+
echo ".bash_profile 文件不存在"
87+
fi
88+
89+
# 形式二
90+
if [ -f .bash_profile ]
91+
then echo ".bash_profile 文件存在"
92+
else echo ".bash_profile 文件不存在"
93+
fi
94+
```
95+
96+
## test 命令
97+
98+
`if`结构往往与`test`命令一起使用,有两种形式。
99+
100+
```bash
101+
# 第一种形式
102+
test 表达式
103+
104+
# 第二种形式
105+
[ 表达式 ]
106+
```
107+
108+
注意,第二种形式之中,`[``]`与内部的表达式之间都必须有空格。
109+
110+
`if test -e filename`判断一个文件是否存在。
46111
47112
```bash
48113
if test -e /tmp/foo.txt ; then
@@ -58,6 +123,22 @@ if [ -e /tmp/foo.txt ] ; then
58123
fi
59124
```
60125
126+
`test`命令的参数如下。
127+
128+
- `-d file`:如果`file`为目录,返回`true`
129+
- `-e file`:如果`file`文件存在,返回`true`
130+
- `-f file`:如果`file`文件存在,并且为一个常规文件,返回`true`
131+
- `-L file`:如果`file`是一个符号链接,返回`true`
132+
- `-r file`:如果`file`是一个文件,且用户有读权限,返回`true`
133+
- `-w file`:如果`file`是一个文件,且用户有写权限,返回`true`
134+
- `-x file`:如果`file`是一个文件,且用户有执行权限,返回`true`
135+
- `file1 -nt file2`:如果`file1``file2`更新(根据修改时间),返回`true`
136+
- `file1 -ot file2`:如果`file1``file2`更旧(根据修改时间),返回`true`
137+
- `-z string`:如果`string`是一个空字符串,返回`true`
138+
- `-n string`:如果`string`是一个非空字符串,返回`true`
139+
- `string1 = string2`:如果`string1``string2`相等,返回`true`
140+
- `string1 != string2`:如果`string1``string2`不相等,返回`true`
141+
61142
## for 循环
62143
63144
foo 循环用于命令的重复执行。

docs/script.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,26 @@ $ exit
208208
`exit`命令后面可以跟参数,该参数就是退出状态。
209209

210210
```bash
211+
# 退出值为0(成功)
212+
$ exit 0
213+
214+
# 退出值为1(失败)
211215
$ exit 1
212216
```
213217

214218
退出时,脚本会返回一个退出值。脚本的退出值,`0`表示正常,`1`表示发生错误,`2`表示用法不对,`126`表示不是可执行脚本,`127`表示命令没有发现。如果脚本被信号`N`终止,则退出值为`128 + N`。简单来说,只要退出值非0,就认为执行出错。
215219

220+
下面是一个例子。
221+
222+
```bash
223+
if [ $(id -u) != "0" ]; then
224+
echo "根用户才能执行当前脚本"
225+
exit 1
226+
fi
227+
```
228+
229+
上面的例子中,`id -u`命令返回用户的 ID,一旦用户的 ID 不等于`0`(根用户的 ID),脚本就会退出,并且退出码为`1`,表示运行失败。
230+
216231
上一条命令的退出值,可以用系统变量`$?`查询。使用这个命令,可以知道上一条命令是否执行成功。
217232

218233
`exit``return`命令的差别是,`return`命令是函数的退出,并返回一个值给调用者,脚本依然执行。`exit`是整个脚本的退出,如果在函数之中调用`exit`,则退出函数,并终止脚本执行。

docs/set.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ bar
9595
set -o xtrace
9696
```
9797

98+
脚本当中如果要关闭命令输出,可以使用`set +x`
99+
100+
```bash
101+
#!/bin/bash
102+
103+
number=1
104+
105+
set -x
106+
if [ $number = "1" ]; then
107+
echo "Number equals 1"
108+
else
109+
echo "Number does not equal 1"
110+
fi
111+
set +x
112+
```
113+
114+
上面的例子中,只对特定的代码段打开命令输出。
115+
98116
## Bash 的错误处理
99117

100118
如果脚本里面有运行失败的命令(返回值非0),Bash 默认会继续执行后面的命令。

docs/variable.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ $ printenv
3030
- `TERM`:终端类型名,即终端仿真器所用的协议。
3131
- `USER`:当前用户的用户名。
3232

33-
Bash 还提供一些特殊变量。
33+
这些环境变量很少发生变化,一般可以视为常量。由于它们的变量名全部都是大写,所以传统上,如果用户要自己定义一个常量,也会使用全部大写的变量名。
34+
35+
Bash 还提供一些特殊的环境变量。
3436

3537
- `$` 进程的ID
3638
- `?` 上一个命令的退出码

0 commit comments

Comments
 (0)