-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path21.html
More file actions
278 lines (278 loc) · 20.6 KB
/
Copy path21.html
File metadata and controls
278 lines (278 loc) · 20.6 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">Shell 流程控制</a>
<a href="21.html" class="active">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="20.html" class="page-nav-link">← Shell 流程控制</a>
<a href="22.html" class="page-nav-link">Linux 命令大全 →</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\nlinux shell 可以用户定义函数,然后在shell脚本中可以随便调用。\n\n\n\nshell中函数的定义格式如下:\n\n\n\n\n[ function ] funname [()]\n\n{\n\n action;\n\n [return int;]\n\n}\n\n\n\n说明:\n\n\n\n\n\n- \n1、可以带 function fun() 定义,也可以直接 fun() 定义,不带任何参数。\n\n- \n2、参数返回,可以显示加:**return** 返回,如果不加,将以最后一条命令运行结果,作为返回值。 **return** 后跟数值 n(0-255).\n\n\n\n\n下面的例子定义了一个函数并进行调用:\n\n\n\n\n## 实例\n\n#!/bin/bash\n\n# author:菜鸟教程\n\n# url:www.runoob.com\n\ndemoFun(){\n\n echo "这是我的第一个 shell 函数!"\n\n}\n\necho "-----函数开始执行-----"\n\ndemoFun\n\necho "-----函数执行完毕-----"\n\n\n\n\n输出结果:\n\n\n\n\`\`\`\n\n-----函数开始执行-----\n这是我的第一个 shell 函数!\n-----函数执行完毕-----\n\n\`\`\`\n\n\n下面定义一个带有 return 语句的函数:\n\n\n\n\n## 实例\n\n#!/bin/bash\n\n# author:菜鸟教程\n\n# url:www.runoob.com\n\nfunWithReturn(){\n\n echo "这个函数会对输入的两个数字进行相加运算..."\n\n echo "输入第一个数字: "\n\n read aNum\n\n echo "输入第二个数字: "\n\n read anotherNum\n\n echo "两个数字分别为 $aNum 和 $anotherNum !"\n\n return $(($aNum+$anotherNum))\n\n}\n\nfunWithReturn\n\necho "输入的两个数字之和为 $? !"\n\n\n\n\n输出类似下面:\n\n\n\n\`\`\`\n\n这个函数会对输入的两个数字进行相加运算...\n输入第一个数字: \n1\n输入第二个数字: \n2\n两个数字分别为 1 和 2 !\n输入的两个数字之和为 3 !\n\n\`\`\`\n\n\n函数返回值在调用该函数后通过 $? 来获得。\n\n\n\n**注意:**所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。\n\n\n**注意:** return 语句只能返回一个介于 0 到 255 之间的整数,而两个输入数字的和可能超过这个范围。\n\n\n要解决这个问题,您可以修改 return 语句,直接使用 echo 输出和而不是使用 return:\n\n\n\n## 实例\n\nfunWithReturn(){\n\n echo "这个函数会对输入的两个数字进行相加运算..."\n\n echo "输入第一个数字: "\n\n read aNum\n\n echo "输入第二个数字: "\n\n read anotherNum\n\n sum=$(($aNum + $anotherNum))\n\n echo "两个数字分别为 $aNum 和 $anotherNum !"\n\n echo $sum # 输出两个数字的和\n\n}\n\n\n\n\n---\n\n\n## 函数参数\n\n在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...\n\n\n\n带参数的函数示例:\n\n\n\n\n## 实例\n\n#!/bin/bash\n\n# author:菜鸟教程\n\n# url:www.runoob.com\n\nfunWithParam(){\n\n echo "第一个参数为 $1 !"\n\n echo "第二个参数为 $2 !"\n\n echo "第十个参数为 $10 !"\n\n echo "第十个参数为 ${10} !"\n\n echo "第十一个参数为 ${11} !"\n\n echo "参数总数有 $# 个!"\n\n echo "作为一个字符串输出所有参数 $* !"\n\n}\n\nfunWithParam 1 2 3 4 5 6 7 8 9 34 73\n\n\n\n\n输出结果:\n\n\n\n\`\`\`\n\n第一个参数为 1 !\n第二个参数为 2 !\n第十个参数为 10 !\n第十个参数为 34 !\n第十一个参数为 73 !\n参数总数有 11 个!\n作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !\n\n\`\`\`\n\n\n注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${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 脚本运行的当前进程ID号\n \n \n \n $!\n \n 后台运行的最后一个进程的ID号\n \n \n \n $@\n \n 与$*相同,但是使用时加引号,并在引号中返回每个参数。\n \n \n \n $-\n \n 显示Shell使用的当前选项,与set命令功能相同。\n \n \n \n $?\n \n 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。\n \n \n \n \n \n \n \n\n \n \n [ Linux cat 命令](http://www.runoob.com/linux/linux-comm-cat.html)\n \n\n \n \n \n \n \n\n\n\n\n\n \n## \n \n \n \n 2 篇笔记\n 写笔记\n\n\n\n\n\n \n\n #0 乱码kun\n\n ly1***657282@gmail.com\n\n 510$? 仅对其上一条指令负责,一旦函数返回后其返回值没有立即保存入参数,那么其返回值将不再能通过 $? 获得。\n\n\n\n测试代码:\n\n\n\n\n\n\`\`\`\n#!/bin/bash\nfunction demoFun1(){\n echo "这是我的第一个 shell 函数!"\n return \`expr 1 + 1\`\n}\n\ndemoFun1\necho $?\n\nfunction demoFun2(){\n echo "这是我的第二个 shell 函数!"\n expr 1 + 1\n}\n\ndemoFun2\necho $?\ndemoFun1\necho 在这里插入命令!\necho $?\n\`\`\`\n\n\n\n输出结果:\n\n\n\n\n\n\`\`\`\n这是我的第一个 shell 函数!\n2\n这是我的第二个 shell 函数!\n2\n0\n这是我的第一个 shell 函数!\n在这里插入命令!\n0\n\`\`\`\n\n\n\n调用 demoFun2 后,函数最后一条命令 expr 1 + 1 得到的返回值($?值)为 0,意思是这个命令没有出错。所有的命令的返回值仅表示其是否出错,而不会有其他有含义的结果。\n\n\n\n第二次调用 demoFun1 后,没有立即查看 $? 的值,而是先插入了一条别的 echo 命令,最后再查看 $? 的值得到的是 0,也就是上一条 echo 命令的结果,而 demoFun1 的返回值被覆盖了。\n\n\n\n下面这个测试,连续使用两次 **echo $?**,得到的结果不同,更为直观:\n\n\n\n\n\n\`\`\`\n#!/bin/bash\n\nfunction demoFun1(){\n echo "这是我的第一个 shell 函数!"\n return \`expr 1 + 1\`\n}\n\ndemoFun1\necho $?\necho $?\n\`\`\`\n\n\n\n输出结果:\n\n\n\n\n\n\`\`\`\n这是我的第一个 shell 函数!\n2\n0\n\`\`\`\n[乱码kun](https://www.runoob.comjavascript:;) 乱码kun\n\n ly1***657282@gmail.com\n\n7年前 (2019-07-13)\n#0 乱码kun\n\n ly1***657282@gmail.com\n\n 296函数与命令的执行结果可以作为条件语句使用。要注意的是,和 C 语言不同,shell 语言中 0 代表 true,0 以外的值代表 false。\n\n\n\n请参见下例:\n\n\n\n\n\n\`\`\`\n#!/bin/bash\n\necho "Hello World !" | grep -e Hello\necho $?\necho "Hello World !" | grep -e Bye\necho $?\nif echo "Hello World !" | grep -e Hello\nthen\n echo true\nelse\n echo false\nfi\n\nif echo "Hello World !" | grep -e Bye\nthen\n echo true\nelse\n echo false\nfi\n\nfunction demoFun1(){\n return 0\n}\n\nfunction demoFun2(){\n return 12\n}\n\nif demoFun1\nthen\n echo true\nelse\n echo false\nfi\n\nif demoFun2\nthen\n echo true\nelse\n echo false\nfi\n\`\`\`\n\n\n\n其执行结果如下:\n\n\n\n\n\n\`\`\`\nHello World !\n0\n1\nHello World !\ntrue\nfalse\ntrue\nfalse\n\`\`\`\n\n\n\ngrep 是从给定字符串中寻找匹配内容的命令。首先看出如果找到了匹配的内容,会打印匹配部分且得到的返回值 $? 为 0,如果找不到,则返回值 $? 为 1。\n\n\n\n接下来分别将这两次执行的 grep 命令当作条件语句交给 if 判断,得出返回值 $? 为 0,即执行成功时,条件语句为 true,当返回值 $? 为 1,即执行失败时,条件语句为 false。\n\n\n\n之后再用函数的 return 值作为测试,其中 demoFun1 返回值为 0,demoFun2 返回值选择了任意一个和 0 不同的整数,这里为 12。\n\n\n\n将函数作为条件语句交给 if 判断,得出返回值为 0 时,依然为 true,而返回值只要不是 0,条件语句都判断为 false。\n\n[乱码kun](https://www.runoob.comjavascript:;) 乱码kun\n\n ly1***657282@gmail.com\n\n7年前 (2019-07-16)\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>