-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path20.html
More file actions
278 lines (278 loc) · 25.9 KB
/
Copy path20.html
File metadata and controls
278 lines (278 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="CodeClub - Shell 流程控制" />
<meta name="author" content="Jackson" />
<title>Shell 流程控制 - CodeClub</title>
<link rel="icon" type="image/x-icon" href="../../img/logo.png" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<link rel="stylesheet" href="../../css/main.css" />
<link rel="stylesheet" href="../../css/dark-mode.css" />
<link rel="stylesheet" href="../../css/mobile-optimizations.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/12.0.0/marked.min.js"></script>
<style>
.tutorial-page {
max-width: 1200px;
margin: 70px auto 40px;
padding: 0 20px;
display: flex;
gap: 20px;
}
.tutorial-sidebar {
width: 240px;
flex-shrink: 0;
position: sticky;
top: 70px;
max-height: calc(100vh - 90px);
overflow-y: auto;
background: var(--bg-secondary, #fff);
border-radius: 8px;
padding: 16px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
.tutorial-sidebar h3 {
font-size: 15px;
margin: 0 0 12px;
color: var(--text-primary, #1a1a2e);
padding-bottom: 8px;
border-bottom: 2px solid var(--primary, #3b82f6);
}
.tutorial-sidebar a {
display: block;
padding: 5px 8px;
color: var(--text-secondary, #666);
text-decoration: none;
font-size: 13px;
border-radius: 4px;
line-height: 1.4;
}
.tutorial-sidebar a:hover {
background: var(--primary, #3b82f6);
color: #fff;
}
.tutorial-sidebar a.active {
background: var(--primary, #3b82f6);
color: #fff;
font-weight: 600;
}
.tutorial-main {
flex: 1;
min-width: 0;
}
.tutorial-article {
background: var(--bg-secondary, #fff);
border-radius: 8px;
padding: 32px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
.tutorial-article h1 {
font-size: 26px;
margin-bottom: 16px;
color: var(--text-primary, #1a1a2e);
border-bottom: 2px solid var(--primary, #3b82f6);
padding-bottom: 12px;
}
.markdown-body {
font-size: 15px;
line-height: 1.8;
color: var(--text-secondary, #333);
}
.markdown-body h1 {
font-size: 24px;
border-bottom: 1px solid var(--border, #e5e7eb);
padding-bottom: 8px;
}
.markdown-body h2 {
font-size: 20px;
border-bottom: 1px solid var(--border, #e5e7eb);
padding-bottom: 6px;
}
.markdown-body h3 {
font-size: 17px;
}
.markdown-body pre {
background: #1e1e2e;
color: #cdd6f4;
padding: 16px;
border-radius: 8px;
overflow-x: auto;
}
.markdown-body code {
font-family: 'Fira Code', 'Cascadia Code', monospace;
font-size: 14px;
}
.markdown-body p code {
background: #f1f5f9;
padding: 2px 6px;
border-radius: 4px;
color: #e83e8c;
}
.markdown-body blockquote {
border-left: 4px solid var(--primary, #3b82f6);
padding: 12px 20px;
margin: 16px 0;
background: #f8fafc;
border-radius: 0 8px 8px 0;
}
.markdown-body img {
max-width: 100%;
border-radius: 8px;
}
.markdown-body table {
border-collapse: collapse;
width: 100%;
margin: 16px 0;
}
.markdown-body table th,
.markdown-body table td {
border: 1px solid var(--border, #e5e7eb);
padding: 8px 12px;
text-align: left;
}
.markdown-body table th {
background: var(--bg-primary, #f8fafc);
font-weight: 600;
}
.markdown-body table tr:nth-child(2n) {
background: var(--bg-primary, #f8fafc);
}
.markdown-body ul,
.markdown-body ol {
padding-left: 24px;
margin: 12px 0;
}
.markdown-body li {
margin: 4px 0;
}
.markdown-body hr {
border: none;
border-top: 1px solid var(--border, #e5e7eb);
margin: 24px 0;
}
.markdown-body a {
color: var(--primary, #3b82f6);
text-decoration: none;
}
.markdown-body a:hover {
text-decoration: underline;
}
.page-navigation {
display: flex;
justify-content: space-between;
margin-top: 24px;
padding-top: 16px;
border-top: 1px solid var(--border, #e5e7eb);
}
.page-nav-link {
color: var(--primary, #3b82f6);
text-decoration: none;
font-weight: 500;
font-size: 14px;
}
.page-nav-link:hover {
text-decoration: underline;
}
.source-link {
margin-top: 16px;
font-size: 13px;
color: var(--text-secondary, #999);
}
.source-link a {
color: var(--primary, #3b82f6);
}
@media (max-width: 768px) {
.tutorial-page {
flex-direction: column;
}
.tutorial-sidebar {
width: 100%;
position: static;
max-height: 200px;
}
}
</style>
</head>
<body>
<nav class="navbar">
<div class="navbar-container">
<a class="navbar-brand" href="../../index.html">
<img src="../../img/logo.png" alt="CodeClub" width="32" height="32" />
<span>CodeClub</span>
</a>
<button class="mobile-menu-toggle" aria-label="切换导航菜单"><span></span><span></span><span></span></button>
<ul class="navbar-nav">
<li><a class="nav-link" href="../../index.html">首页</a></li>
<li><a class="nav-link" href="../../nav.html">网址导航</a></li>
<li><a class="nav-link" href="../../blog/">博客</a></li>
<li><a class="nav-link" href="../../wiki/index.html" target="_blank">WiKi</a></li>
<li><a class="nav-link active" href="../index.html">教程</a></li>
<li><a class="nav-link" href="../../book.html">图书</a></li>
<li><a class="nav-link" href="../../contact.html">联系我</a></li>
<li><a class="nav-link" href="../../about.html">关于我</a></li>
</ul>
</div>
</nav>
<div class="tutorial-page">
<div class="tutorial-sidebar">
<h3>📖 Shell</h3>
<a href="0.html">Linux</a>
<a href="1.html">Linux 简介</a>
<a href="2.html">Linux 安装</a>
<a href="3.html">Linux 云服务器</a>
<a href="4.html">WSL 安装 Linux</a>
<a href="5.html">Linux 系统启动过程</a>
<a href="6.html">Linux 系统目录结构</a>
<a href="7.html">Linux 忘记密码解决方法</a>
<a href="8.html">Linux 远程登录</a>
<a href="9.html">Linux 文件基本属性</a>
<a href="10.html">Linux 文件与目录管理</a>
<a href="11.html">Linux 用户和用户组管理</a>
<a href="12.html">Linux 磁盘管理</a>
<a href="13.html">Linux vi/vim</a>
<a href="14.html">linux yum 命令</a>
<a href="15.html">Linux apt 命令</a>
<a href="16.html">Shell 教程</a>
<a href="17.html">Shell 变量</a>
<a href="18.html">Shell echo 命令</a>
<a href="19.html">Shell test 命令</a>
<a href="20.html" class="active">Shell 流程控制</a>
<a href="21.html">Shell 函数</a>
<a href="22.html">Linux 命令大全</a>
<a href="23.html">Nginx 安装配置</a>
<a href="24.html">MySQL 安装配置</a>
</div>
<div class="tutorial-main">
<div class="tutorial-article">
<div class="markdown-body" id="md-content"></div>
<div class="page-navigation">
<a href="19.html" class="page-nav-link">← Shell test 命令</a>
<a href="21.html" class="page-nav-link">Shell 函数 →</a>
</div>
<div class="source-link">原文来源:<a href="https://www.runoob.com" target="_blank">菜鸟教程</a></div>
</div>
</div>
</div>
<footer class="footer">
<div class="footer-content"><p>© 2024 CodeClub. All rights reserved.</p></div>
</footer>
<script src="../../js/dark-mode.js"></script>
<script src="../../js/mobile-menu.js"></script>
<script>
const md = `# Shell 流程控制\n\n\n\n和 Java、PHP 等语言不一样,sh 的流程控制不可为空,如(以下为 PHP 流程控制写法):\n\n\n\n\n## 实例\n\n\nif (isset($_GET["q"])) {\n\n search(q);\n\n}\n\nelse {\n\n // 不做任何事情\n\n}\n\n\n\n\n在 sh/bash 里可不能这么写,如果 else 分支没有语句执行,就不要写这个 else。\n\n\n\n---\n\n\n## \nif else\n\n\n\n### \nif\n\nif 语句语法格式:\n\n\n\n\`\`\`\n\nif condition\nthen\n command1 \n command2\n ...\n commandN \nfi\n\n\`\`\`\n\n\n写成一行(适用于终端命令提示符):\n\n\n\n\`\`\`\n\nif [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi\n\n\`\`\`\n\n\n末尾的 fi 就是 if 倒过来拼写,后面还会遇到类似的。\n\n\n\n\n\n### if else\n\n\nif else 语法格式:\n\n\n\n\`\`\`\n\nif condition\nthen\n command1 \n command2\n ...\n commandN\nelse\n command\nfi\n\n\`\`\`\n\n\n\n### \nif else-if else\n\n\nif else-if else 语法格式:\n\n\n\n\`\`\`\n\nif condition1\nthen\n command1\nelif condition2 \nthen \n command2\nelse\n commandN\nfi\n\n\`\`\`\n\nif else 的 [...] 判断语句中大于使用 -gt,小于使用 -lt。\n\n\n\n\`\`\`\nif [ "$a" -gt "$b" ]; then\n ...\nfi\n\`\`\`\n\n如果使用 ((...)) 作为判断语句,大于和小于可以直接使用 > 和 。\n\n\n\n\`\`\`\nif (( a > b )); then\n ...\nfi\n\`\`\`\n\n以下实例判断两个变量是否相等:\n\n\n\n\n## 实例\n\na=10\n\nb=20\n\nif [ $a == $b ]\n\nthen\n\n echo "a 等于 b"\n\nelif [ $a -gt $b ]\n\nthen\n\n echo "a 大于 b"\n\nelif [ $a -lt $b ]\n\nthen\n\n echo "a 小于 b"\n\nelse\n\n echo "没有符合的条件"\n\nfi\n\n\n输出结果:\n\n\n\n\`\`\`\n\na 小于 b\n\n\`\`\`\n\n\n使用 ((...)) 作为判断语句:\n\n\n\n## 实例\n\na=10\n\nb=20\n\nif (( $a == $b ))\n\nthen\n\n echo "a 等于 b"\n\nelif (( $a > $b ))\n\nthen\n\n echo "a 大于 b"\n\nelif (( $a $b ))\n\nthen\n\n echo "a 小于 b"\n\nelse\n\n echo "没有符合的条件"\n\nfi\n\n\n输出结果:\n\n\n\n\`\`\`\n\na 小于 b\n\n\`\`\`\n\nif else 语句经常与 test 命令结合使用,如下所示:\n\n\n\n\n## 实例\n\nnum1=$[2*3]\n\nnum2=$[1+5]\n\nif test $[num1] -eq $[num2]\n\nthen\n\n echo \'两个数字相等!\'\n\nelse\n\n echo \'两个数字不相等!\'\n\nfi\n\n\n\n输出结果:\n\n\n\n\`\`\`\n\n两个数字相等!\n\n\`\`\`\n\n\n\n\n\n---\n\n\n## \nfor 循环\n\n\n与其他编程语言类似,Shell支持for循环。\n\n\n\nfor循环一般格式为:\n\n\n\n\`\`\`\n\nfor var in item1 item2 ... itemN\ndo\n command1\n command2\n ...\n commandN\ndone\n\n\`\`\`\n\n\n写成一行:\n\n\n\n\`\`\`\n\nfor var in item1 item2 ... itemN; do command1; command2… done;\n\n\`\`\`\n\n当变量值在列表里,for 循环即执行一次所有命令,使用变量名获取列表中的当前取值。命令可为任何有效的 shell 命令和语句。in 列表可以包含替换、字符串和文件名。\n\n\nin列表是可选的,如果不用它,for循环使用命令行的位置参数。\n\n\n例如,顺序输出当前列表中的数字:\n\n\n\n\n## 实例\n\nfor loop in 1 2 3 4 5\n\ndo\n\n echo "The value is: $loop"\n\ndone\n\n\n\n\n输出结果:\n\n\n\n\`\`\`\n\nThe value is: 1\nThe value is: 2\nThe value is: 3\nThe value is: 4\nThe value is: 5\n\n\`\`\`\n\n顺序输出字符串中的字符:\n\n\n\n\`\`\`\n\n#!/bin/bash\n\nfor str in This is a string\ndo\n echo $str\ndone\n\n\`\`\`\n\n输出结果:\n\n\n\n\`\`\`\n\nThis\nis\na\nstring\n\n\`\`\`\n\n\n---\n\n## \nwhile 语句\n\nwhile 循环用于不断执行一系列命令,也用于从输入文件中读取数据。其语法格式为:\n\n\n\n\`\`\`\n\nwhile condition\ndo\n command\ndone\n\n\`\`\`\n\n\n以下是一个基本的 while 循环,测试条件是:如果 int 小于等于 5,那么条件返回真。int 从 1 开始,每次循环处理时,int 加 1。运行上述脚本,返回数字 1 到 5,然后终止。\n\n\n\n\n## 实例\n\n#!/bin/bash\n\nint=1\n\nwhile(( $int=5 ))\n\ndo\n\n echo $int\n\n let "int++"\n\ndone\n\n\n\n\n运行脚本,输出:\n\n\n\n\`\`\`\n\n1\n2\n3\n4\n5\n\n\`\`\`\n\n以上实例使用了 Bash let 命令,它用于执行一个或多个表达式,变量计算中不需要加上 $ 来表示变量,具体可查阅:[Bash let 命令](https://www.runoob.com//www.runoob.com/linux/linux-comm-let.html)\n\n。\n\nwhile循环可用于读取键盘信息。下面的例子中,输入信息被设置为变量FILM,按结束循环。\n\n\n\n\n## 实例\n\necho \'按下 退出\'\n\necho -n \'输入你最喜欢的网站名: \'\n\nwhile read FILM\n\ndo\n\n echo "是的!$FILM 是一个好网站"\n\ndone\n\n\n\n\n运行脚本,输出类似下面:\n\n\n\n\`\`\`\n\n按下 退出\n输入你最喜欢的网站名:菜鸟教程\n是的!菜鸟教程 是一个好网站\n\n\`\`\`\n\n\n### \n无限循环\n\n\n无限循环语法格式:\n\n\n\n\`\`\`\n\nwhile :\ndo\n command\ndone\n\n\`\`\`\n\n\n或者\n\n\`\`\`\n\nwhile true\ndo\n command\ndone\n\`\`\`\n\n\n或者\n\n\`\`\`\n\nfor (( ; ; ))\n\n\`\`\`\n\n\n\n---\n\n\n## \nuntil 循环\n\nuntil 循环执行一系列命令直至条件为 true 时停止。\n\nuntil 循环与 while 循环在处理方式上刚好相反。\n\n\n一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用。\n\n\nuntil 语法格式:\n\n\n\n\`\`\`\n\nuntil condition\ndo\n command\ndone\n\n\`\`\`\n\ncondition 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环。\n\n\n以下实例我们使用 until 命令来输出 0 ~ 9 的数字:\n\n\n\n## 实例\n\n#!/bin/bash\n\na=0\n\nuntil [ ! $a -lt 10 ]\n\ndo\n\n echo $a\n\n a=\`expr $a + 1\`\n\ndone\n\n\n\n运行结果:\n\n\n输出结果为:\n\n\n\n\`\`\`\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n\`\`\`\n\n\n---\n\n## \ncase ... esac\n\n\n**case ... esac** 为多选择语句,与其他语言中的 switch ... case 语句类似,是一种多分支选择结构,每个 case 分支用右圆括号开始,用两个分号 ;; 表示 break,即执行结束,跳出整个 case ... esac 语句,esac(就是 case 反过来)作为结束标记。\n\n\n可以用 case 语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。\n\n\n**case ... esac** 语法格式如下:\n\n\n\n\`\`\`\n\ncase 值 in\n模式1)\n command1\n command2\n ...\n commandN\n ;;\n模式2)\n command1\n command2\n ...\n commandN\n ;;\nesac\n\n\`\`\`\n\ncase 工作方式如上所示,取值后面必须为单词 **in**,每一模式必须以右括号结束。取值可以为变量或常数,匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。\n\n\n取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。\n\n\n下面的脚本提示输入 1 到 4,与每一种模式进行匹配:\n\n\n\n\n## 实例\n\necho \'输入 1 到 4 之间的数字:\'\n\necho \'你输入的数字为:\'\n\nread aNum\n\ncase $aNum in\n\n 1) echo \'你选择了 1\'\n\n ;;\n\n 2) echo \'你选择了 2\'\n\n ;;\n\n 3) echo \'你选择了 3\'\n\n ;;\n\n 4) echo \'你选择了 4\'\n\n ;;\n\n *) echo \'你没有输入 1 到 4 之间的数字\'\n\n ;;\n\nesac\n\n\n\n\n输入不同的内容,会有不同的结果,例如:\n\n\n\n\`\`\`\n\n输入 1 到 4 之间的数字:\n你输入的数字为:\n3\n你选择了 3\n\n\`\`\`\n\n下面的脚本匹配字符串:\n\n\n\n## 实例\n\n#!/bin/sh\n\nsite="runoob"\n\ncase "$site" in\n\n "runoob") echo "菜鸟教程"\n\n ;;\n\n "google") echo "Google 搜索"\n\n ;;\n\n "taobao") echo "淘宝网"\n\n ;;\n\nesac\n\n\n输出结果为:\n\n\n\n\`\`\`\n菜鸟教程\n\`\`\`\n\n\n---\n\n## 跳出循环\n\n在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,Shell 使用两个命令来实现该功能:**break** 和 **continue**。\n\n\n\n### \nbreak 命令\n\n\n\nbreak 命令允许跳出所有循环(终止执行后面的所有循环)。\n\n\n\n下面的例子中,脚本进入死循环直至用户输入数字大于5。要跳出这个循环,返回到shell提示符下,需要使用break命令。\n\n\n\n\n## 实例\n\n#!/bin/bash\n\nwhile :\n\ndo\n\n echo -n "输入 1 到 5 之间的数字:"\n\n read aNum\n\n case $aNum in\n\n 1|2|3|4|5) echo "你输入的数字为 $aNum!"\n\n ;;\n\n *) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"\n\n break\n\n ;;\n\n esac\n\ndone\n\n\n\n执行以上代码,输出结果为:\n\n\n\n\`\`\`\n\n输入 1 到 5 之间的数字:3\n你输入的数字为 3!\n输入 1 到 5 之间的数字:7\n你输入的数字不是 1 到 5 之间的! 游戏结束\n\n\`\`\`\n\n\n### \ncontinue\n\n\n\ncontinue 命令与 break 命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。\n\n\n\n对上面的例子进行修改:\n\n\n\n\n## 实例\n\n#!/bin/bash\n\nwhile :\n\ndo\n\n echo -n "输入 1 到 5 之间的数字: "\n\n read aNum\n\n case $aNum in\n\n 1|2|3|4|5) echo "你输入的数字为 $aNum!"\n\n ;;\n\n *) echo "你输入的数字不是 1 到 5 之间的!"\n\n continue\n\n echo "游戏结束"\n\n ;;\n\n esac\n\ndone\n\n\n\n\n运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句\n**\necho "游戏结束"**\n永远不会被执行。\n\n \n \n \n \n \n\n \n \n [ Shell 函数](http://www.runoob.com/linux/linux-shell-func.html)\n \n\n \n \n \n \n \n\n\n\n\n\n \n## \n \n \n \n 4 篇笔记\n 写笔记\n\n\n\n\n\n \n\n #0 情空明月\n\n moo***v@gmail.com\n\n 344shell 中的 for 循环不仅可以用文章所述的方法。\n\n\n\n对于习惯其他语言 for 循环的朋友来说可能有点别扭。\n\n\n\n\n\`\`\`\nfor((assignment;condition:next));do\n command_1;\n command_2;\n commond_..;\ndone;\n\`\`\`\n\n如上所示,这里的 for 循环与 C 中的相似,但并不完全相同。\n\n\n\n通常情况下 shell 变量调用需要加 $,但是 for 的 (()) 中不需要,下面来看一个例子:\n\n\n\n\n\`\`\`\n#!/bin/bash\nfor((i=1;i[情空明月](https://www.runoob.comjavascript:;) 情空明月\n\n moo***v@gmail.com\n\n8年前 (2018-02-27)\n#0 This_Is_Y\n\n Thi***s_Y@qq.com\n\n [ 参考地址](https://blog.csdn.net/liuqinglong_along/article/details/52191382)\n\n 113从 ubuntu 6.10 开始,ubuntu 就将先前默认的 **bash shell** 更换成了**dash shell**,其表现为 **/bin/sh** 链接倒了 **/bin/dash** 而不是传统的 **/bin/bash**。\n\n\n\n可以通过 ls -l /bin/*sh 命令看到:\n\n\n\n\n\n\n\n所以在使用 sh 命令执行脚本的时候实际使用的是 dash,而 dash 不支持这种 C 语言格式的 for 循环写法。\n\n\n\n**解决方法:**使用 bash 代替 **sh** 运行脚本:\n\n\n\n\n\`\`\`\nbash test.sh\n\`\`\`\n[This_Is_Y](https://www.runoob.com/note/60772) This_Is_Y\n\n Thi***s_Y@qq.com\n\n [ 参考地址](https://blog.csdn.net/liuqinglong_along/article/details/52191382)\n\n5年前 (2021-01-18)\n#0 风鹏正举\n\n cle***@126.com\n\n 17如果 for 循环是按 1 递增的,可以利用 seq 命令来生成数字系列,参考写法如下:\n\n\n\n\n\`\`\`\n#!/bin/bash\nmax=5\n\nfor index in \`seq $max\`\ndo\n echo $index\ndone\n\`\`\`\n\n执行结果:\n\n\n\n\n\`\`\`\n1\n2\n3\n4\n5\n\`\`\`\n\n在某些情况下比使用 while 更加方便。\n\n[风鹏正举](https://www.runoob.comjavascript:;) 风鹏正举\n\n cle***@126.com\n\n4年前 (2021-11-04)\n#0 開色\n\n wrd***main@163.com\n\n 31test 命令和 [ 底层是一个命令, [ 是一个命令,会自动检测右中括号 ],通过 which [ 查看 Linux 中命令的位置,test 不会自动检测右中括号 ] 。\n\n\n\n[ 底层使用 c/c++ 编写的可以看它的源码:[https://github.com/freebsd/freebsd-src/blob/master/bin/test/test.c](https://github.com/freebsd/freebsd-src/blob/master/bin/test/test.c)。\n\n\n\n[[ 是 bash shell 环境自己实现的判断方式,比 [ 更安全,例如:\n\n\n\n1. [ 不能识别 **$var** 变量值\n\n\n\n\n\`\`\`\n$ var=\'\'\n$ [ $var = \'\' ] && echo True\n-bash: [: =: unary operator expected\n$ [ "$var" = \'\' ] && echo True\nTrue\n$ [[ $var = \'\' ]] && echo True\nTrue\n\`\`\`\n\n2. [ 会把大于号、小于号识别为重定向符号,把 **a** 当成文件输入输出\n\n\n\n\n\`\`\`\n$ var=\n$ [ "$var" [ 相比于 [[ 的一个优点,应该是 bash shell 环境自己实现的 [[ 在其他 shell 环境不能兼容,所以 [ 有更好的可移植性。\n\n\n\n详情可以看:\n\n\n\n[http://mywiki.wooledge.org/BashGuide/Practices#Bash_Tests](http://mywiki.wooledge.org/BashGuide/Practices#Bash_Tests)\n\n\n[https://zhuanlan.zhihu.com/p/136591210](https://zhuanlan.zhihu.com/p/136591210)\n\n[開色](https://www.runoob.comjavascript:;) 開色\n\n wrd***main@163.com\n\n4年前 (2021-11-10)\n \n\n \n \n\n \n \n \n### 点我分享笔记\n\n \n \n \n \n \n \n [取消](https://www.runoob.comjavascript:;)\n\n\n \n \n \n \n \n \n\n\n 分享笔记\n \n \n \n \n \n \n\n 昵称昵称 (必填)\n 邮箱邮箱 (必填)\n 引用地址引用地址\n \n\n \n \n \n \n\n \n \n\n\n\n\n\n\n\n\n\n \n\n \n\n\n\n\n\n\n \n \n \n \n \n \n\n\n \n \n \n \n \n \n \n \n \n\n\n\n\n\n 在线实例\n\n\n ·[HTML 实例](https://www.runoob.com/html/html-examples.html)\n\n\n ·[CSS 实例](https://www.runoob.com/css/css-examples.html)\n\n\n ·[JavaScript 实例](https://www.runoob.com/js/js-examples.html)\n\n\n ·[Ajax 实例](https://www.runoob.com/ajx/ajax-examples.html)\n\n\n ·[jQuery 实例](https://www.runoob.com/jquery/jquery-examples.html)\n\n\n ·[XML 实例](https://www.runoob.com/xml/xml-examples.html)\n\n\n ·[Java 实例](https://www.runoob.com/java/java-examples.html)\n\n\n\n\n\n 字符集&工具\n\n\n · [HTML 字符集设置](https://www.runoob.com/charsets/html-charsets.html)\n\n\n · [HTML ASCII 字符集](https://www.runoob.com/tags/html-ascii.html)\n\n\n · [JS 混淆/加密](https://www.jyshare.com/front-end/6939/)\n\n\n · [PNG/JPEG 图片压缩](https://www.jyshare.com/front-end/6232/)\n\n\n · [HTML 拾色器](https://www.runoob.com/tags/html-colorpicker.html)\n\n\n · [JSON 格式化工具](https://www.jyshare.com/front-end/53)\n\n\n · [随机数生成器](https://www.jyshare.com/front-end/6680/)\n\n\n\n\n 最新更新\n\n\n ·\n [\n OpenCode 自定义... ](http://www.runoob.com/opencode/opencode-custom-tools.html)\n\n\n ·\n [\n OpenCode Skills ](http://www.runoob.com/opencode/opencode-skills.html)\n\n\n ·\n [\n OpenCode MCP 服... ](http://www.runoob.com/opencode/opencode-mcp.html)\n\n\n ·\n [\n OpenCode LSP 服... ](http://www.runoob.com/opencode/opencode-lsp.html)\n\n\n ·\n [\n OpenCode 权限配置 ](http://www.runoob.com/opencode/opencode-permissions.html)\n\n\n ·\n [\n OpenCode 格式化... ](http://www.runoob.com/opencode/opencode-formatter.html)\n\n\n ·\n [\n OpenCode 自定义... ](http://www.runoob.com/opencode/opencode-commands.html)\n\n\n\n\n 站点信息\n\n\n ·\n [意见反馈](https://www.runoob.commailto:admin@runoob.com)\n\n\n ·\n [免责声明](https://www.runoob.com/disclaimer)\n\n\n ·\n [关于我们](https://www.runoob.com/aboutus)\n\n\n ·\n [文章归档](https://www.runoob.com/archives)\n\n\n\n\n\n\n\n **关注微信**\n\n\n\n \n\n\n\n\n\n\n Copyright © 2013-2026 **[菜鸟教程](https://www.runoob.com//www.runoob.com/)** \n **[runoob.com](https://www.runoob.com//www.runoob.com/)** All Rights Reserved. 备案号:[闽ICP备15012807号-1](https://beian.miit.gov.cn/)\n\n\n\n [ ](https://www.runoob.comjavascript:void(0))\n [](https://www.runoob.comjavascript:void(0))\n [](https://www.runoob.comjavascript:void(0))\n\n\n\n#### 微信关注\n\n `;
document.getElementById('md-content').innerHTML = marked.parse(md);
</script>
</body>
</html>