-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash.html
451 lines (435 loc) · 21.1 KB
/
bash.html
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>bash — myhome 1.0.0 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.0.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="top" title="myhome 1.0.0 documentation" href="index.html" />
<link rel="next" title="c & c++" href="c_cpp.html" />
<link rel="prev" title="Conderls’ Documents" href="index.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="c_cpp.html" title="c & c++"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="index.html" title="Conderls’ Documents"
accesskey="P">previous</a> |</li>
<li><a href="index.html">myhome 1.0.0 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="bash">
<h1>bash<a class="headerlink" href="#bash" title="Permalink to this headline">¶</a></h1>
<div class="section" id="id1">
<h2>变量的命名与赋值<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
<p>变量按作用域分为:局部变量和环境变量(全局变量)。</p>
<ul class="simple">
<li>局部变量设置:<ul>
<li>通过简单赋值: <tt class="docutils literal"><span class="pre">a=1</span></tt>, <strong>等号前后不能有任何空白符</strong> 。</li>
<li>内置函数 <tt class="docutils literal"><span class="pre">declare</span></tt> (不含-x选项)</li>
<li>内置函数 <tt class="docutils literal"><span class="pre">local</span></tt> ,但仅限于函数内使用。函数不使用 <tt class="docutils literal"><span class="pre">local</span></tt> 的变量在整个shell中可见</li>
</ul>
</li>
<li>环境变量设置:<ul>
<li><tt class="docutils literal"><span class="pre">declare</span> <span class="pre">-x</span></tt></li>
<li><tt class="docutils literal"><span class="pre">export</span> <span class="pre">varname=value</span></tt></li>
</ul>
</li>
</ul>
<p>变量的引用: <tt class="docutils literal"><span class="pre">$var</span></tt> 或 <tt class="docutils literal"><span class="pre">${var}</span></tt> ,两者在不引起歧义时等价。</p>
</div>
<div class="section" id="id2">
<h2>位置和特殊参量<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2>
<table border="1" class="docutils">
<colgroup>
<col width="34%" />
<col width="66%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">位置和特殊参量</th>
<th class="head">指代对象</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>$0</td>
<td>脚本名</td>
</tr>
<tr class="row-odd"><td>$#</td>
<td>位置参数个数</td>
</tr>
<tr class="row-even"><td>$*</td>
<td>所有的位置参数</td>
</tr>
<tr class="row-odd"><td>$@</td>
<td>未加引号时与$*的含义相同</td>
</tr>
<tr class="row-even"><td>$1...${N}</td>
<td>单独的位置变量</td>
</tr>
<tr class="row-odd"><td>$$</td>
<td>当前 shell 的 PID</td>
</tr>
<tr class="row-even"><td>$-</td>
<td>当前的 sh 选项设置</td>
</tr>
<tr class="row-odd"><td>$?</td>
<td>已执行的上一条命令的退出值</td>
</tr>
<tr class="row-even"><td>$!</td>
<td>最后一个进入后台的作业 PID</td>
</tr>
<tr class="row-odd"><td>$_</td>
<td>上一条命令的最后一个参数</td>
</tr>
</tbody>
</table>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">说明:</th><td class="field-body"><tt class="docutils literal"><span class="pre">$*</span></tt> 是一个由所有位置参量组成的单一的字符串,由环境变量 IFS 第一个字符分隔;相当于 <tt class="docutils literal"><span class="pre">$1c$2c...</span></tt> , c 是 IFS 的第一个字符;$@是 N 个独立的双引字符串,由空格分隔,即相当于 <tt class="docutils literal"><span class="pre">"$1"</span> <span class="pre">"$2"</span> <span class="pre">...</span> <span class="pre">"$N"</span></tt> 。</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="shell">
<h2>开启、关闭shell特性<a class="headerlink" href="#shell" title="Permalink to this headline">¶</a></h2>
<div class="section" id="set">
<h3>set<a class="headerlink" href="#set" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre>set -o noclobber # 开启noclobber特性
set +o noclobber # 关闭noclobber特性
set -o # 列出所有set控制的每一项特性及其状态
set +o # 以特殊方式列出上述特性
</pre></div>
</div>
</div>
<div class="section" id="shopt">
<h3>shopt<a class="headerlink" href="#shopt" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre>shopt -s dotglob # -s 设置
shopt -u dotglob # -u 取消设置
shopt -o -s noclobber # -o 设置由set控制的特性
shopt # 列出shopt控制的选项
shopt -s # 列出shopt设置开启的特性
shopt -u # 列出shopt设置取消的特性
</pre></div>
</div>
</div>
</div>
<div class="section" id="readline">
<h2>配置Readline库<a class="headerlink" href="#readline" title="Permalink to this headline">¶</a></h2>
<p>使用Readline库的bash和其他程序都会读取由INPUTRC环境变量指定的文件,以获取初始化信息。如果没有设置INPUTRC,这些程序将从文件 <tt class="docutils literal"><span class="pre">~/.inputrc</span></tt> 中读取,忽略其以空白行或以#开头的行。</p>
<div class="section" id="id3">
<h3>变量<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre>set veriable value
</pre></div>
</div>
</div>
<div class="section" id="id4">
<h3>键绑定<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h3>
<p>用户可以用以下两种方式,指定按键序列到Readline命令的映射绑定。</p>
<div class="highlight-python"><div class="highlight"><pre>keyname: command_name
" Keystroke_sequence ": command_name
</pre></div>
</div>
<p>比如:</p>
<div class="highlight-python"><div class="highlight"><pre>control-u: kill-whole-line
</pre></div>
</div>
<p>第二种形式中可以使用反斜杠转义序列来描述特殊的键如:CONTROL(\C)、META(\M)、ESCAPE(\e)</p>
<p>使用 <tt class="docutils literal"><span class="pre">bind</span> <span class="pre">-P</span></tt> 命令显示所有Readline命令列表,vi模式下使用的命令均以vi开头,其余的则在emacs模式下可用,使用 <tt class="docutils literal"><span class="pre">bind</span> <span class="pre">-P</span> <span class="pre">command_name</span></tt> 查看某命令绑定的按键序列。</p>
<p>可以用双引号将文本括起来(只在emacs模式下可用),以绑定该文本:</p>
<div class="highlight-python"><div class="highlight"><pre>"LOS": "The Linux Operating System"
</pre></div>
</div>
<p>使用该命令后,当用户输入LOS时,bash就会插入相应字符串。</p>
</div>
<div class="section" id="id5">
<h3>条件结构<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h3>
<p>语法如下:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$if</span> <span class="o">[</span><span class="nb">test</span><span class="o">[=</span>value<span class="o">]]</span>
commands
<span class="o">[</span><span class="nv">$else</span>
commands
<span class="nv">$endif</span>
</pre></div>
</div>
<p>其中,<strong>test</strong> 为mode、term或bash。如:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$if</span> <span class="nv">mode</span><span class="o">=</span>vi
<span class="nv">$if</span> <span class="nv">term</span><span class="o">=</span>xterm
<span class="nv">$if</span> bash
</pre></div>
</div>
</div>
</div>
<div class="section" id="id6">
<h2>别名<a class="headerlink" href="#id6" title="Permalink to this headline">¶</a></h2>
<p>bash下使用语法:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nb">alias</span> <span class="o">[</span>name<span class="o">[=</span>value<span class="o">]]</span> <span class="c">#等号左右没有空格或制表符,value两边必须有括号</span>
<span class="nb">alias </span><span class="nv">ls</span><span class="o">=</span><span class="s1">'ls -F'</span> <span class="c">#别名不替换自己,避免可能存在的无限递归。</span>
<span class="c">#别名中若使用双引号,则引号内容中的变量将进行扩展,单引号则不会。</span>
<span class="c">#使用./command或执行该命令的绝对路径名将阻止shell调用别名。</span>
<span class="nb">alias</span> <span class="c">#查看已定义的别名</span>
</pre></div>
</div>
</div>
<div class="section" id="id7">
<h2>数组<a class="headerlink" href="#id7" title="Permalink to this headline">¶</a></h2>
<p>bash支持一维数组,默认下标从0开始:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">names</span><span class="o">=([</span>2<span class="o">]=</span>alice <span class="o">[</span>0<span class="o">]=</span>hatter <span class="o">[</span>1<span class="o">]=</span>duchess<span class="o">)</span>
<span class="nv">names</span><span class="o">=(</span>hatter duchess alice<span class="o">)</span>
<span class="nv">names</span><span class="o">=(</span>hatter <span class="o">[</span>5<span class="o">]=</span>duchess alice<span class="o">)</span> <span class="c">#names[0]=hatter names[5]=duchess names[6]=alice</span>
<span class="c">#下标为1,2,3,4的元素不存在</span>
<span class="c"># 引用格式:</span>
<span class="k">${</span><span class="nv">array</span><span class="p">[i]</span><span class="k">}</span>
<span class="k">for </span>i in <span class="s2">"${array[@]}"</span>;<span class="k">do</span> <span class="c"># ${array[@]}或${array[*]} 获取所有元素</span>
<span class="nb">echo</span> <span class="nv">$i</span>
<span class="k">done</span>
<span class="k">${</span><span class="p">!array[@]</span><span class="k">}</span> <span class="c"># 获取有元素的下标,如开头第三个数组有值的下标:0,5,6</span>
<span class="k">${#</span><span class="nv">array</span><span class="p">[@]</span><span class="k">}</span> 或 <span class="k">${#</span><span class="nv">array</span><span class="p">[*]</span><span class="k">}</span> <span class="c"># 获取非NULL元素个数</span>
</pre></div>
</div>
</div>
<div class="section" id="id8">
<h2>函数<a class="headerlink" href="#id8" title="Permalink to this headline">¶</a></h2>
<p>shell函数存放在物理内存(RAM)中而不是磁盘文件中,因此,shell访问函数的数度快于访问脚本的速度,而且shell还对函数预处理,启动速度也比脚本快的多。shell函数的执行和调用实在同一个shell中进行,而脚本中若定义太多函数,启动一个子shell的开销将变得很大。</p>
<p>shell函数声明可以放在 <tt class="docutils literal"><span class="pre">~/.bash_profile</span></tt> 或使用该函数的脚本中,也可直接放在命令行中。使用unset命令删除变量和函数。</p>
<p>函数声明:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="o">[</span><span class="k">function</span><span class="o">]</span> <span class="k">function</span>-name <span class="o">()</span>
<span class="o">{</span>
commands
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="id9">
<h2>命令行扩展<a class="headerlink" href="#id9" title="Permalink to this headline">¶</a></h2>
<div class="section" id="id10">
<h3>花括号扩展<a class="headerlink" href="#id10" title="Permalink to this headline">¶</a></h3>
<div class="highlight-bash"><div class="highlight"><pre><span class="nb">echo </span>chap_<span class="o">{</span>one,two,three<span class="o">}</span>.txt
mkdir var<span class="o">{</span>A,B,C<span class="o">}</span> <span class="c"># mkdir var[A-C]不可行</span>
<span class="c">#花括号里至少要有一个逗号并且没有未引用的空白字符,可以嵌套使用</span>
</pre></div>
</div>
</div>
<div class="section" id="id11">
<h3>算术扩展<a class="headerlink" href="#id11" title="Permalink to this headline">¶</a></h3>
<div class="highlight-bash"><div class="highlight"><pre><span class="k">$(())</span>
<span class="k">$((</span>expression<span class="k">))</span> <span class="c"># 计算expression,可作为参数传递给命令或命令行任何数字位置上</span>
<span class="nb">read </span>age
<span class="k">$((</span><span class="m">60</span><span class="o">-</span>age<span class="k">))</span>
<span class="k">$((</span><span class="m">2</span><span class="o">*</span><span class="nv">$x</span><span class="o">+</span><span class="m">2</span><span class="o">*</span><span class="nv">$y</span><span class="k">))</span>
<span class="k">$((</span><span class="m">2</span><span class="o">*</span>x+2*y<span class="k">))</span> <span class="c">#等价于上式</span>
<span class="nb">let</span> 内置命令
<span class="nb">let</span> <span class="s2">"number=$(wc -l < letter.txt)/66 +1"</span>
<span class="nb">let </span><span class="nv">a</span><span class="o">=</span>5+3 <span class="nv">b</span><span class="o">=</span>7+2
<span class="nb">let</span> <span class="s2">"i= i+2"</span> <span class="c"># 带空格需要加引号</span>
<span class="c"># 使用let或$(())进行算术扩展时,引用变量可以不在变量名前加$符号,但推荐加。</span>
<span class="nb">declare</span> –i num
<span class="nv">num</span><span class="o">=</span>hello <span class="c">#error!</span>
<span class="nv">num</span><span class="o">=</span>5 + 5 <span class="c">#error!不能有空格</span>
<span class="nv">num</span><span class="o">=</span><span class="s2">"4 * 6"</span> <span class="c">#带引号,可以有空格</span>
<span class="nv">num</span><span class="o">=</span>6.789 <span class="c">#num=6 去尾</span>
</pre></div>
</div>
<p>let支持的操作符:</p>
<table border="1" class="docutils">
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">操作符</th>
<th class="head">含 义</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>-</td>
<td>负号</td>
</tr>
<tr class="row-odd"><td>!</td>
<td>逻辑非</td>
</tr>
<tr class="row-even"><td>~</td>
<td>按位取反</td>
</tr>
<tr class="row-odd"><td>*</td>
<td>乘法</td>
</tr>
<tr class="row-even"><td>/</td>
<td>除法</td>
</tr>
<tr class="row-odd"><td>%</td>
<td>求模</td>
</tr>
<tr class="row-even"><td>+</td>
<td>加法</td>
</tr>
<tr class="row-odd"><td>-</td>
<td>减法</td>
</tr>
<tr class="row-even"><td><<</td>
<td>左移位</td>
</tr>
<tr class="row-odd"><td>>></td>
<td>右移位</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre"><=</span> <span class="pre">>=</span> <span class="pre"><</span> <span class="pre">></span> <span class="pre">==</span> <span class="pre">!=</span></tt></td>
<td>关系运算符</td>
</tr>
<tr class="row-odd"><td>&</td>
<td>按位与</td>
</tr>
<tr class="row-even"><td>^|</td>
<td>异或</td>
</tr>
<tr class="row-odd"><td>&&</td>
<td>逻辑与</td>
</tr>
<tr class="row-even"><td>||</td>
<td>逻辑或</td>
</tr>
<tr class="row-odd"><td>!</td>
<td>逻辑非</td>
</tr>
<tr class="row-even"><td>=</td>
<td>赋值</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">*=</span> <span class="pre">/=</span> <span class="pre">%=</span> <span class="pre">+=</span> <span class="pre">-=</span></tt></td>
<td> </td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre"><<=</span> <span class="pre">>>=</span> <span class="pre">&=</span> <span class="pre">^=</span> <span class="pre">|=</span></tt></td>
<td>赋值简写符</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">++</span> <span class="pre">--</span></tt></td>
<td>自增 自减</td>
</tr>
<tr class="row-even"><td>**</td>
<td>乘方</td>
</tr>
<tr class="row-odd"><td>expr?expr:expr</td>
<td>条件表达式</td>
</tr>
<tr class="row-even"><td>expr1,expr2</td>
<td>逗号表达式</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="id12">
<h3>命令扩展<a class="headerlink" href="#id12" title="Permalink to this headline">¶</a></h3>
<div class="highlight-bash"><div class="highlight"><pre><span class="c">#两种方式</span>
<span class="k">$(</span><span class="nb">command</span><span class="k">)</span>
<span class="sb">`</span> <span class="nb">command</span><span class="sb">`</span> <span class="c"># 这里是反引号,非单引号</span>
<span class="nv">a</span><span class="o">=</span><span class="k">$(</span><span class="nb">pwd</span><span class="k">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">bash</a><ul>
<li><a class="reference internal" href="#id1">变量的命名与赋值</a></li>
<li><a class="reference internal" href="#id2">位置和特殊参量</a></li>
<li><a class="reference internal" href="#shell">开启、关闭shell特性</a><ul>
<li><a class="reference internal" href="#set">set</a></li>
<li><a class="reference internal" href="#shopt">shopt</a></li>
</ul>
</li>
<li><a class="reference internal" href="#readline">配置Readline库</a><ul>
<li><a class="reference internal" href="#id3">变量</a></li>
<li><a class="reference internal" href="#id4">键绑定</a></li>
<li><a class="reference internal" href="#id5">条件结构</a></li>
</ul>
</li>
<li><a class="reference internal" href="#id6">别名</a></li>
<li><a class="reference internal" href="#id7">数组</a></li>
<li><a class="reference internal" href="#id8">函数</a></li>
<li><a class="reference internal" href="#id9">命令行扩展</a><ul>
<li><a class="reference internal" href="#id10">花括号扩展</a></li>
<li><a class="reference internal" href="#id11">算术扩展</a></li>
<li><a class="reference internal" href="#id12">命令扩展</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="index.html"
title="previous chapter">Conderls’ Documents</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="c_cpp.html"
title="next chapter">c & c++</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/bash.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="c_cpp.html" title="c & c++"
>next</a> |</li>
<li class="right" >
<a href="index.html" title="Conderls’ Documents"
>previous</a> |</li>
<li><a href="index.html">myhome 1.0.0 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2014, conderls.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
</div>
</body>
</html>