继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

test

android零基础入门
关注TA
已关注
手记 288
粉丝 97
获赞 603

<!DOCTYPE html>

<html><head>

<meta http-equiv="Content-Type" content="text/html; charset=unicode">

<!--defaultCSS-->

<title>_1_1bash变量_交互_逻辑.md</title>






<style type="text/css" id="wiz_todo_style_id" wiz_link_version="01.00.09">.wiz-todo, .wiz-todo-img {width: 16px; height: 16px; cursor: default; padding: 0 10px 0 2px; vertical-align: -10%;-webkit-user-select: none;} .wiz-todo-label { display: inline-block; padding-top: 7px; padding-bottom: 6px; line-height: 1.5;} .wiz-todo-label-checked {  color: #666;} .wiz-todo-label-unchecked {text-decoration: initial;} .wiz-todo-completed-info {padding-left: 44px; display: inline-block; } .wiz-todo-avatar { width:20px; height: 20px; vertical-align: -20%; margin-right:10px; border-radius: 2px;} .wiz-todo-account, .wiz-todo-dt { color: #666; }</style>
















<style id="wiz_custom_css">        html, body {            font-size: 15px;        }        body {            font-family: Helvetica, 'Hiragino Sans GB', '微软雅黑', 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif;            line-height: 1.6;            margin: 0;            padding: 20px 36px;            padding: 1.33rem 2.4rem;        }        h1, h2, h3, h4, h5, h6 {            margin: 20px 0 10px;            margin: 1.33rem 0 0.667rem;            padding: 0;            font-weight: bold;        }        h1 {            font-size: 21px;            font-size: 1.4rem;        }        h2 {            font-size: 20px;            font-size: 1.33rem;        }        h3 {            font-size: 18px;            font-size: 1.2rem;        }        h4 {            font-size: 17px;            font-size: 1.13rem;        }        h5 {            font-size: 15px;            font-size: 1rem;        }        h6 {            font-size: 15px;            font-size: 1rem;            color: #777777;            margin: 1rem 0;        }        div, p, ul, ol, dl, li {            margin: 0;        }        blockquote, table, pre, code {            margin: 8px 0;        }        ul, ol {            padding-left: 32px;            padding-left: 2.13rem;        }        blockquote {            padding: 0 12px;            padding: 0 0.8rem;        }        blockquote > :first-child {            margin-top: 0;        }        blockquote > :last-child {            margin-bottom: 0;        }        img {            border: 0;            max-width: 100%;            height: auto !important;            margin: 2px 0;        }        table {            border-collapse: collapse;            border: 1px solid #bbbbbb;        }        td {            padding: 4px 8px;            border-collapse: collapse;            border: 1px solid #bbbbbb;        }        @media screen and (max-width: 660px) {            body {                padding: 20px 18px;                padding: 1.33rem 1.2rem;            }        }        @media only screen and (-webkit-max-device-width: 1024px), only screen and (-o-max-device-width: 1024px), only screen and (max-device-width: 1024px), only screen and (-webkit-min-device-pixel-ratio: 3), only screen and (-o-min-device-pixel-ratio: 3), only screen and (min-device-pixel-ratio: 3) {            html, body {                font-size: 17px;            }            body {                line-height: 1.7;                padding: 0.75rem 0.9375rem;                color: #353c47;            }            h1 {                font-size: 2.125rem;            }            h2 {                font-size: 1.875rem;            }            h3 {                font-size: 1.625rem;            }            h4 {                font-size: 1.375rem;            }            h5 {                font-size: 1.125rem;            }            h6 {                color: inherit;            }            ul, ol {                padding-left: 2.5rem;            }            blockquote {                padding: 0 0.9375rem;            }        }</style><style name="wiz_tmp_editor_style">html, body {    font-size: 15px;}body {    font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif;    line-height: 1.6;    color: ;    background-color: ;    margin: 0;    padding: 16px 20px;}h1, h2, h3, h4, h5, h6 {    margin: 20px 0 10px;    margin: 1.33rem 0 0.667rem;    padding: 0;    font-weight: bold;}h1 {    font-size: 21px;    font-size: 1.4rem;}h2 {    font-size: 20px;    font-size: 1.33rem;}h3 {    font-size: 18px;    font-size: 1.2rem;}h4 {    font-size: 17px;    font-size: 1.13rem;}h5 {    font-size: 15px;    font-size: 1rem;}h6 {    font-size: 15px;    font-size: 1rem;    color: #777777;    margin: 1rem 0;}div, p, ul, ol, dl, li {    margin: 0;}blockquote, table, pre, code{    margin: 8px 0;}ul, ol {    padding-left: 32px;    padding-left: 2.13rem;}blockquote {    border-left: 4px solid #dddddd;    padding: 0 12px;    padding: 0 0.8rem;}blockquote > :first-child {    margin-top: 0;}blockquote > :last-child {    margin-bottom: 0;}img {    border: 0;    max-width: 100%;    height: auto !important;    margin: 2px 0;}table {    border-collapse: collapse;    border: 1px solid #bbbbbb;}td {    padding:4px 8px;    border-collapse: collapse;    border: 1px solid #bbbbbb;}@media screen and (max-width: 660px) {    body {        padding: 20px 18px;        padding: 1.33rem 1.2rem;    }}@media only screen and (-webkit-max-device-width: 1024px), only screen and (-o-max-device-width: 1024px), only screen and (max-device-width: 1024px), only screen and (-webkit-min-device-pixel-ratio: 3), only screen and (-o-min-device-pixel-ratio: 3), only screen and (min-device-pixel-ratio: 3) {    html, body {        font-size: 17px;    }    body {        line-height: 1.7;        padding: 0.75rem 0.9375rem;        color: #353c47;    }    h1 {        font-size: 2.125rem;    }    h2 {        font-size: 1.875rem;    }    h3 {        font-size: 1.625rem;    }    h4 {        font-size: 1.375rem;    }    h5 {        font-size: 1.125rem;    }    h6 {        color: inherit;    }    ul, ol {        padding-left: 2.5rem;    }    blockquote {        padding: 0 0.9375rem;    }}</style><link rel="stylesheet" type="text/css" href="file:///F:\Program Files (x86)\WizNote\WizTools\htmleditor\editor.css" wiz_ charset="utf-8"><link rel="stylesheet" type="text/css" href="file:///F:\Program Files (x86)\WizNote\WizTools\htmleditor\scrollbar.css" wiz_ charset="utf-8"><script type="text/javascript" src="file:///F:\Program Files (x86)\WizNote\WizTools\htmleditor\utils.js" wiz_ charset="utf-8"></script><script type="text/javascript" src="file:///F:\Program Files (x86)\WizNote\WizTools\htmleditor\localize.js" wiz_ charset="utf-8"></script><script type="text/javascript" src="file:///F:\Program Files (x86)\WizNote\WizTools\htmleditor\core\wizEditor.js" wiz_ charset="utf-8"></script><script type="text/javascript" src="file:///F:\Program Files (x86)\WizNote\WizTools\htmleditor\core\checklist\todo.js" wiz_ charset="utf-8"></script><script type="text/javascript" src="file:///F:\Program Files (x86)\WizNote\WizTools\htmleditor\core\imageresize\dragresize.js" wiz_ charset="utf-8"></script><script type="text/javascript" src="file:///F:\Program Files (x86)\WizNote\WizTools\htmleditor\editorHelper.js" wiz_ charset="utf-8"></script></head>


<body  contenteditable="true"><div >##变量</div><div ><span >变量可以由字母数字下划线组成,数字不打头。</span></div><div ><span >bash为弱类型语言,</span><span >变量</span><span >默认类型是字符串型。如果要进行数值运算,则需要指定类型。</span></div><div >变量需要=连接,=左右没有空格,若变量的值有空格用引号。括起来。</div><div >变量可以叠加`echo $aa &nbsp;aa="$aa"456 ` aa后再添456也可以`aa=${aa}456`</div><div ><span >环境变量的名称建议写大写。</span></div><div ><font face="微软雅黑">变量引用` ${name}, $name`</font></div><div ><span >###bash变量的类型</span></div><div ><font face="微软雅黑">本地变量:作用域仅为当前shell进程,就是用户自定义shell;</font></div><div >环境变量:作用域为当前shell进程及其子进程,主要和操作系统环境相关的数据。</div><div ><font face="微软雅黑">局部变量:作用域仅为某代码片断(函数上下文);</font></div><div ><font face="微软雅黑"><br></font></div><div >位置参数变量:当执行脚本的shell进程传递的参数,向脚本中传递参数或数据,变量名不能自定义,作用是固定的;</div><div >预定义变量:bash中已经定义好的变量,变量名不能自定义,变量作用固定。</div><div ><span >位参变量是预定义的一种。从上到下越来越严格。</span></div><div ><span >***</span></div><div ><span ><br></span></div><div >####本地变量</div><div >本地变量:</div><div >set &nbsp; &nbsp; &nbsp;//查看所有变量 &nbsp;unset删除变量。</div><div ><font face="微软雅黑">unset name</font><span >撤销变量 ,此处并不是引用。</span><br></div><div >***</div><div ><span >####环境变量</span><span >&nbsp;</span></div><div ><span >#####变量声明和赋值方法</span></div><div ><span >```</span></div><div ><span >1.export 变量名=变量值 &nbsp;//声明为全局变量。</span></div><div ><div><font face="微软雅黑">2.name=value</font></div><div><span >&nbsp; &nbsp;&nbsp;</span><font face="微软雅黑">export name</font></div><div><font face="微软雅黑"><div>3. declare -x name=value</div><div>4. name=value</div><div>declare -x name</div></font></div></div><div ><span >```</span></div><div ><span >##### 引用(同上)</span></div><div ><span >&nbsp;${na &nbsp; &nbsp; &nbsp; &nbsp;me}</span></div><div ><div><font face="微软雅黑">查看环境变量:`export, declare -x, printenv, env`</font></div><div><font face="微软雅黑">撤销环境变量:`unset name`</font></div></div><div ><span >在当前shell和子shell中生效,写入配置文件中对所有shell生效。</span></div><div ><span >父shell的环境下,新建shell为子shell。直接输xxshell</span></div><div ><span >&nbsp; &nbsp;`&nbsp;</span><span >pstree &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//确定shell位置` &nbsp;&nbsp;</span></div><div ><span >&nbsp; &nbsp; `</span>env &nbsp;变量名 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//查询环境变量`</div><div ><span >&nbsp; &nbsp;`&nbsp;</span>unset &nbsp;变量名 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//删除变`</div><div ><div><font face="微软雅黑">注意:bash内嵌了许多环境变量(通常为全大写字符),用于定义bash的工作环境</font>:<span >PATH, HISTFILE, HISTSIZE, HISTFILESIZE, HISTCONTROL, SHELL, HOME, UID, PWD, OLDPWD</span></div></div><div >PATH:用冒号分割的路径,系统命令所在。</div><div ><span >自己的脚本不要拷到系统目录下,应该PATH="$PATH":/root &nbsp;追加&nbsp; &nbsp; &nbsp; &nbsp;</span></div><div >PS1;,系统定义提示符的变量,就是shell提示符,虽然不是环境变量,</div><div >***</div><div ><div><font face="微软雅黑">####只读变量</font></div><div><font face="微软雅黑">(1) declare -r name</font></div><div><font face="微软雅黑">(2) readonly name</font></div></div><div ><font face="微软雅黑">只读变量无法重新赋值,并且不支持撤销;存活时间为当前shell进程的生命周期,随shell进程终止而终止;</font></div><div >***</div><div >###bash算术运算</div><div ><span >注:(因为bash中默认类型为字符型,需要自行设定才可运算)</span></div><div >算术运算符:`+ &nbsp;- * / &nbsp;% **`<span ><br></span></div><div ><span >```</span><br></div><div ><span >&nbsp;declare&nbsp;[+或-] [选项] 变量名</span><br></div><div ><div>&nbsp;<span >&nbsp; &nbsp;</span><span >-</span><span >给变量设定类型属性 &nbsp;+取消变量的类型属性&nbsp;</span></div><div><span >&nbsp; &nbsp;&nbsp;</span><span >-i &nbsp;将变量声明为整型 &nbsp;&nbsp;</span></div><div>&nbsp; &nbsp; -x &nbsp; 将变量声明为环境变量 &nbsp;</div><div><span >&nbsp; &nbsp;&nbsp;</span><span >-p 显示指定变量被声明的类型</span></div><div>&nbsp;&nbsp;declare &nbsp;-i cc=$aa+$bb &nbsp;<br></div><div>```</div><div>####算术运算的方式:<span >&nbsp;&nbsp;</span></div><div><span >支持一般运算符,逻辑运算符</span></div><div>1. &nbsp;`<span >let VAR=$num1 or $num2` &nbsp;bash内建命令,通常用于运算</span><span >&nbsp;&nbsp;</span></div><div><span >2. &nbsp;`VAR=</span><span >$[</span><span >experssion</span><span >]`&nbsp;</span><span >(最常用)</span></div><div><span >3. `VAR=</span><span >$((</span><span >experssion</span><span >))`&nbsp;</span></div><div><span >4.&nbsp;</span><span >`VAR=$(expr a + b &nbsp;)` &nbsp; //此处的表达式需要空格分割</span><span >&nbsp;</span></div><div><span >例:&nbsp;</span></div><div><span >&nbsp;` ff=$(($aa+$bb))` &nbsp;//其中的空格有没有都行 ,</span><span >&nbsp;单括号括起来的是系统命令 &nbsp;</span><span >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></div></div><div ><span >&nbsp;`dd=$(expr $aa + $bb)` &nbsp; //把括号里的命令结果赋dd</span><br></div><div ><div><font face="微软雅黑">注意:有些时候乘法符号需要转义;</font><span > </span></div><div><font face="微软雅黑">####增强型赋值:</font></div><div><font face="微软雅黑">变量做某种算术运算后回存至此变量中;</font></div><div><font face="微软雅黑">```</font></div><div><font face="微软雅黑">`let i=$i+#` &nbsp;</font><span class="Apple-tab-span" > </span>`<span >let i+=#`</span></div><div><font face="微软雅黑">`+=,-=,*=, /=, %=`</font><span > </span></div><div><font face="微软雅黑">自增:`</font><font face="微软雅黑" >VAR=$[$VAR+1]` ;`</font><span >let &nbsp;VAR+=1 &nbsp; `;`</span><span >let &nbsp;VAR++`</span></div><div><font face="微软雅黑">自减:`</font><span >VAR=$[$VAR-1]`; ` &nbsp;</span><span >let &nbsp;VAR-=1`;`</span><span >let &nbsp;VAR--`</span></div></div><div >```</div><div >***</div><div >####条件测试</div><div ><div><font face="微软雅黑">条件测试:</font><span >&nbsp; &nbsp;&nbsp;</span><span >判断某需求是否满足,需要由测试机制来实现;</span></div><div><span >如何编写测试表达式以实现所需的测试:</span><br></div><div><span >&nbsp; &nbsp;&nbsp;</span><font face="微软雅黑">(1) 执行命令,并利用命令状态返回值来判断;</font></div><div><font face="微软雅黑"><span class="Apple-tab-span" > </span>0:成功&nbsp;</font><span class="Apple-tab-span" > </span><span >1-255:失败</span></div><div><span >&nbsp; &nbsp;&nbsp;</span><font face="微软雅黑">(2) 测试表达式</font></div><div><font face="微软雅黑"><span class="Apple-tab-span" > </span>test &nbsp;EXPRESSION;</font><span class="Apple-tab-span" > </span><span >[ EXPRESSION ];</span><span class="Apple-tab-span" > </span><span >` EXPRESSION `;</span></div><div><span >注意:EXPRESSION两端必须有空白字符,否则为语法错误;</span><span >上述命令并不会主动输出结果,需要输入`echo$?`来查看上一次判断结果.</span><br></div><div><span class="Apple-tab-span" ><font face="微软雅黑">*** </font></span><br></div><div><font face="微软雅黑">#####bash的测试类型:</font></div><div><font face="微软雅黑">数值测试;</font><span >字符串测试;</span><span >文件测试</span></div><div><font face="微软雅黑">数值测试</font></div><div><font face="微软雅黑">```</font></div><div><span ><div><span >&nbsp; &nbsp;&nbsp;</span>-eq:是否等于; [ $num1 -eq $num2 ]</div></span><span ><div><span >&nbsp; &nbsp; -</span>ne:是否不等于;</div></span><span ><div><span >&nbsp; &nbsp;&nbsp;</span>-gt:是否大于;</div></span><span ><div><span >&nbsp; &nbsp;&nbsp;</span>-ge:是否大于等于;</div></span><span ><div><span >&nbsp; &nbsp;&nbsp;</span>-lt:是否小于;</div></span><span ><div><span >&nbsp; &nbsp;&nbsp;</span>-le:是否小于等于;</div></span></div><div><span >```</span></div><div><span ><br></span></div><div><span ><div>字符串测试:</div><div>字符,变量比较的时候需要加引用;要使用[[]] &nbsp;否则会出现不可预期的情况。</div><div>```</div></span><span ><div><span >&nbsp; &nbsp;&nbsp;</span>==:是否等于;</div></span><span ><div><span >&nbsp; &nbsp;&nbsp;</span>&gt;:是否大于;</div></span><span ><div><span >&nbsp; &nbsp;&nbsp;</span>&lt;:是否小于;</div></span><span ><div><span >&nbsp; &nbsp;&nbsp;</span>!=:是否不等于;</div></span><span ><div><span >&nbsp; &nbsp;&nbsp;</span>=~:左侧字符串是否能够被右侧的PATTERN所匹配;</div><div><span class="Apple-tab-span" > </span></div></span><span ><div><span >&nbsp; &nbsp;&nbsp;</span>-z "STRING":判断指定的字串是否为空;空则为真,不空则假;</div></span><div ><span >&nbsp; &nbsp;</span><span >&nbsp;</span><span >-n "STRING":判断指定的字符串是否不空;不空则真,空则为假;</span></div><div ><span >```</span></div><div ><span ><br></span></div><div >文件测试</div><div >```</div><div ><div><span >&nbsp; &nbsp;&nbsp;</span>存在性测试</div><div><span >&nbsp; &nbsp;&nbsp;</span><span >&nbsp; &nbsp;&nbsp;</span><span >-a &nbsp;FILE</span></div><div><span >&nbsp; &nbsp;&nbsp;</span><span >&nbsp; &nbsp;&nbsp;</span><span >-e &nbsp;FILE &nbsp;</span><span >是否存在文件(是为真);</span></div><div>文件的存在性测试,存在则为真,否则则为假;</div><div>存在性及类型测试</div><div><span >&nbsp; &nbsp;&nbsp;</span>-b &nbsp;FILE:是否存在并且为 块设备 文件;</div><div><span >&nbsp; &nbsp;&nbsp;</span>-c &nbsp;FILE:是否存在并且为 字符设备 文件;</div><div><span >&nbsp; &nbsp;&nbsp;</span>-d &nbsp;FILE:是否存在并且为 目录文件;</div><div><span >&nbsp; &nbsp;&nbsp;</span>-f &nbsp;FILE:是否存在并且为 普通文件;</div><div><span >&nbsp; &nbsp;&nbsp;</span>-h &nbsp;FILE或 -L &nbsp;FILE:是否存在并且为 符号链接文件;</div><div><span >&nbsp; &nbsp;&nbsp;</span>-p FILE:是否存在且为 命名管道文件;</div><div><span >&nbsp; &nbsp;&nbsp;</span>-S &nbsp;FILE:是否存在且为 套接字文件;</div><div>文件权限测试:</div><div><span >&nbsp; &nbsp;&nbsp;</span>-r &nbsp;FILE:是否存在并且 对当前用户可读;</div><div><span >&nbsp; &nbsp;&nbsp;</span>-w &nbsp;FILE:是否存在并且 对当前用户可写;</div><div><span >&nbsp; &nbsp;&nbsp;</span>-x &nbsp;FILE:是否存在并且 对当前用户可执行;</div><div>特殊权限测试:</div><div><span >&nbsp; &nbsp;&nbsp;</span>-u &nbsp;FILE:是否存在并且 拥有suid权限;用不多</div><div><span >&nbsp; &nbsp;&nbsp;</span>-g &nbsp;FILE:是否存在并且 拥有sgid权限;<span >用不多&nbsp;</span></div><div><span >&nbsp; &nbsp;&nbsp;</span>-k &nbsp;FILE:是否存在并且 拥有sticky权限;<span >用不多</span></div><div>文件是否有内容:</div><div><span >&nbsp; &nbsp;&nbsp;</span>-s &nbsp;FILE:是否有内容;</div><div>时间戳:</div><div><span >&nbsp; &nbsp;&nbsp;</span>-N FILE:文件自从上一次读操作后是否被修改过;</div><div>从属关系测试:</div><div><span >&nbsp; &nbsp;&nbsp;</span>-O &nbsp;FILE:当前用户是否为文件的属主;</div><div><span >&nbsp; &nbsp;&nbsp;</span>-G &nbsp;FILE:当前用户是否属于文件的属组;</div><div>双目测试:</div><div><span >&nbsp; &nbsp;&nbsp;</span>FILE1 &nbsp;-ef &nbsp;FILE2:1与2是否指向同一个文件系统的相同inode的硬链接,<span >inode号一致</span><span >;</span></div><div><span >&nbsp; &nbsp;&nbsp;</span>FILE1 &nbsp;-nt &nbsp;FILE2:FILE1是否新于FILE2;</div><div><span >&nbsp; &nbsp;&nbsp;</span>FILE1 &nbsp;-ot &nbsp;FILE2:FILE1是否旧于FILE2;</div></div><div >```</div><div >***</div><div ><div>####逻辑组合运算:</div><div>这样的执行也反映在CLI中。逻辑优先级非&gt;与&gt;或.</div><div>第一种方式:</div><div>```</div><div>COMMAND1 &amp;&amp; COMMAND2<span >&nbsp; &nbsp; 1对2才执行</span></div><div>COMMAND1 || COMMAND2 &nbsp; &nbsp;<span >1不对2执行</span></div><div>! COMMAND&nbsp;</div><div><span >[ -O FILE ] &amp;&amp; [ -r FILE ]</span><br></div><div><span >命令1;命令2 &nbsp; &nbsp;先后执行</span><br></div><div><span >```</span></div><div><span >第二种方式:</span><br></div><div>```</div><div>EXPRESSION1 &nbsp;-a &nbsp;EXPRESSION2 &nbsp; &nbsp;<span >逻辑与,判断1和判断2都成立,结果才为真</span></div><div>EXPRESSION1 &nbsp;-o &nbsp;EXPRESSION2 &nbsp;&nbsp;<span >逻辑或, 判断1和判断2有一个为真就是真</span></div><div>! EXPRESSION &nbsp; &nbsp;<span >逻辑非,原判断式取反</span></div><div><span >[ -O FILE -a -x FILE ]</span><br></div></div><div ><span >命令1&amp;&amp; 命令2||命令3 &nbsp; 1对执行2,1不对,执行3</span><br></div><div><div >```</div><div >***</div><div><div><span >向脚本传递参数:</span><br></div><div><font face="微软雅黑"><span >位置参数变量</span></font><span >(只可以改动值,系统定义好的功能)</span></div><div><span >```</span></div><div><span >$n &nbsp; &nbsp;n为数字,$0代表命令本身,$1-$9代表第一到九个参数,十以上的参数需要大括号包含如${} &nbsp; &nbsp;</span><span >&nbsp;</span><br></div><div><span >$1...接受脚本外传入的参数。</span></div><div><span >轮替:`</span><span >shift &nbsp;[n]`:位置参数轮替;剔除前几个参数。</span></div></div><div ><div >```</div><div >特殊变量:</div><div >```</div><div >$* &nbsp; 所有参数,$*把所有参数看成一个整体,只会传入一次,一次全部传入。</div><div >$@ 所有参数,不过$@把每个参数区分对待,给几个传几次。</div><div >$# &nbsp; 所有参数的个数。 计算传入脚本的参数个数。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div >&nbsp;</div><div ><span >$$当前进程的PID</span></div><div >$!返回最后一个后台进程的PID</div><div ><span >``` &nbsp; &nbsp;&nbsp;</span></div></div><div >*** &nbsp; &nbsp; &nbsp; &nbsp;</div><div><font face="微软雅黑"><span >#####脚本的状态返回值:</span></font></div><div><font face="微软雅黑"><span >默认是脚本中执行的最后一条件命令的状态返回值;</span></font></div><div><span >`$#` 最后一个命令的返回状态 (0-255) 返回0表示执行正确,命令找不到127,文件找不到2.(程序识别任意)</span></div><div><font face="微软雅黑"><span >自定义状态退出状态码:</span></font></div><div><font face="微软雅黑"><span >`exit &nbsp;[n]:n`为自己指定的状态码;</span></font></div><div >注意:shell进程遇到exit时,即会终止,因此,整个脚本执行即为结束; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div >*** &nbsp; &nbsp; &nbsp;&nbsp;</div></div></div></div><div >\d &nbsp;显示日期,格式为“星期 月 日”</div><div >\h &nbsp;显示简写主机名,如默认主机名“localhost”</div><div >\t &nbsp; 显示24小时制时间,格式 HH:MM:SS</div><div ><span >\T &nbsp;显示12小时制时间,格式同上</span></div><div >\A &nbsp;显示24小时,格式为HH:MM</div><div ><span >\u &nbsp;显示当前用户</span></div><div >\w &nbsp;显示当前所在目录的完整名称</div><div >\W 显示当前所在目录的最后一个目录。</div><div >\# &nbsp; 执行的第几个命令</div><div >\$ &nbsp; 提示符如果是root会显示提示符为“#”,普通用户显示为“$”&nbsp;</div><div >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eg#PS1 &nbsp;显示当前提示符。自己改PS1=‘[]'</div><div ><br></div><div >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div >###bash脚本编程之用户交互:</div><div >####read参数</div><div >```</div><div ><span >read[选项][变量名]</span></div><div >&nbsp; -p “PROMPT提示信息”: 在等待read输入时,输出提示信息&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div >&nbsp; -t &nbsp;TIMEOUT &nbsp; : &nbsp; read命令会一直等待用户输入,使用此选项可以指定等待时间</div><div >&nbsp; -n &nbsp;字符数: &nbsp; &nbsp;read命令只接受指定的字符数,就会执行</div><div >&nbsp; -s : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 隐藏输入数据,适用于机密信息的输入&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div >``` &nbsp;</div><div >####<span >echo用法</span></div><div ><div >```</div><div >echo [OPTION]...“ {转义符}[输出内容] ”</div><div >&nbsp; &nbsp; &nbsp; -e &nbsp;支持转义符</div><div ><span >先添选项-e,在输出内容里添加转义符。</span></div><div ><span >转义符</span><span >&nbsp; &nbsp;&nbsp;</span><span >输出</span></div><div ><span >\\</span><span >&nbsp; &nbsp; &nbsp; &nbsp;</span><span >输出\本身</span><br></div><div >\a<span >&nbsp; &nbsp; &nbsp; &nbsp;</span><span >输出警告音</span></div><div >\b&nbsp;<span >&nbsp; &nbsp; &nbsp;&nbsp;</span><span >退格</span></div><div >\c<span >&nbsp; &nbsp; &nbsp;&nbsp;</span><span >取消输出行末尾的转换符,和“-n”选项一样</span></div><div >\f<span >&nbsp; &nbsp; &nbsp;&nbsp;</span><span >换页</span></div><div >\n<span >&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span >换行</span><br></div><div >\r<span >&nbsp; &nbsp; &nbsp;&nbsp;</span><span >回车</span></div><div >\e &nbsp; &nbsp; &nbsp;escape键<br>\t &nbsp; &nbsp; &nbsp;水平制表符,<br>\v<span >&nbsp; &nbsp; &nbsp;&nbsp;</span><span >垂直制表符</span><br></div><div >\0nnn<span >&nbsp; &nbsp;&nbsp;</span><span >按照八进制ASCII表输出字符,0为数字0,nnn是三位八进制数</span><span ><br></span></div><div ><span >\xhh</span><span >&nbsp; &nbsp;&nbsp;</span><span >按照十六进制ASCII表输出字符,其中hh是俩位十六进制数</span></div><div >```</div><div >echo默认结尾没有换行,print默认有换行符</div><div >***</div><div >#####echo的字符颜色</div><div >&nbsp;`echo -e "\e[31m &nbsp;abcd \e[0m" ` &nbsp; //输出红色字符abcd,31m表颜色,\e[31m起始 &nbsp; &nbsp; \e[0m"</div><div >支持颜色</div><div >30m=黑色 &nbsp; 31m=红色 &nbsp; 32m=绿色 &nbsp; 33=*** &nbsp;34m=蓝色 &nbsp;35m=洋红色 &nbsp;36m=青色</div><div >37m=白色</div><div >***</div><div ><div >####printf&nbsp;</div><div >```</div><div >printf‘打印格式’ 实际内容 &nbsp; &nbsp;%s字符串 &nbsp;%f 浮点</div><div >&nbsp; &nbsp; &nbsp; \a 警告声音输出 &nbsp; &nbsp; &nbsp;\b退格键(backspace) &nbsp; \f &nbsp;清屏 &nbsp; &nbsp; &nbsp;\n换行&nbsp; &nbsp; &nbsp; &nbsp; \r enter键 &nbsp; &nbsp; &nbsp;</div><div >&nbsp; &nbsp; &nbsp; \t &nbsp;水平tab键(制表符,空格) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\v 垂直tab键 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\xNN NN为两位数字按键 转数字为字符</div><div >不加‘’输出只认第一个格式输出符,后面也不能加文件名和管道。</div><div>```</div><div>***</div></div></div><div ><span >&nbsp; &nbsp; &nbsp; &nbsp; 变量测试和替换</span></div><table border="1" cellpadding="2" cellspacing="0" ><tbody ><tr ><td valign="top" >变量置换方式</td><td valign="top" ><div >变量y没有设置</div></td><td valign="top" ><div >变量y为空值</div></td><td valign="top" ><div >变量y设置值</div></td></tr><tr ><td valign="top" >x=${y-新值}</td><td valign="top" ><div >x=新值</div></td><td valign="top" ><div >x为空</div></td><td valign="top" ><div >x=$y</div></td></tr><tr ><td valign="top" ><div >x=${y:-新值}</div></td><td valign="top" ><div >x=新值</div></td><td valign="top" ><div >x=新值</div></td><td valign="top" ><div >x=$y</div></td></tr><tr ><td valign="top" ><div >x=${y+新值}</div></td><td valign="top" ><div >x为空</div></td><td valign="top" >x=新值</td><td valign="top" ><div >x=新值</div></td></tr><tr ><td valign="top" ><div >x=${y:+新值}</div></td><td valign="top" ><div >x为空</div></td><td valign="top" ><div >x为空</div></td><td valign="top" ><div >x=新值</div></td></tr><tr ><td valign="top" ><div >x=${y=新值}</div></td><td valign="top" ><div >x=新值</div><div >y=新值</div></td><td valign="top" ><div >x为空</div><div >y值不变</div></td><td valign="top" ><div >x=$y</div><div >y值不变</div></td></tr><tr ><td valign="top" >x=${y:=新值}</td><td valign="top" ><div >x=新值</div><div >y=新值</div></td><td valign="top" ><div >x=新值</div><div >y=新值</div></td><td valign="top" >x=$y<br >y值不变<br ></td></tr><tr ><td valign="top" ><div >x=${y?新值}</div></td><td valign="top" ><div >新值输出到标准错误输出(就是屏幕)</div></td><td valign="top" ><div >x为空</div></td><td valign="top" ><div >x=$y</div></td></tr><tr ><td valign="top" ><div >x=${y:?新值}</div></td><td valign="top" ><div >新值输出到标准错误输出</div></td><td valign="top" >新值输出到标准错误输出</td><td valign="top" ><div >x=$y</div></td></tr></tbody></table><div ><br ></div><div >注:程序使用,不需要记,多查。</div><div >测试方式中有没有:的区别就是在Y为空时候的对待方式。-+ 完全相反。=会影响xy &nbsp;?结果输出到屏幕。</div><div ><div><br></div></div><div ><br></div></body></html>


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP