追逐着月光 發表於 2017-12-6 16:01:00

Shell编程基础篇-上

<h2>1.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">前言</span></h2>
<h3>1.1.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">为什么学</span>Shell</h3>
<p style="text-indent: 21pt">Shell<span style="font-family: 等线">脚本语言是实现</span>Linux/UNIX<span style="font-family: 等线">系统管理及自动化运维所必备的重要工具,</span> Linux/UNIX<span style="font-family: 等线">系统的底层及基础应用软件的核心大都涉及</span>Shell<span style="font-family: 等线">脚本的内容。每一个合格</span> <span style="font-family: 等线">的</span>Linux<span style="font-family: 等线">系统管理员或运维工程师,都需要能够熟练地编写</span>Shell<span style="font-family: 等线">脚本语言,并能够阅</span> <span style="font-family: 等线">读系统及各类软件附带的</span>Shell<span style="font-family: 等线">脚本内容。只有这样才能提升运维人员的工作效率,适</span> <span style="font-family: 等线">应曰益复杂的工作环境,减少不必要的重复工作,从而为个人的职场发展奠定较好的基础</span></p>
<h3>1.1.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">什么是</span>shell</h3>
<p style="text-indent: 21pt">Shell<span style="font-family: 等线">是一个命令解释器,它在操作系统的最外层,负责直接与用户对话,把用户的输入解释给操作系统,并处理各种各样的操作系统的输出结果,输出屏幕返回给用户。</span></p>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线">这种对话方式可以是:</span></span></em></p>
<p style="text-indent: 11pt"><span style="font-family: 等线">交互的方式:从键盘输入命令,通过</span>/bin/bash<span style="font-family: 等线">的解析,可以立即得到</span>Shell<span style="font-family: 等线">的回应</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ls            </span>
anaconda-<span style="color: rgba(0, 0, 0, 1)">ks.cfg
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ls |bash </span>
anaconda-ks.cfg   </pre>
</div>
<p style="text-indent: 11pt"><span style="font-family: 等线">非交互的方式</span>: <span style="font-family: 等线">脚本</span></p>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201712/1190037-20171206153555144-842391077.png" alt=""></p>
<h3>1.1.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">什么是</span>Shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">脚本</span></h3>
<p style="margin-left: 21pt"><span style="font-family: 等线">&nbsp; 命令、变量和流程控制语句等有机的结合起来</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shell<span style="font-family: 等线">脚本擅长处理纯文本类型的数据,而</span>linux<span style="font-family: 等线">中,几乎所有的配置文件,日志,都是纯文本类型文件</span></p>
<h3>1.1.4 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">脚本语言的种类</span></h3>
<p><strong><span style="font-family: 等线">一、编译型语言</span></strong></p>
<p><strong><span style="font-family: 等线">定义:</span></strong><span style="font-family: 等线">指用专用的编译器,针对特定的操作平台(操作系统)将某种高级语言源代码一次性翻译成可被硬件平台直接运行的二进制机器码(具有操作数,指令、及相应的格式),这个过程叫做编译(</span>./configure&nbsp; make makeinstall <span style="font-family: 等线">);编译好的可执行性文件(</span>.exe<span style="font-family: 等线">),可在相对应的平台上运行(移植性差,但运行效率高)。。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 等线">典型的编译型语言有,</span> <span style="background: rgba(255, 255, 0, 1)">C</span><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">语言、</span><span style="background: rgba(255, 255, 0, 1)">C++</span><span style="font-family: 等线">等。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 等线">另外,</span>Java<span style="font-family: 等线">语言是一门很特殊的语言,</span>Java<span style="font-family: 等线">程序需要进行编译步骤,但并不会生成特定平台的二进制机器码,它编译后生成的是一种与平台无关的字节码文件(</span>*.class<span style="font-family: 等线">)(移植性好的原因),这种字节码自然不能被平台直接执行,运行时需要由解释器解释成相应平台的二进制机器码文件;大多数人认为</span>Java<span style="font-family: 等线">是一种编译型语言,但我们说</span>Java<span style="font-family: 等线">即是编译型语言,也是解释型语言也并没有错。</span></p>
<p><strong><span style="font-family: 等线">二、解释型语言</span></strong></p>
<p><strong><span style="font-family: 等线">定义:</span></strong><span style="font-family: 等线">指用专门解释器对源程序逐行解释成特定平台的机器码并立即执行的语言;相当于把编译型语言的编译链接过程混到一起同时完成的。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 等线">解释型语言执行效率较低,且不能脱离解释器运行,但它的跨平台型比较容易,只需提供特定解释器即可。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 等线">常见的解释型语言有,</span> Python<span style="font-family: 等线">(同时是脚本语言)与</span>Ruby<span style="font-family: 等线">等。</span></p>
<p><strong><span style="font-family: 等线">三、脚本语言</span></strong></p>
<p><strong><span style="font-family: 等线">定义:</span></strong><span style="font-family: 等线">为了缩短传统的编写</span>-<span style="font-family: 等线">编译</span>-<span style="font-family: 等线">链接</span>-<span style="font-family: 等线">运行(</span>edit-compile-link-run<span style="font-family: 等线">)过程而创建的计算机编程语言。</span></p>
<p style="text-indent: 21pt"><strong><span style="font-family: 等线">特点:</span></strong><span style="font-family: 等线">程序代码即是最终的执行文件,只是这个过程需要解释器的参与,所以说脚本语言与解释型语言有很大的联系。脚本语言通常是被解释执行的,而且程序是文本文件。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 等线">典型的脚本语言有,</span>JavaScript<span style="font-family: 等线">,</span>Python<span style="font-family: 等线">,</span><span style="background: rgba(255, 255, 0, 1)">shell</span><span style="font-family: 等线">等。</span></p>
<p><strong><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">其他常用的脚本语句种类</span></span></strong></p>
<p style="text-indent: 21pt"><strong>PHP</strong><span style="font-family: 等线">是网页程序,也是脚本语言。是一款更专注于</span>web<span style="font-family: 等线">页面开发(前端展示)的脚本语言,例如:</span>Dedecms,discuz<span style="font-family: 等线">。</span>PHP<span style="font-family: 等线">程序也可以处理系统日志,配置文件等,</span>php<span style="font-family: 等线">也可以调用系统命令。</span></p>
<p style="text-indent: 21pt"><strong>Perl</strong><span style="font-family: 等线">脚本语言。比</span>shell<span style="font-family: 等线">脚本强大很多,语法灵活、复杂,实现方式很多,不易读,团队协作困难,但仍不失为很好的脚本语言,存世大量的程序软件。</span>MHA<span style="font-family: 等线">高可用</span>Perl<span style="font-family: 等线">写的</span></p>
<p style="text-indent: 21pt"><strong>Python</strong><span style="font-family: 等线">,不但可以做脚本程序开发,也可以实现</span>web<span style="font-family: 等线">程序以及软件的开发。近两年越来越多的公司都会要求会</span>Python<span style="font-family: 等线">。</span></p>
<p><strong><span style="text-decoration: underline"><span style="background: rgba(0, 255, 0, 1)">Shell</span></span></strong><strong><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">脚本与</span><span style="background: rgba(0, 255, 0, 1)">php/perl/python</span></span></strong><strong><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">语言的区别和优势?</span></span></strong></p>
<p style="text-indent: 21pt">shell<span style="font-family: 等线">脚本的优势在于处理操作系统底层的业务</span> <span style="font-family: 等线">(</span>linux<span style="font-family: 等线">系统内部的应用都是</span>shell<span style="font-family: 等线">脚本完成)因为有大量的</span>linux<span style="font-family: 等线">系统命令为它做支撑。</span>2000<span style="font-family: 等线">多个命令都是</span>shell<span style="font-family: 等线">脚本编程的有力支撑,特别是</span>grep<span style="font-family: 等线">、</span>awk<span style="font-family: 等线">、</span>sed<span style="font-family: 等线">等。例如:一键软件安装、优化、监控报警脚本,常规的业务应用,</span>shell<span style="font-family: 等线">开发更简单快速,符合运维的简单、易用、高效原则</span>.</p>
<p><span style="font-family: 等线">  </span>PHP<span style="font-family: 等线">、</span>Python<span style="font-family: 等线">优势在于开发运维工具以及</span>web<span style="font-family: 等线">界面的管理工具,</span>web<span style="font-family: 等线">业务的开发等。处理一键软件安装、优化,报警脚本。常规业务的应用等</span>php/python<span style="font-family: 等线">也是能够做到的。但是开发效率和复杂比用</span>shell<span style="font-family: 等线">就差很多了。</span></p>
<p><span style="color: rgba(0, 255, 0, 1)"><strong><span style="font-family: 等线">系统环境说明</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat /etc/redhat-release </span>
CentOS Linux release 7.4.1708<span style="color: rgba(0, 0, 0, 1)"> (Core)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> uname -r</span>
3.10.0-693<span style="color: rgba(0, 0, 0, 1)">.el7.x86_64
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> getenforce </span>
<span style="color: rgba(0, 0, 0, 1)">Disabled
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> systemctl status firewalld.service </span>
● firewalld.service - firewalld -<span style="color: rgba(0, 0, 0, 1)"> dynamic firewall daemon
   Loaded: loaded (</span>/usr/lib/systemd/system/<span style="color: rgba(0, 0, 0, 1)">firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
   Docs: man:firewalld(</span>1)</pre>
</div>
<h3>1.1.5 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">系统中的</span>shell</h3>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线">查看系统中的命解释器</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat /etc/shells </span>
/bin/<span style="color: rgba(0, 0, 0, 1)">sh
</span>/bin/<span style="color: rgba(0, 0, 0, 1)">bash
</span>/sbin/<span style="color: rgba(0, 0, 0, 1)">nologin
</span>/usr/bin/<span style="color: rgba(0, 0, 0, 1)">sh
</span>/usr/bin/<span style="color: rgba(0, 0, 0, 1)">bash
</span>/usr/sbin/nologin</pre>
</div>
<p><strong><span style="text-decoration: underline"><span style="font-family: 等线">常用操作系统的默认</span>shell </span></strong></p>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p>1.Linux<span style="font-family: &quot;微软雅黑&quot;, sans-serif">是</span>Bourne Again shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">(</span>bash<span style="font-family: &quot;微软雅黑&quot;, sans-serif">)</span></p>
<p>2.Solaris<span style="font-family: &quot;微软雅黑&quot;, sans-serif">和</span>FreeBSD<span style="font-family: &quot;微软雅黑&quot;, sans-serif">缺省的是</span>Bourne shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">(</span>sh<span style="font-family: &quot;微软雅黑&quot;, sans-serif">)</span></p>
<p>3.AIX<span style="font-family: &quot;微软雅黑&quot;, sans-serif">下是</span>Korn Shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">(</span>ksh<span style="font-family: &quot;微软雅黑&quot;, sans-serif">)</span></p>
<p>4.HP-UX<span style="font-family: &quot;微软雅黑&quot;, sans-serif">缺省的是</span>POSIX shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">(</span>sh<span style="font-family: &quot;微软雅黑&quot;, sans-serif">)</span></p>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $SHELL</span>
/bin/bash</pre>
</div>
<p><strong><span style="text-decoration: underline"><span style="background: rgba(0, 255, 0, 1)">bash</span></span></strong><strong><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">版本</span></span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> bash -version</span>
GNU bash, 版本 4.2.46(2)-release (x86_64-redhat-linux-<span style="color: rgba(0, 0, 0, 1)">gnu)
Copyright (C) </span>2011<span style="color: rgba(0, 0, 0, 1)"> Free Software Foundation, Inc.
许可证 GPLv3</span>+: GNU GPL 许可证版本3或者更高 &lt;http://gnu.org/licenses/gpl.html&gt;<span style="color: rgba(0, 0, 0, 1)">

这是自由软件,您可以自由地更改和重新发布。
在法律允许的范围内没有担保.</span>&nbsp;</pre>
</div>
<p><strong><span style="color: rgba(255, 0, 0, 1); background: rgba(0, 255, 255, 1)">bash </span></strong><strong><span style="font-family: 等线; color: rgba(255, 0, 0, 1); background: rgba(0, 255, 255, 1)">破壳漏洞</span></strong></p>
<div class="cnblogs_code">
<pre>使用 命令 env x=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">() { :;}; echo be careful</span><span style="color: rgba(128, 0, 0, 1)">'</span> bash -c <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">echo this is a test</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
如果返回结果为一行,则为正常,
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> env x='() { :;}; echo be careful' bash -c "echo this is a test"</span>
this <span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> a test

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">解决办法 升级当前的bash版本</span>
yum install update bash</pre>
</div>
<p><em><span style="text-decoration: underline">sh</span></em><em><span style="text-decoration: underline"><span style="font-family: 等线">与</span>bash </span></em><em><span style="text-decoration: underline"><span style="font-family: 等线">的关系</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">ll /bin/sh</span>
lrwxrwxrwx. 1 root root 4 11月 13 11:15 /bin/sh -&gt; bash</pre>
</div>
<p><em><span style="text-decoration: underline">/bin</span></em><em><span style="text-decoration: underline"><span style="font-family: 等线">与</span> /user/bin </span></em><em><span style="text-decoration: underline"><span style="font-family: 等线">的关系</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ll /bin -d</span>
lrwxrwxrwx. 1 root root 7 11月 13 11:15 /bin -&gt; usr/bin</pre>
</div>
<h2>1.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">脚本书写规范</span></h2>
<h3>1.2.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">脚本统一存放目录</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> mkdir -p /server/scripts/</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cd /server/scripts/</span></pre>
</div>
<h3>1.2.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">选择解释器</span></h3>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">注意格式</span> <span style="font-family: &quot;微软雅黑&quot;, sans-serif">↓</span></p>
<p style="text-indent: 21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">其中开头的</span>"#!"<span style="font-family: &quot;微软雅黑&quot;, sans-serif">字符又称为幻数,在执行</span>bash<span style="font-family: &quot;微软雅黑&quot;, sans-serif">脚本的时候,内核会根据</span>"#!"<span style="font-family: &quot;微软雅黑&quot;, sans-serif">后的解释器来确定该用那个程序解释这个脚本中的内容。</span></p>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> head -1 /etc/init.d/*</span>
==&gt; /etc/init.d/functions &lt;==
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> -*-Shell-script-*-</span>

==&gt; /etc/init.d/netconsole &lt;==
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span>

==&gt; /etc/init.d/network &lt;==
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">! /bin/bash</span></pre>
</div>
<h3>1.2.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">编辑脚本使用</span>vim</h3>
<p><span style="font-family: 等线">使用</span> .vimrc <span style="font-family: 等线">文件,能够快速的生成开头的注释信息</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat~/.vimrc </span>
autocmd BufNewFile *.py,*.cc,*.sh,*.java <span style="color: rgba(0, 0, 255, 1)">exec</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">:call SetTitle()</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">

func SetTitle()
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> expand(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%:e</span><span style="color: rgba(128, 0, 0, 1)">"</span>) == <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sh</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
      call setline(</span>1,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">#!/bin/bash</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      call setline(</span>2, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">##############################################################</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      call setline(</span>3, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"># File Name: </span><span style="color: rgba(128, 0, 0, 1)">"</span>.expand(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">))
      call setline(</span>4, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"># Version: V1.0</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      call setline(</span>5, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"># Author: clsn</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      call setline(</span>6, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"># Organization: http://blog.znix.top</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      call setline(</span>7, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"># Created Time : </span><span style="color: rgba(128, 0, 0, 1)">"</span>.strftime(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%F %T</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">))
      call setline(</span>8, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"># Description:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      call setline(</span>9, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">##############################################################</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
      call setline(</span>10, <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">)
    endif
endfunc</span></pre>
</div>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线">使用后的效果</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> catscripts_test.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">#############################################################</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: scripts_test.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-04 11:39:57</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:First scripts file</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">#############################################################</span></pre>
</div>
<p style="text-indent: 15.75pt"><span style="font-family: 等线">在</span>Shell<span style="font-family: 等线">脚本中,跟在</span>#<span style="font-family: 等线">后面的内容表示注释。注释部分不会被执行,仅给人看。注释可以自成一行,也可以跟在命令后面,与命令同行。要养成写注释的习惯,方便自己与他人。</span></p>
<p style="text-indent: 15.75pt"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">最好不用中文注释,因为在不同字符集的系统会出现乱码</span><span style="font-family: 等线">。</span>(<span style="font-family: 等线">字符集为</span>zh_CN.UTF-8,<span style="font-family: 等线">为中文</span>)<span style="font-family: 等线">。</span></p>
<h3>1.2.4 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">文件名规范</span></h3>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family: 等线">名字要有意义,并且结尾以</span> .sh <span style="font-family: 等线">结束</span></p>
<h3>1.2.5 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">开发的规范和习惯小结</span></h3>
<p style="margin-left: 11pt">1) <span style="font-family: 等线">放在统一的目录</span></p>
<p style="margin-left: 11pt">2) <span style="font-family: 等线">脚本以</span>.sh<span style="font-family: 等线">为扩展名</span></p>
<p style="margin-left: 11pt">3) <span style="font-family: 等线">开头指定脚本解释器。</span></p>
<p style="margin-left: 11pt">4) <span style="font-family: 等线">开头加版本版权等信息,可配置</span>~/.vimrc<span style="font-family: 等线">文件自动添加。</span></p>
<p style="margin-left: 11pt">5) <span style="font-family: 等线">脚本不要用中文注释,尽量用英文注释。</span></p>
<p style="margin-left: 11pt">6) <span style="font-family: 等线">代码书写优秀习惯</span></p>
<p style="margin-left: 22pt">&nbsp; a<span style="font-family: 等线">、成对的内容一次性写出来,防止遗漏,如</span>[&nbsp; ]<span style="font-family: 等线">、</span>' '<span style="font-family: 等线">、</span>" "<span style="font-family: 等线">等</span></p>
<p style="margin-left: 22pt">&nbsp; b<span style="font-family: 等线">、</span>[&nbsp; ]<span style="font-family: 等线">两端要有空格,先输入</span>[&nbsp; ],<span style="font-family: 等线">退格,输入</span>2<span style="font-family: 等线">个空格,再退格写。</span></p>
<p style="margin-left: 22pt">&nbsp; c<span style="font-family: 等线">、流程控制语句一次书写完,再添加内容。</span>(if <span style="font-family: 等线">条件</span> ; then &nbsp;<span style="font-family: 等线">内容</span>;fi)ddd</p>
<p style="margin-left: 22pt">&nbsp; d<span style="font-family: 等线">、通过缩进让代码易读。</span></p>
<p style="margin-left: 22pt">&nbsp; f<span style="font-family: 等线">、脚本中的引号都是英文状态下的引号,其他字符也是英文状态。</span></p>
<h2>1.3 shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">脚本的执行</span></h2>
<h3>1.3.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">执行脚本的办法</span></h3>
<div class="cnblogs_code">
<pre>sh/<span style="color: rgba(0, 0, 0, 1)">bash   scripts.sh
chown </span>+x   ./scripts.sh&amp;&amp; ./<span style="color: rgba(0, 0, 0, 1)">scripts.sh
source scripts.sh
. (空格) scripts.sh
cat oldboyedu.sh </span>|bash<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 效率较低</span></pre>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="background: rgba(0, 255, 0, 1)">source </span></span></em></strong><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">与</span><span style="background: rgba(0, 255, 0, 1)"> . </span></span></em></strong><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">(点)</span> </span></em></strong><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">的作用</span></span></em></strong></p>
<p><em><span style="text-decoration: underline">soucre</span></em><em><span style="text-decoration: underline"><span style="font-family: 等线">命令</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> help source|head -2</span>
<span style="color: rgba(0, 0, 0, 1)">source: source 文件名 [参数]
    在当前 shell 中执行一个文件中的命令。</span></pre>
</div>
<p><em><span style="text-decoration: underline">. (</span></em><em><span style="text-decoration: underline"><span style="font-family: 等线">点</span>)</span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> help . |head -2</span>
<span style="color: rgba(0, 0, 0, 1)">.: . 文件名 [参数]
    在当前 shell 中执行一个文件中的命令。</span></pre>
</div>
<h3>1.3.2 sh <span style="font-family: &quot;微软雅黑&quot;, sans-serif">于</span> source<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的区别</span></h3>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201712/1190037-20171206153935784-244298231.png" alt=""></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> shclsn_test.sh </span>
<span style="color: rgba(0, 0, 0, 1)">Hello World!
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">sh新建一个Shell窗口(新建一个进程)执行一个文件中的命令。</span>
<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> source clsn_test.sh </span>
<span style="color: rgba(0, 0, 0, 1)">Hello World!
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $clsn</span>
Hello World!</pre>
</div>
<p><strong><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">面试题:</span></strong></p>
<p><span style="font-family: 等线">问</span>sh test.sh<span style="font-family: 等线">后</span>echo $user<span style="font-family: 等线">返回的结果</span>_<span style="text-decoration: underline">_</span><span style="text-decoration: underline"><span style="font-family: 等线">空</span>_ </span>?</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat test.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span>
user=`whoami`</pre>
</div>
<h2>1.4 Shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的变量</span></h2>
<h3>1.4.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">什么是变量</span></h3>
<p><span style="font-family: 等线">变量可以分为两类:环境变量(全局变量)和普通变量(局部变量)</span></p>
<p><span style="font-family: 等线">  <strong>环境变量</strong>也可称为全局变量,可以在创建他们的</span>Shell<span style="font-family: 等线">及其派生出来的任意子进程</span>shell<span style="font-family: 等线">中使用,环境变量又可分为自定义环境变量和</span>Bash<span style="font-family: 等线">内置的环境变量</span></p>
<p><span style="font-family: 等线">  <strong>普通变量</strong>也可称为局部变量,只能在创建他们的</span>Shell<span style="font-family: 等线">函数或</span>Shell<span style="font-family: 等线">脚本中使用。普通变量一般是由开发者用户开发脚本程序时创建的。</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><span style="font-family: 等线">特殊变量</span></strong></p>
<h3>1.4.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">环境变量</span></h3>
<p style="text-indent: 21pt"><span style="font-family: 等线">使用</span> <strong><em><span style="background: rgba(0, 255, 0, 1)">env</span></em></strong><span style="background: rgba(0, 255, 0, 1)">/<strong><em>declare</em></strong>/<strong><em>set/export -p</em></strong></span> <span style="font-family: 等线">命令查看系统中的环境变量,这三个命令的的输出方式稍有不同。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> env</span>
XDG_SESSION_ID=1<span style="color: rgba(0, 0, 0, 1)">
HOSTNAME</span>=<span style="color: rgba(0, 0, 0, 1)">clsn
TERM</span>=<span style="color: rgba(0, 0, 0, 1)">linux
SHELL</span>=/bin/<span style="color: rgba(0, 0, 0, 1)">bash
HISTSIZE</span>=1000<span style="color: rgba(0, 0, 0, 1)">
SSH_CLIENT</span>=10.0.0.1 5537 22<span style="color: rgba(0, 0, 0, 1)">
SSH_TTY</span>=/dev/pts/<span style="color: rgba(0, 0, 0, 1)">0
USER</span>=<span style="color: rgba(0, 0, 0, 1)">root
</span>~~~</pre>
</div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family: 等线">输出一个系统中的</span> <span style="font-family: 等线">环境变量</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $LANG</span>
zh_CN.UTF-8</pre>
</div>
<h3>1.4.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">普通变量</span></h3>
<p style="text-indent: 21pt"><span style="font-family: 等线">本地变量在用户当前的</span>Shell<span style="font-family: 等线">生存期的脚本中使用。例如,本地变量</span>OLDBOY<span style="font-family: 等线">取值为</span>bingbing<span style="font-family: 等线">,这个值在用户当前</span>Shell<span style="font-family: 等线">生存期中有意义。如果在</span>Shell<span style="font-family: 等线">中启动另一个进程或退出,本地变量值将无效</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">定义普通变量实践</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> a=1</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> b='2'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> c="3"</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo "$a"</span>
1<span style="color: rgba(0, 0, 0, 1)">
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo "$b"</span>
2<span style="color: rgba(0, 0, 0, 1)">
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo "${c}"</span></pre>
</div>
<p style="text-indent: 21pt"><span style="font-family: 等线">提示:</span>$<span style="font-family: 等线">变量名表示输出变量,可以用</span>$c<span style="font-family: 等线">和</span>${c}<span style="font-family: 等线">两种用法</span></p>
<p style="text-indent: 21pt"><span style="font-family: 等线">小结:连续普通字符串内容赋值给变量,不管用什么引号或者不用引号,它的内容是什么,打印变量就输出什么</span></p>
<h3>1.4.4 export<span style="font-family: &quot;微软雅黑&quot;, sans-serif">命令</span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> help export </span>
export: export [-fn] [名称[=值] ...] 或 export -<span style="color: rgba(0, 0, 0, 1)">p
为 shell 变量设定导出属性。

标记每个 NAME 名称为自动导出到后续命令执行的环境。如果提供了 VALUE
则导出前将 VALUE 作为赋值。</span></pre>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="background: rgba(0, 255, 0, 1)">export</span></span></em></strong><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">命令的说明</span></span></em></strong></p>
<p style="text-indent: 21pt"><span style="font-family: 等线">当前</span>shell<span style="font-family: 等线">窗口及子</span>shell<span style="font-family: 等线">窗口生效</span></p>
<p style="text-indent: 21pt"><span style="font-family: 等线">在新开的</span>shell<span style="font-family: 等线">窗口不会生效,生效需要写入配置文件</span></p>
<p># <span style="font-family: 等线">定义变量</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> CSLN=clsn</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> export CSLN1=1</span></pre>
</div>
<p># <span style="font-family: 等线">当前窗口查看</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $CSLN</span>
<span style="color: rgba(0, 0, 0, 1)">clsn
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $CSLN1</span>
1</pre>
</div>
<p># <span style="font-family: 等线">编写测试脚本</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim quanju.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span>
<span style="color: rgba(0, 0, 0, 1)">echo $CSLN
echo $CSLN1</span></pre>
</div>
<p># <span style="font-family: 等线">使用</span>sh<span style="font-family: 等线">执行</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> shquanju.sh </span>

1</pre>
</div>
<p># <span style="font-family: 等线">使用</span>source <span style="font-family: 等线">执行</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> source quanju.sh </span>
<span style="color: rgba(0, 0, 0, 1)">clsn
</span>1</pre>
</div>
<h3>1.4.5 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">环境变量相关配置文件</span></h3>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p>/etc/proflie</p>
<p>/etc/bashrc</p>
<p>~/.bashrc</p>
<p>~/.bash_profile</p>
<p>/etc/proflie.d/&nbsp; # <span style="font-family: &quot;微软雅黑&quot;, sans-serif">目录</span></p>
</div>
<p><strong><em><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">四文件读取顺序(</span><span style="background: rgba(0, 255, 0, 1)">CentOS6</span></em></strong><strong><em><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">和</span><span style="background: rgba(0, 255, 0, 1)">7</span></em></strong><strong><em><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">都一样)</span></em></strong></p>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p style="text-indent: 21.2pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">①</span> /etc/profile</p>
<p style="text-indent: 21.2pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">②</span> ~/.bash_profile</p>
<p style="text-indent: 21.2pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">③</span> ~/.bashrc</p>
<p style="text-indent: 21.2pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial"><span style="font-family: &quot;微软雅黑&quot;, sans-serif">④</span> /etc/bashrc</p>
</div>
<p style="text-align: center" align="center"><img src="https://images2017.cnblogs.com/blog/1190037/201712/1190037-20171206154157347-2037699353.png" alt=""></p>
<p style="text-align: center" align="center"><strong><span style="font-family: 等线">文件读取过程示意图</span></strong></p>
<p><strong><em><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">验证四文件读取顺序的方法</span></em></strong></p>
<div class="cnblogs_code">
<pre>sed -i <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1a echo "$(date +%T-%s) /etc/profile1" &gt;&gt;/tmp/clsn</span><span style="color: rgba(128, 0, 0, 1)">'</span> /etc/<span style="color: rgba(0, 0, 0, 1)">profile
sed </span>-i <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$a echo "$(date +%T-%s) /etc/profile2" &gt;&gt;/tmp/clsn</span><span style="color: rgba(128, 0, 0, 1)">'</span> /etc/<span style="color: rgba(0, 0, 0, 1)">profile
sed </span>-i <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1a echo "$(date +%T-%s) /etc/bashrc1" &gt;&gt;/tmp/clsn</span><span style="color: rgba(128, 0, 0, 1)">'</span> /etc/<span style="color: rgba(0, 0, 0, 1)">bashrc
sed </span>-i <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$a echo "$(date +%T-%s) /etc/bashrc2" &gt;&gt;/tmp/clsn</span><span style="color: rgba(128, 0, 0, 1)">'</span> /etc/<span style="color: rgba(0, 0, 0, 1)">bashrc
sed </span>-i <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1a echo "$(date +%T-%s) ~/.bashrc1" &gt;&gt;/tmp/clsn</span><span style="color: rgba(128, 0, 0, 1)">'</span> ~/<span style="color: rgba(0, 0, 0, 1)">.bashrc
sed </span>-i <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$a echo "$(date +%T-%s) ~/.bashrc2" &gt;&gt;/tmp/clsn</span><span style="color: rgba(128, 0, 0, 1)">'</span> ~/<span style="color: rgba(0, 0, 0, 1)">.bashrc
sed </span>-i <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1a echo "$(date +%T-%s) ~/.bash_profile1" &gt;&gt;/tmp/clsn</span><span style="color: rgba(128, 0, 0, 1)">'</span> ~/<span style="color: rgba(0, 0, 0, 1)">.bash_profile
sed </span>-i <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$a echo "$(date +%T-%s) ~/.bash_profile2" &gt;&gt;/tmp/clsn</span><span style="color: rgba(128, 0, 0, 1)">'</span> ~/.bash_profile</pre>
</div>
<h3>1.4.6 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">环境变量的知识小结</span></h3>
<p style="margin-left: 28.1pt; text-indent: -21pt"><span style="font-family: Wingdings">ü&nbsp;</span><span style="font-family: &quot;微软雅黑&quot;, sans-serif">变量名通常要大写。</span></p>
<p style="margin-left: 28.1pt; text-indent: -21pt"><span style="font-family: Wingdings">ü&nbsp;</span><span style="font-family: &quot;微软雅黑&quot;, sans-serif">变量可以在自身的</span>Shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">及子</span>Shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">中使用。</span></p>
<p style="margin-left: 28.1pt; text-indent: -21pt"><span style="font-family: Wingdings">ü&nbsp;</span><span style="font-family: &quot;微软雅黑&quot;, sans-serif">常用</span>export<span style="font-family: &quot;微软雅黑&quot;, sans-serif">来定义环境变量。</span></p>
<p style="margin-left: 28.1pt; text-indent: -21pt"><span style="font-family: Wingdings">ü&nbsp;</span><span style="font-family: &quot;微软雅黑&quot;, sans-serif">执行</span>env<span style="font-family: &quot;微软雅黑&quot;, sans-serif">默认可以显示所有的环境变量名称及对应的值。</span></p>
<p style="margin-left: 28.1pt; text-indent: -21pt"><span style="font-family: Wingdings">ü&nbsp;</span><span style="font-family: &quot;微软雅黑&quot;, sans-serif">输出时用“</span>$<span style="font-family: &quot;微软雅黑&quot;, sans-serif">变量名”,取消时用“</span><em><span style="background: rgba(0, 255, 0, 1)">unset</span></em><em><span style="font-family: &quot;微软雅黑&quot;, sans-serif; background: rgba(0, 255, 0, 1)">变量名</span></em><span style="font-family: &quot;微软雅黑&quot;, sans-serif">”。</span></p>
<p style="margin-left: 28.1pt; text-indent: -21pt"><span style="font-family: Wingdings">ü&nbsp;</span><span style="font-family: &quot;微软雅黑&quot;, sans-serif">书写</span>crond<span style="font-family: &quot;微软雅黑&quot;, sans-serif">定时任务时要注意,脚本要用到的环境变量最好先在所执行的</span>Shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">脚本中重新定义。</span></p>
<p style="margin-left: 28.1pt; text-indent: -21pt"><span style="font-family: Wingdings">ü&nbsp;</span><span style="font-family: &quot;微软雅黑&quot;, sans-serif">如果希望环境变量永久生效,则可以将其<span style="text-decoration: underline">放在用户环境变量文件或全局环境变量文件</span>里。</span></p>
<h3>1.4.7 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">变量中引号的使用</span></h3>
<p style="margin-left: 21pt"><span style="font-family: 等线">只有在变量的值中有空格的时候,会使用引号。</span></p>
<p style="margin-left: 21pt"><span style="font-family: 等线">单引号与双引号的区别在于,是否能够解析特殊符号。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> name=znix</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> name2='clsn'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> name3="http://blog.znix.top"</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $name</span>
<span style="color: rgba(0, 0, 0, 1)">znix
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $name2</span>
<span style="color: rgba(0, 0, 0, 1)">clsn
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $name3</span>
http://<span style="color: rgba(0, 0, 0, 1)">blog.znix.top
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> name4='cl sn'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $name4</span>
<span style="color: rgba(0, 0, 0, 1)">cl sn
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> name5="cl sn"</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $name5</span>
<span style="color: rgba(0, 0, 0, 1)">cl sn
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> name6='cl sn $PWD'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $name6</span>
<span style="color: rgba(0, 0, 0, 1)">cl sn $PWD
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> name6="cl sn $PWD"</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $name6</span>
cl sn /root</pre>
</div>
<h3>1.4.8 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">普通变量的要求</span></h3>
<p style="margin-left: 21pt">1)&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family: 等线">内容是纯数字、简单的连续字符(内容中不带任何空格)时,定义时可以不加任何引号,例如:</span></p>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p style="text-indent: 35.4pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">a.ClsnAge=22</p>
<p style="text-indent: 35.4pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">b.NETWORKING=yes</p>
</div>
<p style="margin-left: 21pt">2)&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family: 等线">没有特殊情况时,字符串一律用双引号定义赋值,特别是多个字符串中间有空格时,例如:</span></p>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p style="text-indent: 35.4pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">a.NFSD_MODULE="no load"</p>
<p style="text-indent: 35.4pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">b.MyName="Oldboy is a handsome boy."</p>
</div>
<p style="text-indent: 21pt">3)&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family: 等线">当变量里的内容需要原样输出时,要用单引号(</span>M<span style="font-family: 等线">),这样的需求极少,例如:</span></p>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p style="text-indent: 35.4pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">a.OLDBOY_NAME='OLDBOY'</p>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">变量使用反引号赋值</span></span></em></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> time=`date`</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $time</span>
2017年 12月 05日 星期二 09:02:06<span style="color: rgba(0, 0, 0, 1)"> CST
   
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> file=`ls`</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $file</span>
clsn_test.sh panduan.sh quanju.sh yhk.sh</pre>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">使用</span><span style="background: rgba(255, 255, 0, 1)">${}</span> </span></em></strong></p>
<p style="text-indent: 21pt"><span style="font-family: 等线">打印变量的时候防止出现“金庸新著”的问题</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> time=`date`</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $time_day</span>
<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${time}_day</span>
2017年 12月 05日 星期二 09:02:06<span style="color: rgba(0, 0, 0, 1)"> CST_day
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $time-day</span>
2017年 12月 05日 星期二 09:02:06 CST-day</pre>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">编写脚本测试</span><span style="background: rgba(0, 255, 0, 1)">${}</span></span></em></strong></p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 使用脚本测试</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim bianliang.sh </span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: bianliang.sh</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-05 09:10:29</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span>
<span style="color: rgba(0, 0, 0, 1)">   
time</span>=<span style="color: rgba(0, 0, 0, 1)">`date`
echo $timeday
echo ${time}day
   
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> shbianliang.sh </span>
<span style="color: rgba(0, 0, 0, 1)">   
2017年 12月 05日 星期二 0</span>9:11:19 CSTday</pre>
</div>
<h3>1.4.9 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">定义变量名技巧</span></h3>
<p style="margin-left: 42pt; text-indent: -21pt">1.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">变量名只能为字母、数字或下划线,只能以字母或下划线开头。</span></p>
<p style="margin-left: 42pt; text-indent: -21pt">2.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">变量名的定义要有一定的规范,并且要见名知意。</span></p>
<p><span style="font-family: 等线">示例:</span></p>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p style="text-indent: 35.4pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">ClsnAge=22&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;#&lt;==<span style="font-family: &quot;微软雅黑&quot;, sans-serif">每个单词的首字母大写的写法</span></p>
<p style="text-indent: 35.4pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">clsn_age=22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #&lt;==<span style="font-family: &quot;微软雅黑&quot;, sans-serif">单词之间用</span>"_"<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的写法</span></p>
<p style="text-indent: 35.4pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">clsnAgeSex=man&nbsp; &nbsp;#&lt;==<span style="font-family: &quot;微软雅黑&quot;, sans-serif; background: rgba(0, 255, 0, 1)">驼峰语法</span><span style="font-family: &quot;微软雅黑&quot;, sans-serif">:首个单词的首字母小写,其余单词首字母大写</span></p>
<p style="text-indent: 35.4pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">CLSNAGE=22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #&lt;==<span style="font-family: &quot;微软雅黑&quot;, sans-serif">单词全大写的写法</span></p>
</div>
<p style="margin-left: 42pt; text-indent: -21pt">3.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">一般的变量定义、赋值常用双引号;简单连续的字符串可以不加引号;希望原样输出时使用单引号。</span></p>
<p style="margin-left: 42pt; text-indent: -21pt">4.&nbsp;<span style="font-family: &quot;微软雅黑&quot;, sans-serif">希望变量的内容是命令的解析结果时,要用反引号</span>''<span style="font-family: &quot;微软雅黑&quot;, sans-serif">,或者用</span>$()<span style="font-family: &quot;微软雅黑&quot;, sans-serif">把命令括起来再赋值。</span></p>
<h2>1.5 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">特殊变量</span></h2>
<h3>1.5.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">位置变量</span></h3>
<p><span style="font-family: 等线">常用的特殊位置参数说明</span></p>
<table style="width: 100%; border-collapse: collapse; border-style: none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 14.88%; border-top: 1pt solid rgba(91, 155, 213, 1); border-bottom: 1pt solid rgba(91, 155, 213, 1); border-left: 1pt solid rgba(91, 155, 213, 1); border-right: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: 宋体">位置变量</span></strong></p>
</td>
<td style="width: 85.12%; border-top: 1pt solid rgba(91, 155, 213, 1); border-right: 1pt solid rgba(91, 155, 213, 1); border-bottom: 1pt solid rgba(91, 155, 213, 1); border-left: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: 宋体">作用说明</span></strong></p>
</td>
</tr>
<tr>
<td style="width: 14.88%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$0</span></strong></p>
</td>
<td style="width: 85.12%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">获取当前执行的</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">shell</span><span style="font-size: 10pt; font-family: 宋体">脚本的文件名,如果执行脚本带路径那么就包括脚本路径。</span></p>
</td>
</tr>
<tr>
<td style="width: 14.88%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$n</span></strong></p>
</td>
<td style="width: 85.12%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">获取当前执行的</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">shell</span><span style="font-size: 10pt; font-family: 宋体">脚本的第</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">n</span><span style="font-size: 10pt; font-family: 宋体">个参数值,</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">n=1..9</span><span style="font-size: 10pt; font-family: 宋体">,当</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">n</span><span style="font-size: 10pt; font-family: 宋体">为</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">0</span><span style="font-size: 10pt; font-family: 宋体">时表示脚本的文件名,如果</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">n</span><span style="font-size: 10pt; font-family: 宋体">大于</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">9</span><span style="font-size: 10pt; font-family: 宋体">用大括号括起来</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">{10}</span><span style="font-size: 10pt; font-family: 宋体">,参数以空格隔开。</span></p>
</td>
</tr>
<tr>
<td style="width: 14.88%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$#</span></strong></p>
</td>
<td style="width: 85.12%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">获取当前执行的</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">shell</span><span style="font-size: 10pt; font-family: 宋体">脚本后面接的参数的总个数</span></p>
</td>
</tr>
<tr>
<td style="width: 14.88%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$*</span></strong></p>
</td>
<td style="width: 85.12%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">获取当前</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">shell</span><span style="font-size: 10pt; font-family: 宋体">的所有传参的参数,不加引号同</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$@;</span><span style="font-size: 10pt; font-family: 宋体">如果给</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$*</span><span style="font-size: 10pt; font-family: 宋体">加上双引号,例如</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">: </span><span style="font-size: 10pt; font-family: 宋体">“</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$*</span><span style="font-size: 10pt; font-family: 宋体">”</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">,</span><span style="font-size: 10pt; font-family: 宋体">则表示将所有的参数视为单个字符串,相当于“</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$1$2$3</span><span style="font-size: 10pt; font-family: 宋体">”。</span></p>
</td>
</tr>
<tr>
<td style="width: 14.88%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$@</span></strong></p>
</td>
<td style="width: 85.12%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">获取当前</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">shell</span><span style="font-size: 10pt; font-family: 宋体">的所有传参的参数,不加引号同</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$*;</span><span style="font-size: 10pt; font-family: 宋体">如果给</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$@</span><span style="font-size: 10pt; font-family: 宋体">加上双引号,例如</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">: </span><span style="font-size: 10pt; font-family: 宋体">“</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$@</span><span style="font-size: 10pt; font-family: 宋体">”</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">,</span><span style="font-size: 10pt; font-family: 宋体">则表示将所有参数视为不同的独立字符串,相当于“</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$1</span><span style="font-size: 10pt; font-family: 宋体">”</span> <span style="font-size: 10pt; font-family: 宋体">“</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$2</span><span style="font-size: 10pt; font-family: 宋体">”</span> <span style="font-size: 10pt; font-family: 宋体">“</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$3</span><span style="font-size: 10pt; font-family: 宋体">”</span> <span style="font-size: 10pt; font-family: 宋体">“……”,这是将参数传递给其他程序的最佳方式,因为他会保留所有内嵌在每个参数里的任何空白。</span></p>
</td>
</tr>
<tr>
<td style="width: 100%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" colspan="2" width="100%">
<p style="margin-bottom: 0.0001pt"><span style="font-size: 10pt; font-family: 宋体">当“</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$*</span><span style="font-size: 10pt; font-family: 宋体">”和“</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$@</span><span style="font-size: 10pt; font-family: 宋体">”都加双引号时,两者有区别,都不加双引号时,两者无区别。</span></p>
</td>
</tr>
</tbody>
</table>
<p><strong><em><span style="text-decoration: underline"><span style="background: rgba(0, 255, 0, 1)">$0,$1.$2 ~ </span></span></em></strong><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">参数实践</span></span></em></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim chanshu.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################ </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: chanshu.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-05 09:39:16</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################ </span>
<span style="color: rgba(0, 0, 0, 1)">   
echo $0
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第一个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> $1<span style="color: rgba(0, 0, 0, 1)">
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第二个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> $2<span style="color: rgba(0, 0, 0, 1)">
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第11个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> ${11<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh chanshu.sh</span>
<span style="color: rgba(0, 0, 0, 1)">chanshu.sh
第一个参数:
第二个参数:
第11个参数:
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh chanshu.sh 1 2 3 4 5 6 7 8 9 10 11 </span>
<span style="color: rgba(0, 0, 0, 1)">chanshu.sh
第一个参数: </span>1<span style="color: rgba(0, 0, 0, 1)">
第二个参数: </span>2<span style="color: rgba(0, 0, 0, 1)">
第11个参数: </span>11</pre>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="background: rgba(0, 255, 0, 1)">$# </span></span></em></strong><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">参数实践</span></span></em></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim chanshu.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: chanshu.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-05 09:39:16</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span>
<span style="color: rgba(0, 0, 0, 1)">

echo $0
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第一个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> $1<span style="color: rgba(0, 0, 0, 1)">
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第二个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> $2<span style="color: rgba(0, 0, 0, 1)">
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第10个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> ${10<span style="color: rgba(0, 0, 0, 1)">}
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第11个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> ${11<span style="color: rgba(0, 0, 0, 1)">}
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">参数个数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> $<span style="color: rgba(0, 128, 0, 1)">#
</span><span style="color: rgba(0, 0, 0, 1)">


</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh chanshu.sh 55 2 3 4 5 6 7 8 9 10 11 112</span>
<span style="color: rgba(0, 0, 0, 1)">chanshu.sh
第一个参数: </span>55<span style="color: rgba(0, 0, 0, 1)">
第二个参数: </span>2<span style="color: rgba(0, 0, 0, 1)">
第10个参数: </span>10<span style="color: rgba(0, 0, 0, 1)">
第11个参数: </span>11<span style="color: rgba(0, 0, 0, 1)">
参数个数: </span>12</pre>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="background: rgba(0, 255, 0, 1)">$* </span></span></em></strong><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">参数实践</span></span></em></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim chanshu.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: chanshu.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-05 09:39:16</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span>
<span style="color: rgba(0, 0, 0, 1)">
echo $0
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第一个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> $1<span style="color: rgba(0, 0, 0, 1)">
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第二个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> $2<span style="color: rgba(0, 0, 0, 1)">
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第10个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> ${10<span style="color: rgba(0, 0, 0, 1)">}
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">第11个参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> ${11<span style="color: rgba(0, 0, 0, 1)">}
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">参数个数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> $<span style="color: rgba(0, 128, 0, 1)">#
</span>echo <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">参数:</span><span style="color: rgba(128, 0, 0, 1)">"</span> $*
<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">chanshu.sh</span><span style="color: rgba(128, 0, 0, 1)">"</span> 18L<span style="color: rgba(0, 0, 0, 1)">, 456C 已写入                                 
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh chanshu.sh 55 2 3 4 5 6 7 8 9 10 11 112</span>
<span style="color: rgba(0, 0, 0, 1)">chanshu.sh
第一个参数: </span>55<span style="color: rgba(0, 0, 0, 1)">
第二个参数: </span>2<span style="color: rgba(0, 0, 0, 1)">
第10个参数: </span>10<span style="color: rgba(0, 0, 0, 1)">
第11个参数: </span>11<span style="color: rgba(0, 0, 0, 1)">
参数个数: </span>12<span style="color: rgba(0, 0, 0, 1)">
参数: </span>55 2 3 4 5 6 7 8 9 10 11 112</pre>
</div>
<p><em><span style="text-decoration: underline"><span style="background: rgba(0, 255, 0, 1)">$* </span></span></em><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">与</span><span style="background: rgba(0, 255, 0, 1)"> $@ </span></span></em><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">对比实践</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> set -- "I am" handsome boy..</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $1</span>
<span style="color: rgba(0, 0, 0, 1)">I am
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $2</span>
<span style="color: rgba(0, 0, 0, 1)">handsome
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $3</span>
<span style="color: rgba(0, 0, 0, 1)">boy..
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $*</span>
<span style="color: rgba(0, 0, 0, 1)">I am handsome boy..
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $@</span>
<span style="color: rgba(0, 0, 0, 1)">I am handsome boy..

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> for i in $*;do echo $i ;done</span>
<span style="color: rgba(0, 0, 0, 1)">I
am
handsome
boy..
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> for i in $@;do echo $i ;done</span>
<span style="color: rgba(0, 0, 0, 1)">I
am
handsome
boy..
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> for i in "$@";do echo $i ;done</span>
<span style="color: rgba(0, 0, 0, 1)">I am
handsome
boy..
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> for i in "$*";do echo $i ;done</span>
I am handsome boy..</pre>
</div>
<h3>1.5.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">进程状态变量</span></h3>
<p>Shell<span style="font-family: 等线">进程的特殊状态变量说明</span></p>
<div align="center">
<table style="border-collapse: collapse; border-style: none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 70.65pt; border-top: 1pt solid rgba(91, 155, 213, 1); border-bottom: 1pt solid rgba(91, 155, 213, 1); border-left: 1pt solid rgba(91, 155, 213, 1); border-right: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" width="94">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: 宋体">位置变量</span></strong></p>
</td>
<td style="width: 452.15pt; border-top: 1pt solid rgba(91, 155, 213, 1); border-right: 1pt solid rgba(91, 155, 213, 1); border-bottom: 1pt solid rgba(91, 155, 213, 1); border-left: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" width="603">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: 宋体">作用说明</span></strong></p>
</td>
</tr>
<tr>
<td style="width: 70.65pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="94">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$?</span></strong></p>
</td>
<td style="width: 452.15pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="603">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">获取执行上一个指令的执行状态返回值(</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">0</span><span style="font-size: 10pt; font-family: 宋体">为成功,非零为失败</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">)</span><span style="font-size: 10pt; font-family: 宋体">,这个变量最常用</span></p>
</td>
</tr>
<tr>
<td style="width: 70.65pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="94">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$$</span></strong></p>
</td>
<td style="width: 452.15pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" width="603">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">获取当前执行的</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">Shell</span><span style="font-size: 10pt; font-family: 宋体">脚本的进程号(</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">PID)</span><span style="font-size: 10pt; font-family: 宋体">,这个变量不常用,了解即可</span></p>
</td>
</tr>
<tr>
<td style="width: 70.65pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="94">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$!</span></strong></p>
</td>
<td style="width: 452.15pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="603">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">获取上一个在后台工作的进程的进程号(</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">PID)</span><span style="font-size: 10pt; font-family: 宋体">,这个变量不常用,了解即可</span></p>
</td>
</tr>
<tr>
<td style="width: 70.65pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="94">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$_</span></strong></p>
</td>
<td style="width: 452.15pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" width="603">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">获取在此之前执行的命令或脚本的最后一个参数,这个变量不常用,了解即可</span></p>
</td>
</tr>
</tbody>
</table>
</div>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">进程参数实践</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $?</span>
<span style="color: rgba(0, 0, 0, 1)">0
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $$</span>
1368<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $!</span>
<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $_</span>
echo</pre>
</div>
<h3>1.5.3 echo<span style="font-family: &quot;微软雅黑&quot;, sans-serif">参数说明</span></h3>
<div align="center">
<table style="border-collapse: collapse; border-style: none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 176.95pt; border-top: 1pt solid rgba(91, 155, 213, 1); border-bottom: 1pt solid rgba(91, 155, 213, 1); border-left: 1pt solid rgba(91, 155, 213, 1); border-right: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" valign="top" width="236">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: 宋体">参数</span></strong></p>
</td>
<td style="width: 345.85pt; border-top: 1pt solid rgba(91, 155, 213, 1); border-right: 1pt solid rgba(91, 155, 213, 1); border-bottom: 1pt solid rgba(91, 155, 213, 1); border-left: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" valign="top" width="461">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: 宋体">参数说明</span></strong></p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="236">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">-n</span></strong></p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="461">
<p style="margin-bottom: 0.0001pt"><span style="font-size: 10pt; font-family: 宋体">不要追加换行</span></p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" valign="top" width="236">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">-e</span></strong></p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" valign="top" width="461">
<p style="margin-bottom: 0.0001pt"><span style="font-size: 10pt; font-family: 宋体">启用下列反斜杠转义的解释</span></p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="236">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">-E</span></strong></p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="461">
<p style="margin-bottom: 0.0001pt"><span style="font-size: 10pt; font-family: 宋体">显式地抑制对于反斜杠转义的解释</span></p>
</td>
</tr>
<tr>
<td style="width: 522.8pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" colspan="2" valign="top" width="697">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif; color: rgba(255, 255, 255, 1)">`echo' </span></strong><strong><span style="font-size: 10pt; font-family: 宋体">对下列反斜杠字符进行转义:</span></strong></p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="236">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">\n</span></strong></p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="461">
<p style="margin-bottom: 0.0001pt"><span style="font-size: 10pt; font-family: 宋体">换行</span></p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" valign="top" width="236">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">\r</span></strong></p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" valign="top" width="461">
<p style="margin-bottom: 0.0001pt"><span style="font-size: 10pt; font-family: 宋体">回车</span></p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="236">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">\t</span></strong></p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="461">
<p style="margin-bottom: 0.0001pt"><span style="font-size: 10pt; font-family: 宋体">横向制表符</span></p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" valign="top" width="236">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">\b</span></strong></p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" valign="top" width="461">
<p style="margin-bottom: 0.0001pt"><span style="font-size: 10pt; font-family: 宋体">退格</span></p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="236">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">\v</span></strong></p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="461">
<p style="margin-bottom: 0.0001pt"><span style="font-size: 10pt; font-family: 宋体">纵向制表符</span></p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" valign="top" width="236">
<p style="margin-bottom: 0.0001pt"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">\c</span></strong></p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" valign="top" width="461">
<p style="margin-bottom: 0.0001pt"><span style="font-size: 10pt; font-family: 宋体">抑制更多的输出</span></p>
</td>
</tr>
</tbody>
</table>
</div>
<h2>1.6 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">定义变量的方式</span></h2>
<h3>1.6.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">三种定义变量的方式</span></h3>
<p style="margin-left: 11pt">&nbsp;&nbsp;1<span style="font-family: 等线">、直接赋值</span></p>
<p style="margin-left: 11pt">&nbsp; 2<span style="font-family: 等线">、传参</span> (<span style="font-family: 等线">传递参数</span>)</p>
<p style="margin-left: 11pt">&nbsp; 3<span style="font-family: 等线">、交互式设置变量,使用</span>read<span style="font-family: 等线">命令</span></p>
<h3>1.6.2 read<span style="font-family: &quot;微软雅黑&quot;, sans-serif">命令说明</span></h3>
<p><span style="font-family: 等线">在命令行中使用</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> read</span>
132<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $REPLY </span>
132<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> read clsn </span>
456<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $clsn</span>
456<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $REPLY </span>
132</pre>
</div>
<p><span style="font-family: 等线">在脚本中使用</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim clsn_test.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span>
read -p <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">请输入:</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">clsn

echo $clsn</span></pre>
</div>
<p><span style="font-family: 等线">执行结果</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh clsn_test.sh </span>
<span style="color: rgba(0, 0, 0, 1)">请输入:clsn_znix
clsn_znix</span></pre>
</div>
<p>read<span style="font-family: 等线">命令的帮助说明</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> read --help</span>
-bash: read: --<span style="color: rgba(0, 0, 0, 1)">: 无效选项
read: 用法:read [</span>-ers] [-a 数组] [-d 分隔符] [-i 缓冲区文字] [-n 读取字符数] [-N 读取字符数] [-p 提示符] [-t 超时] [-u 文件描述符] [-s不显示终端的任何输入] [名称 ...]</pre>
</div>
<h3>1.6.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">定义方法实践</span></h3>
<p><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">直接赋值方法</span></span></em></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim bianliang.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: bianliang.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-05 09:10:29</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">#############################################################</span>
name=<span style="color: rgba(0, 0, 0, 1)">CLSN
age</span>=22<span style="color: rgba(0, 0, 0, 1)">
sex</span>=<span style="color: rgba(0, 0, 0, 1)">Man
hobby</span>=<span style="color: rgba(0, 0, 0, 1)">`ls`
ethFile</span>=/etc/sysconfig/network-scripts/ifcfg-<span style="color: rgba(0, 0, 0, 1)">eth0

echo $hobby
ls $ethFile
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> shbianliang.sh</span>
<span style="color: rgba(0, 0, 0, 1)">bianliang.sh chanshu.sh clsn.sh clsn_test.sh panduan.sh quanju.sh xiugaizhuji.sh yhk.sh
</span>/etc/sysconfig/network-scripts/ifcfg-eth0</pre>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">传参</span><span style="background: rgba(255, 255, 0, 1)"> (</span></span></em></strong><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">传递参数</span><span style="background: rgba(255, 255, 0, 1)">)</span></span></em></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim bianliang.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">#############################################################</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: bianliang.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-05 09:10:29</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">#############################################################</span>
name=<span style="color: rgba(0, 0, 0, 1)">CLSN
age</span>=22<span style="color: rgba(0, 0, 0, 1)">
sex</span>=<span style="color: rgba(0, 0, 0, 1)">Man
hobby</span>=$1<span style="color: rgba(0, 0, 0, 1)">
ethFile</span>=$2<span style="color: rgba(0, 0, 0, 1)">

echo $hobby
ls $ethFile
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> shbianliang.shclsn /etc/hostname </span>
<span style="color: rgba(0, 0, 0, 1)">clsn
</span>/etc/hostname</pre>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">交互式设置变量</span><span style="background: rgba(255, 255, 0, 1)"> read</span></span></em></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim yhk.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">#############################################################</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: yhk.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-04 17:01:44</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">#############################################################</span>
read -p<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">请输入你的银行卡号:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">Yhk
read </span>-s -p<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">请输入密码:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> miMa
echo
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">你的银行卡号:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">$Yhk
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">你的密码为:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> $miMa
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 测试结果</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> shyhk.sh </span>
请输入你的银行卡号:123456<span style="color: rgba(0, 0, 0, 1)">
请输入密码:
你的银行卡号: </span>123456<span style="color: rgba(0, 0, 0, 1)">
你的密码为: </span>123456</pre>
</div>
<h3>1.6.4 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">写一个交互脚本,实现能够定义主机名及</span>IP<span style="font-family: &quot;微软雅黑&quot;, sans-serif">地址</span></h3>
<p><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">脚本内容</span></span></em></strong><strong><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">↓</span></span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat xiugaizhuji.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: jiaohu.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-05 10:55:42</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span>
<span style="color: rgba(0, 0, 0, 1)">
ethFile</span>=/etc/sysconfig/network-scripts/ifcfg-eth
Now_eth</span>=`hostname -I|awk -F <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">[. ]+</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">{print $4}</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">`

read </span>-p <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">请输入主机名:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> Hostname
read </span>-p <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">请输入IP地址的主机位:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> HostIP

hostnamectl set</span>-<span style="color: rgba(0, 0, 0, 1)">hostname$Hostname

sed</span>-i <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">s#${Now_eth}#${HostIP}#g</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> $ethFile

read </span>-p <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">是否重启服务器:{yes/no}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">REboot

</span><span style="color: rgba(0, 0, 255, 1)">if</span> [ $REboot ==<span style="color: rgba(0, 0, 0, 1)"> yes ]
then
   echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">系统将在10秒后重启!</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
   shutdown </span>-r10
<span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
   echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">请稍后手动重启系统!</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
fi</span></pre>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">脚本测试结果</span></span></em></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh xiugaizhuji.sh</span>
<span style="color: rgba(0, 0, 0, 1)">请输入主机名:clsn
请输入IP地址的主机位:</span>180<span style="color: rgba(0, 0, 0, 1)">
是否重启服务器:{yes</span>/<span style="color: rgba(0, 0, 0, 1)">no}yes
系统将在10秒后重启!
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh xiugaizhuji.sh</span>
<span style="color: rgba(0, 0, 0, 1)">请输入主机名:clsn
请输入IP地址的主机位:</span>180<span style="color: rgba(0, 0, 0, 1)">
是否重启服务器:{yes</span>/<span style="color: rgba(0, 0, 0, 1)">no}no
请稍后手动重启!</span></pre>
</div>
<h2>1.7 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">变量的子串</span></h2>
<h3>1.7.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">变量子串说明</span></h3>
<table style="width: 100%; border-collapse: collapse; border-style: none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 39.26%; border-top: 1pt solid rgba(91, 155, 213, 1); border-bottom: 1pt solid rgba(91, 155, 213, 1); border-left: 1pt solid rgba(91, 155, 213, 1); border-right: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" valign="top" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: 宋体">表达式</span></strong></p>
</td>
<td style="width: 60.74%; border-top: 1pt solid rgba(91, 155, 213, 1); border-right: 1pt solid rgba(91, 155, 213, 1); border-bottom: 1pt solid rgba(91, 155, 213, 1); border-left: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: 宋体">说明</span></strong></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter}</span></strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">返回变量</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">$parameter</span><span style="font-size: 10pt; font-family: 宋体">的内容</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${#parameter}</span></strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">返回变内容的长度(按字符</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif; background: rgba(255, 255, 0, 1)">)</span><span style="font-size: 10pt; font-family: 宋体">,也适用于特殊变量</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameterioffset}</span></strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">在变量</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter}</span><span style="font-size: 10pt; font-family: 宋体">中,从位置</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">offset</span><span style="font-size: 10pt; font-family: 宋体">之后开始提取子串到结尾</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter:offset:length}</span></strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">在变量</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter}</span><span style="font-size: 10pt; font-family: 宋体">中,从位置</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">offset</span><span style="font-size: 10pt; font-family: 宋体">之后开始提取长度为</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">length</span><span style="font-size: 10pt; font-family: 宋体">的子串</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter#word}</span></strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">从变量</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter}</span><span style="font-size: 10pt; font-family: 宋体">开头开始删除最短匹配的</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">word</span><span style="font-size: 10pt; font-family: 宋体">子串</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter##word}</span></strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">从变量</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter}</span><span style="font-size: 10pt; font-family: 宋体">开头开始删除最长匹配的</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">word</span><span style="font-size: 10pt; font-family: 宋体">子串</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter%word}</span></strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">从变量</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter}</span><span style="font-size: 10pt; font-family: 宋体">结尾开始删除最短匹配的</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">word</span><span style="font-size: 10pt; font-family: 宋体">子串</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter%%word}</span></strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">从变量</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter}</span><span style="font-size: 10pt; font-family: 宋体">结尾开始删除最长匹配的</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">word</span><span style="font-size: 10pt; font-family: 宋体">子串</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter/pattem/string}</span></strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">使用</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">string</span><span style="font-size: 10pt; font-family: 宋体">代替第一个匹配的</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">pattern</span></p>
</td>
</tr>
<tr>
<td style="width: 39.26%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="39%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter//pattem/string}</span></strong></p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" valign="top" width="60%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">使用</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">string</span><span style="font-size: 10pt; font-family: 宋体">代替所有匹配的</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">pattern</span></p>
</td>
</tr>
</tbody>
</table>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">计算变赋值的长度</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> clsn=http://blog.znix.top</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn} |wc -L </span>
20<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${#clsn} </span>
20<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> time echo ${clsn} |wc -L </span>
20<span style="color: rgba(0, 0, 0, 1)">

real    0m0.002s
user    0m0.002s
sys    0m0.000s
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> time echo ${#clsn} </span>
20<span style="color: rgba(0, 0, 0, 1)">

real    0m0.000s
user    0m0.000s
sys    0m0.000s</span></pre>
</div>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">截取变量中的字符</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> clsn=abcABC123ABCabc</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn#abc}</span>
<span style="color: rgba(0, 0, 0, 1)">ABC123ABCabc
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn##abc}</span>
<span style="color: rgba(0, 0, 0, 1)">ABC123ABCabc
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn%abc}</span>
<span style="color: rgba(0, 0, 0, 1)">abcABC123ABC
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn%%abc}</span>
<span style="color: rgba(0, 0, 0, 1)">abcABC123ABC
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn#a*c}</span>
<span style="color: rgba(0, 0, 0, 1)">ABC123ABCabc
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn##a*c}</span>
<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn%a*c}</span>
<span style="color: rgba(0, 0, 0, 1)">abcABC123ABC
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn%%a*c}</span>
<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn#a*C}</span>
<span style="color: rgba(0, 0, 0, 1)">123ABCabc
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn#a*C}</span>
<span style="color: rgba(0, 0, 0, 1)">123ABCabc
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn##a*C}</span>
<span style="color: rgba(0, 0, 0, 1)">abc
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn%a*c}</span>
<span style="color: rgba(0, 0, 0, 1)">abcABC123ABC
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn%A*c}</span>
<span style="color: rgba(0, 0, 0, 1)">abcABC123
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn%%A*c}</span>
abc</pre>
</div>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">替换变量内容</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $clsn</span>
<span style="color: rgba(0, 0, 0, 1)">abcABC123ABCabc
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn/abc/clsn}</span>
<span style="color: rgba(0, 0, 0, 1)">clsnABC123ABCabc
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo ${clsn//abc/clsn}</span>
clsnABC123ABCclsn</pre>
</div>
<p><strong><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(0, 255, 0, 1)">有关上述匹配删除的小结</span></span></em></strong></p>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p>#<span style="font-family: &quot;微软雅黑&quot;, sans-serif">表示从幵头删除匹配最短。</span></p>
<p>##<span style="font-family: &quot;微软雅黑&quot;, sans-serif">表示从开头删除匹配最长。</span></p>
<p>%<span style="font-family: &quot;微软雅黑&quot;, sans-serif">表示从结尾删除匹配最短。</span></p>
<p>%%<span style="font-family: &quot;微软雅黑&quot;, sans-serif">表示从结尾删除匹配最长。</span></p>
<p>a*c<span style="font-family: &quot;微软雅黑&quot;, sans-serif">表示匹配的突符串,</span>*<span style="font-family: &quot;微软雅黑&quot;, sans-serif">表示匹配所有,</span>a*c<span style="font-family: &quot;微软雅黑&quot;, sans-serif">匹配开头为</span>a<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、中间为任意多个字符、结尾为</span>c<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的字符串。</span></p>
<p>a*C<span style="font-family: &quot;微软雅黑&quot;, sans-serif">表示匹配的字符串,</span>*<span style="font-family: &quot;微软雅黑&quot;, sans-serif">表示匹配所有,</span>a*C<span style="font-family: &quot;微软雅黑&quot;, sans-serif">匹配开头为</span>a<span style="font-family: &quot;微软雅黑&quot;, sans-serif">、中间为任意多个字符、结尾为</span>C<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的字符串。</span></p>
</div>
<p style="text-indent: 21pt"><span style="font-family: 等线">有关替换的小结</span></p>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">一个“</span>/<span style="font-family: &quot;微软雅黑&quot;, sans-serif">”表示替换匹配的第</span>-<span style="font-family: &quot;微软雅黑&quot;, sans-serif">个字符串。</span></p>
<p><span style="font-family: &quot;微软雅黑&quot;, sans-serif">两个“</span>/<span style="font-family: &quot;微软雅黑&quot;, sans-serif">”表示替换匹配的所有字符串。</span></p>
</div>
<h3>1.7.2 Shell<span style="font-family: &quot;微软雅黑&quot;, sans-serif">的特殊扩展变量说明</span></h3>
<table style="width: 100%; border-collapse: collapse; border-style: none" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 14.58%; border-top: 1pt solid rgba(91, 155, 213, 1); border-bottom: 1pt solid rgba(91, 155, 213, 1); border-left: 1pt solid rgba(91, 155, 213, 1); border-right: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: 宋体">表达式</span></strong></p>
</td>
<td style="width: 85.42%; border-top: 1pt solid rgba(91, 155, 213, 1); border-right: 1pt solid rgba(91, 155, 213, 1); border-bottom: 1pt solid rgba(91, 155, 213, 1); border-left: none; background: rgba(91, 155, 213, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: 宋体">说</span></strong><strong><span style="font-size: 10pt; font-family: 宋体">明</span></strong></p>
</td>
</tr>
<tr>
<td style="width: 14.58%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter:-word}</span></strong></p>
</td>
<td style="width: 85.42%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">如果</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">parameter</span><span style="font-size: 10pt; font-family: 宋体">的变量值为空或未赋值,则会返回</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">word</span><span style="font-size: 10pt; font-family: 宋体">字符串并替代变量的值用途</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">.</span><span style="font-size: 10pt; font-family: 宋体">如果变量未定义,则返回备用的值,防止变量为空值或因未定义而导致异常</span></p>
</td>
</tr>
<tr>
<td style="width: 14.58%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter:=word}</span></strong></p>
</td>
<td style="width: 85.42%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">如果</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">parameter</span><span style="font-size: 10pt; font-family: 宋体">的变量值为空或未赋值,则设置这个变量值为</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">word,</span><span style="font-size: 10pt; font-family: 宋体">并返回其值。位置变量和特殊变量不适用用途:基本同上一个</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter&gt;word}</span><span style="font-size: 10pt; font-family: 宋体">,但该变量又额外给</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">parameter</span><span style="font-size: 10pt; font-family: 宋体">变量赋值了</span></p>
</td>
</tr>
<tr>
<td style="width: 14.58%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter:?word}</span></strong></p>
</td>
<td style="width: 85.42%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); background: rgba(222, 234, 246, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">如果</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">parameter</span><span style="font-size: 10pt; font-family: 宋体">变量值为空或未赋值,那么</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">word</span><span style="font-size: 10pt; font-family: 宋体">字符串将被作为标准错误输出,否则输出变量的值。用途:用于捕捉由于变量未定义而导致的错误,并退出程序</span></p>
</td>
</tr>
<tr>
<td style="width: 14.58%; border-right: 1pt solid rgba(156, 194, 229, 1); border-bottom: 1pt solid rgba(156, 194, 229, 1); border-left: 1pt solid rgba(156, 194, 229, 1); border-top: none; padding: 0 5.4pt" width="14%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><strong><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">${parameter:+word}</span></strong></p>
</td>
<td style="width: 85.42%; border-top: none; border-left: none; border-bottom: 1pt solid rgba(156, 194, 229, 1); border-right: 1pt solid rgba(156, 194, 229, 1); padding: 0 5.4pt" width="85%">
<p style="margin-bottom: 0.0001pt; text-align: justify; text-justify: inter-ideograph"><span style="font-size: 10pt; font-family: 宋体">如果</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">parameter</span><span style="font-size: 10pt; font-family: 宋体">变量值为空或未赋值,则什么都不做,否则</span><span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;, serif">word</span><span style="font-size: 10pt; font-family: 宋体">字符串将替代变量的值</span></p>
</td>
</tr>
</tbody>
</table>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">特殊变量实践</span></span></em></p>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">脚本内容</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> catclsn.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: clsn.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-05 12:13:38</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span>
dir=<span style="color: rgba(0, 0, 0, 1)">
echo ${dir:</span>-/<span style="color: rgba(0, 0, 0, 1)">tmp}
echo ${dir}
echo ${dir:</span>=/<span style="color: rgba(0, 0, 0, 1)">mnt}
echo ${dir}
dir2</span>=<span style="color: rgba(0, 0, 0, 1)"> (空格)
echo ${dir2</span>-/<span style="color: rgba(0, 0, 0, 1)">tmp}
echo ${dir2}
echo ${dir2:</span>-/<span style="color: rgba(0, 0, 0, 1)">tmp}
echo ${dir2}
echo ${dir2</span>=/<span style="color: rgba(0, 0, 0, 1)">mnt}
echo ${dir2}</span></pre>
</div>
<p><span style="font-family: 等线">测试结果</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh clsn.sh </span>
/<span style="color: rgba(0, 0, 0, 1)">tmp

</span>/<span style="color: rgba(0, 0, 0, 1)">mnt
</span>/<span style="color: rgba(0, 0, 0, 1)">mnt


</span>/tmp</pre>
</div>
<p><span style="font-family: 等线">至此</span>shell<span style="font-family: 等线">中的变量就都介绍完了</span></p>
<h2>1.8 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">变量的数值计算</span></h2>
<h3>1.8.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">仅支持整数的运算</span></h3>
<p><strong><span style="background: rgba(255, 255, 0, 1)">echo $((</span></strong><strong><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">数学运算表达式</span><span style="background: rgba(255, 255, 0, 1)">))</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 形式一</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((1 + 1))</span>
2<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((2*7-3/6+5))</span>
19
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 形式二</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ((clsn=2*8))</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $clsn</span>
16
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 形式三</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> znix=$((2*7-3/6+5))</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $znix</span>
19</pre>
</div>
<p style="text-indent: 15.75pt"><strong><span style="font-family: 等线; color: rgba(255, 0, 0, 1)">延伸产物</span><span style="color: rgba(255, 0, 0, 1)">(</span></strong><strong><span style="font-family: 等线; color: rgba(255, 0, 0, 1)">重要</span><span style="color: rgba(255, 0, 0, 1)">)</span></strong></p>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p>i++ <span style="font-family: &quot;微软雅黑&quot;, sans-serif">自增</span>1</p>
<p>i-- <span style="font-family: &quot;微软雅黑&quot;, sans-serif">自减</span>1</p>
<p>++i</p>
<p>--i</p>
</div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em><span style="text-decoration: underline"><span style="font-family: 等线">示例:</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> i=1</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((i++))</span>
1<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((i++))</span>
2<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((i--))</span>
3<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((i--))</span>
2<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((i--))</span>
1<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((++i))</span>
1<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((++i))</span>
2<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((++i))</span>
3<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((--i))</span>
2<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((--i))</span>
1<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $((--i))</span>
0</pre>
</div>
<p><span style="font-family: 等线">记忆方法:</span>++<span style="font-family: 等线">,</span>--</p>
<p style="text-indent: 21pt"><span style="font-family: 等线">变量</span>a<span style="font-family: 等线">在前,表达式的值为</span>a<span style="font-family: 等线">,然后</span>a<span style="font-family: 等线">自增或自减,变量</span>a<span style="font-family: 等线">在符号后,表达式值自增或自减,然后</span>a<span style="font-family: 等线">值自增或自减。</span></p>
<p><strong><span style="background: rgba(255, 255, 0, 1)">let</span></strong><strong><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">命令</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> i=1</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> i=i+1</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $i</span>
i+1<span style="color: rgba(0, 0, 0, 1)">

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> i=1</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> let i=i+1</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $i</span>
2</pre>
</div>
<p><strong><span style="background: rgba(255, 255, 0, 1)">expr </span></strong><strong><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">命令</span></strong></p>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p style="text-indent: 1cm; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">1.<span style="font-family: &quot;微软雅黑&quot;, sans-serif">整数计算</span></p>
<p style="text-indent: 1cm; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">2.<span style="font-family: &quot;微软雅黑&quot;, sans-serif">判断扩展名</span></p>
<p style="text-indent: 1cm; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">3.<span style="font-family: &quot;微软雅黑&quot;, sans-serif">判断输入是否为整数,非整数返回值为</span>2</p>
<p style="text-indent: 1cm; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial">4.<span style="font-family: &quot;微软雅黑&quot;, sans-serif">计算变量的长度</span></p>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> expr 1+1</span>
1+1<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> expr 1 + 1</span>
2<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> expr 1 * 1</span>
<span style="color: rgba(0, 0, 0, 1)">expr: 语法错误
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> expr 1 \* 1</span>
1</pre>
</div>
<p><em><span style="font-family: 等线">非整数返回值为</span>2 </em><em><span style="font-family: 等线">示例:</span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> expr 1 + 1 </span>
2<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $?</span>
<span style="color: rgba(0, 0, 0, 1)">0
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> expr -1 + 1 </span>
<span style="color: rgba(0, 0, 0, 1)">0
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $?</span>
1<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> expr a + 1 </span>
<span style="color: rgba(0, 0, 0, 1)">expr: 非整数参数
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $?</span>
2</pre>
</div>
<p><strong><span style="background: rgba(255, 255, 0, 1)">$[]</span></strong><strong><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">运算符</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $</span>
3<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $</span>
-1<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $</span>
2<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $</span>
0</pre>
</div>
<p><strong><span style="background: rgba(255, 255, 0, 1)">typeset</span></strong><strong><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">命令进行运算</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> typeset -i A=2017 B=2018</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> A=A+B</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo $A</span>
4035</pre>
</div>
<h3>1.8.2 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">可以进行小数运算的命令</span></h3>
<p><strong><span style="background: rgba(255, 255, 0, 1)">bc&nbsp;&nbsp;&nbsp; </span></strong><strong><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">命令</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 安装 bc依赖于base源</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> yum -y install bc</span></pre>
</div>
<p>&nbsp;&nbsp;<span style="font-family: 等线">交互模式测试</span>bc<span style="font-family: 等线">命令</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> bc</span>
bc 1.06.95<span style="color: rgba(0, 0, 0, 1)">
Copyright </span>1991-1994, 1997, 1998, 2000, 2004, 2006<span style="color: rgba(0, 0, 0, 1)"> Free Software Foundation, Inc.
</span>1+1
2<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo 1+1.1|bc</span>
2.1</pre>
</div>
<p style="text-indent: 11pt"><span style="font-family: 等线">免交互模式测试</span>bc<span style="font-family: 等线">命令</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo 'scale=6;1/3'|bc</span>
.333333</pre>
</div>
<p><strong><span style="background: rgba(255, 255, 0, 1)">python </span></strong><strong><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">命令</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">file `which yum `</span>
/usr/bin/<span style="color: rgba(0, 0, 0, 1)">yum: Python script, ASCII text executable
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> python </span>
&gt;&gt;&gt; <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
</span>&gt;&gt;&gt; os.system(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">df -h</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span>&gt;&gt;&gt; 1+1.1
2.1
&gt;&gt;&gt;exit()</pre>
</div>
<p><strong><span style="background: rgba(255, 255, 0, 1)">awk </span></strong><strong><span style="font-family: 等线; background: rgba(255, 255, 0, 1)">命令</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo "7.7 3.8"|awk '{print ($1-$2)}'</span>
3.9<span style="color: rgba(0, 0, 0, 1)">
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo "358 113"|awk '{print ($1-3)/$2}'</span>
3.14159<span style="color: rgba(0, 0, 0, 1)">
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> echo "3 9"|awk '{print ($1+3)*$2}'</span>
54<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> awk BEGIN'{print 1.2+3.3}'</span>
4.5</pre>
</div>
<h3>1.8.3 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">运算相关练习题</span></h3>
<h4><strong>1.8.3.1&nbsp;</strong><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">【练习题】实现一个加减乘除等功能的计算器</span></strong></h4>
<p style="margin-left: 7.1pt"><em><span style="text-decoration: underline"><span style="font-family: 等线">实现脚本:</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> cat jishuanqi.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: jishuanqi.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-06 08:57:13</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span>
<span style="color: rgba(0, 0, 0, 1)">
read </span>-p <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">请输入第一个整数:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> a
read </span>-p <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">请输入第二个整数:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> b


echo $a </span>+ $b =$(($a+<span style="color: rgba(0, 0, 0, 1)">$b))
echo $a </span>- $b =$(($a-<span style="color: rgba(0, 0, 0, 1)">$b))
echo $a \</span>* $b =$(($a*<span style="color: rgba(0, 0, 0, 1)">$b))
echo $a </span>/ $b =$(($a/$b))</pre>
</div>
<p><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </em><em><span style="text-decoration: underline"><span style="font-family: 等线">脚本执行过程:</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh jishuanqi.sh </span>
请输入第一个整数:12<span style="color: rgba(0, 0, 0, 1)">
请输入第二个整数:</span>12
12 + 12 =24
12 - 12 =<span style="color: rgba(0, 0, 0, 1)">0
</span>12 * 12 =144
12 / 12 =1</pre>
</div>
<p><span style="font-family: 等线">精简方法</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim jishuanqi2.sh </span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: jishuanqi2.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-06 15:02:41</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span>
echo $(($1))</pre>
</div>
<p><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </em><em><span style="text-decoration: underline"><span style="font-family: 等线">脚本执行过程:</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh jishuanqi2.sh1+1</span>
2<span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> sh jishuanqi2.sh1*9</span>
9</pre>
</div>
<h4><strong>1.8.3.2&nbsp;</strong><strong><span style="font-family: &quot;微软雅黑&quot;, sans-serif">【练习题】打印结果</span>1+2+3+4+5+6+7+8+9+10=55</strong></h4>
<p style="margin-left: 7.1pt"><em><span style="text-decoration: underline"><span style="font-family: 等线">脚本内容</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> vim yunshuan.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> File Name: yunshuan.sh</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Version: V1.0</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Author: clsn</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Organization: http://blog.znix.top</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Created Time : 2017-12-06 09:40:31</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> Description:</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">############################################################</span>
<span style="color: rgba(0, 0, 0, 1)">
Num</span>=`seq -s + 1 10<span style="color: rgba(0, 0, 0, 1)">`
echo$Num</span>=$(($Num))</pre>
</div>
<p><em><span style="text-decoration: underline"><span style="font-family: 等线">脚本执行结果</span></span></em></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> shyunshuan.sh</span>
1+2+3+4+5+6+7+8+9+10=55</pre>
</div>
<h2>1.9 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">补充说明</span></h2>
<p style="text-indent: 11pt">shell<span style="font-family: 等线">脚本中批量注释的方法</span></p>
<div class="cnblogs_code">
<pre>&lt;&lt;<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">EOF</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
文件内容
EOF
   或
   使用 exit可以注释其之后的所有内容(类似注释,实质为不执行后面的内容)</span></pre>
</div>
<h2>1.9.1 <span style="font-family: &quot;微软雅黑&quot;, sans-serif">参考文献</span></h2>
<div style="border: 2pt solid rgba(0, 0, 0, 1); padding: 1pt 4pt; background: rgba(242, 242, 242, 1)">
<p>http://blog.csdn.net/lansesl2008/article/details/20558369</p>
<p>https://www.abcdocker.com/abcdocker/269</p>
<p>http://blog.51cto.com/life2death/1657133</p>
</div>
<p style="text-align: right">&nbsp;<strong>&nbsp;本文出自“惨绿少年”,欢迎转载,转载请注明出处!http://blog.znix.top</strong></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <div style="padding: 5px; background: #ff9; border: solid 1px #ccc">
作者:惨绿少年
<br>
出处:http://clsn.io
<br>
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
<br>
</div><br><br>
来源:https://www.cnblogs.com/clsn/p/7992981.html
頁: [1]
查看完整版本: Shell编程基础篇-上