Shell编程基础篇-下
1.1 条件表达式
1.1.1 文件判断
常用文件测试操作符
<td style="width: 60.74%; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; border-top-color: #5b9bd5; border-right-color: #5b9bd5; border-bottom-color: #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 1;">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">说明</span></strong>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">文件存在且为目录则为真,即测试表达式成立</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">文件存在且为普通文件则为真,即测试表达式成立</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">文件存在则为真,即测试表达式成立。注意区别于“</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">-f</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">”,</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">-e</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">不辨别</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">是目录还是文件</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">文件存在且可读则为真,即测试表达式成立</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">文件存在且文件大小不为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US"></span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">则为真,即测试表达式成立</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">文件存在且可写则为真,即测试表达式成立</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">文件存在且可执行则为真,即测试表达式成立</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">文件存在且为链接文件则为真,即测试表达式成立</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">文件</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">fl</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">比文件</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">f2</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">新则为真,即测试表达式成立。根据文件的修改</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">时间来计算</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="60%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">文件</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">fl</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">比文件</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">f2</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">旧则为真,即测试表达式成立。根据文件的修改</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">时间来计算</span>
</p>
</td>
_判断文件是否存在,_返回方式
判断目录是否存在
使用变量的方法进行判断
1.1.2 字符串判断
字符串测试操作符
<td style="width: 74.28%; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; border-top-color: #5b9bd5; border-right-color: #5b9bd5; border-bottom-color: #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" width="74%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 1;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">说明</span></strong>
</p>
</td>
<td style="width: 74.28%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="74%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">若字符串的长度不为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">0,</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">则为真,即测试表达式成立,</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">n</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">可以理解为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">no zero</span>
</p>
</td>
<td style="width: 74.28%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="74%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">若字符串的长度为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">0,</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">则为真,即测试表达式成立,</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">z</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">可以理解为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">zero</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">的缩写</span>
</p>
</td>
<td style="width: 74.28%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="74%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">若字符串</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">1</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">等于字符串</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">2,</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">则为真,即测试表达式成立,可使用</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">"=="</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">代替</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">"="</span>
</p>
</td>
<td style="width: 74.28%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="74%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">若字符串</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">1</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">不等于字符串</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">2,</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">则为真,即测试表达式成立,但不能用</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">"!=="</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">代替</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">"!="</span>
</p>
</td>
<p class="MsoNormal" style="margin-bottom: .0001pt; mso-yfti-cnfc: 68;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体; mso-bidi-font-weight: bold;" lang="EN-US">2.</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold;">空格非空</span>
</p>
</td>
-z 判断字符串长度
-n 判断字符串长度
"****串 1" == “ 串 2 “ 使用定义变量的方式进行判断
1.1.3 整数判断
整数二元比较操作符参考
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 5;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">使用的比较符号</span></strong>
</p>
</td>
<td style="width: 24.4%; border-top-width: 1pt; border-top-color: #5b9bd5; border-left: none; border-bottom-width: 1pt; border-bottom-color: #5b9bd5; border-right: none; background: #5b9bd5; padding: 0cm 5.4pt;" valign="top" width="24%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 1;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">在</span></strong><strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体; color: white; mso-themecolor: background1;" lang="EN-US">(())</span></strong><strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">和</span></strong><strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体; color: white; mso-themecolor: background1;" lang="EN-US">[[]]</span></strong><strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">中</span></strong>
</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 1;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">使用的比较符号</span></strong>
</p>
</td>
<td style="width: 48.52%; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; border-top-color: #5b9bd5; border-right-color: #5b9bd5; border-bottom-color: #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" valign="top" width="48%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 1;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">说明</span></strong>
</p>
</td>
</tr>
<tr>
<td style="width: 27.06%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" width="27%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 68;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">-eq</span></strong>
</p>
</td>
<td style="width: 24.4%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="24%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">==</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">或</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">=</span>
</p>
</td>
<td style="width: 48.52%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="48%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">相等,全拼为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">equal</span>
</p>
</td>
</tr>
<tr>
<td style="width: 27.06%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; padding: 0cm 5.4pt;" width="27%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 4;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">-ne</span></strong>
</p>
</td>
<td style="width: 24.4%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="24%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">!</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">=</span>
</p>
</td>
<td style="width: 48.52%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="48%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">不相等,全拼为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">not equal</span>
</p>
</td>
</tr>
<tr>
<td style="width: 27.06%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" width="27%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 68;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">-gt</span></strong>
</p>
</td>
<td style="width: 24.4%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="24%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">> </span>
</p>
</td>
<td style="width: 48.52%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="48%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">大于,全拼为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">greater than</span>
</p>
</td>
</tr>
<tr>
<td style="width: 27.06%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; padding: 0cm 5.4pt;" width="27%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 4;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">-ge</span></strong>
</p>
</td>
<td style="width: 24.4%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="24%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">>=</span>
</p>
</td>
<td style="width: 48.52%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="48%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">大于等于,全拼为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">greater equal</span>
</p>
</td>
</tr>
<tr>
<td style="width: 27.06%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" width="27%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 68;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">-lt</span></strong>
</p>
</td>
<td style="width: 24.4%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="24%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">< </span>
</p>
</td>
<td style="width: 48.52%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="48%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">小于,全拼为丨</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">ess than</span>
</p>
</td>
</tr>
<tr>
<td style="width: 27.06%; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-right-color: #9cc2e5; border-bottom-color: #9cc2e5; border-left-color: #9cc2e5; border-top: none; padding: 0cm 5.4pt;" width="27%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 4;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">-le</span></strong>
</p>
</td>
<td style="width: 24.4%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="24%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US"><=</span>
</p>
</td>
<td style="width: 48.52%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="48%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">小于等于,全拼为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">less equal</span>
</p>
</td>
</tr>
</table>
判断两数是否相等
大于等于
小于
大于
不等于
1.1.4 逻辑符号
常用逻辑操作符
<td style="width: 28.46%; border-top-width: 1pt; border-top-color: #5b9bd5; border-left: none; border-bottom-width: 1pt; border-bottom-color: #5b9bd5; border-right: none; background: #5b9bd5; padding: 0cm 5.4pt;" valign="top" width="28%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 1;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">说明</span></strong>
</p>
</td>
<td style="width: 16.16%; border-top-width: 1pt; border-top-color: #5b9bd5; border-left: none; border-bottom-width: 1pt; border-bottom-color: #5b9bd5; border-right: none; background: #5b9bd5; padding: 0cm 5.4pt;" width="16%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 1;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">在</span></strong><strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体; color: white; mso-themecolor: background1;" lang="EN-US">[[]]</span></strong><strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">和中使用的操作符</span></strong>
</p>
</td>
<td style="width: 36.44%; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; border-top-color: #5b9bd5; border-right-color: #5b9bd5; border-bottom-color: #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" width="36%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: center; mso-yfti-cnfc: 1;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; color: white; mso-themecolor: background1;">说明</span></strong>
</p>
</td>
<td style="width: 28.46%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="28%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">[ </span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">条件</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">A -a </span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">条件</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">B ]</span>
</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">A</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">与</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">B</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">都要成立,整个表达式才成立</span>
</p>
</td>
<td style="width: 16.16%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="16%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">&&</span>
</p>
</td>
<td style="width: 36.44%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="36%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">and</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">,与,两端都为真,则结果为真</span>
</p>
</td>
<td style="width: 28.46%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" valign="top" width="28%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">[ </span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">条件</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">A -o </span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">条件</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">B]</span>
</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">A</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">与</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">B</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">都不成立,整个表达式才不成立</span>
</p>
</td>
<td style="width: 16.16%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="16%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">||</span>
</p>
</td>
<td style="width: 36.44%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; padding: 0cm 5.4pt;" width="36%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">or</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">,或,两端有一个为真,则结果为真</span>
</p>
</td>
<td style="width: 28.46%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="28%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US"> </span>
</p>
</td>
<td style="width: 16.16%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="16%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">!</span>
</p>
</td>
<td style="width: 36.44%; border-top: none; border-left: none; border-bottom-width: 1pt; border-bottom-color: #9cc2e5; border-right-width: 1pt; border-right-color: #9cc2e5; background: #deeaf6; padding: 0cm 5.4pt;" width="36%">
<p class="MsoNormal" style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph; mso-yfti-cnfc: 64;">
<span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; mso-fareast-font-family: 宋体;" lang="EN-US">not</span><span style="font-size: 10.0pt; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman';">,非,两端相反,则结果为真</span>
</p>
</td>
逻辑操作符与整数判断配合
取反
两边都为真
至少有一边为真
感叹号的特殊用法
_使用历史命令,__感叹号加上history__中的序号,_即可执行
1.1.5 【练习题】开发3个shell脚本比较2个整数大小
要求:
<p class="a" 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;">
<span lang="EN-US">2. </span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">用条件表达式(禁止</span><span lang="EN-US">if</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">语句)进行判断。</span>
</p>
<p class="a" 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;">
<span lang="EN-US">3. </span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">将</span><span lang="EN-US">2</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">个整数的比较结果输出到屏幕,出错需要提示。</span>
</p>
使用定义变量方法
expr 1 + $NUM1 &>/dev/null [ $? -eq 2 ] && echo "$NUM1 不是整数 " && exit 2 expr 1 + $NUM2 &>/dev/null [ $? -eq 2 ] && echo "$NUM2 不是整数 " && exit 2
[ "$NUM1" -eq "$NUM2" ] && echo $NUM1 = $NUM2 && exit [ "$NUM1" -gt "$NUM2" ] && echo $NUM1 > $NUM2 && exit [ "$NUM1" -lt "$NUM2" ] && echo $NUM1 < $NUM2
使用传参方法
expr 1 + $NUM1 &>/dev/null [ $? -eq 2 ] && echo "$NUM1 不是整数 " && exit 2 expr 1 + $NUM2 &>/dev/null [ $? -eq 2 ] && echo "$NUM2 不是整数 " && exit 2
[ "$NUM1" -eq "$NUM2" ] && echo $NUM1 = $NUM2 && exit [ "$NUM1" -gt "$NUM2" ] && echo $NUM1 > $NUM2 && exit [ "$NUM1" -lt "$NUM2" ] && echo $NUM1 < $NUM2
使用read****读入
expr 1 + $NUM1 &>/dev/null [ $? -eq 2 ] && echo "$NUM1 不是整数 " && exit 2 expr 1 + $NUM2 &>/dev/null [ $? -eq 2 ] && echo "$NUM2 不是整数 " && exit 2
[ "$NUM1" -eq "$NUM2" ] && echo "$NUM1 = $NUM2" && exit [ "$NUM1" -gt "$NUM2" ] && echo "$NUM1 > $NUM2" && exit echo "$NUM1 < $NUM2"
1.2 if条件语句
# 条件表达式和if语句可以互相转换
1.2.1 三种语法
单分支语句
then
echo </span><span style="color: #800000;">'</span><span style="color: #800000;">文件存在</span><span style="color: #800000;">'</span><span style="color: #000000;">
fi
双分支语句
then
echo "文件存在"
else
echo "文件不存在"
echo "…" »/tmp/test.log
fi
多分支语句
then
echo " hosts文件存在"
elif [ -f /etc/host ]
then
echo " host文件存在"
fi
1.2.2 if条件语句小结
单分支:一个条件一个结果
双分支:一个条件两个结果
多分支:多个条件多个结果
1.2.3 【练习题1】输入2个数字,比较大小(使用if语句结合条件表达式实现)
示例脚本一:
<div id="cnblogs_code_open_aedd633c-e6a5-4762-a599-fe927054b61e" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn panduan1]<span style="color: #008000;">#</span><span style="color: #008000;"> cat if_panduan_1.sh </span>
2 #!/bin/bash
3 #############################################################
4 # File Name: panduan1.sh
5 # Version: V1.0
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-06 11:27:32
9 # Description:
10 #############################################################
11
12 read -p "请输入第一个整数:" NUM1
13 expr 1 + $NUM1 &>/dev/null
14 if [ $? -eq 2 ]
15 then
16 echo "$NUM1 不是整数 "
17 exit 2
18 fi
19
20 read -p "请输入第二个整数:" NUM2
21 expr 1 + $NUM2 &>/dev/null
22 if [ $? -eq 2 ]
23 then
24 echo "$NUM2 不是整数 "
25 exit 2
26 fi
27
28 # 判断输入数值大小
29 if [ $NUM1 -eq $NUM2 ]
30 then
31 echo "$NUM1 = $NUM2"
32 exit
33 fi
34
35 if [ $NUM1 -gt $NUM2 ]
36 then
37 echo "$NUM1 > $NUM2"
38 exit
39 fi
40
41 if [ $NUM1 -lt $NUM2 ]
42 then
43 echo "$NUM1 < $NUM2"
44 fi
<p>
<span class="cnblogs_code_collapse">View Code read方法 单分支</span></div> </div>
<p>
<strong><em>示例脚本二:</em></strong>
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('c3249ca5-68e4-48c0-ae12-4fc1405b69dc')">
<img id="code_img_closed_c3249ca5-68e4-48c0-ae12-4fc1405b69dc" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-11.gif" alt="Shell编程基础篇-下" alt="" /><img id="code_img_opened_c3249ca5-68e4-48c0-ae12-4fc1405b69dc" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('c3249ca5-68e4-48c0-ae12-4fc1405b69dc',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-11.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" /></p>
<div id="cnblogs_code_open_c3249ca5-68e4-48c0-ae12-4fc1405b69dc" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn panduan1]<span style="color: #008000;">#</span><span style="color: #008000;"> cat if_panduan_2.sh </span>
2 #!/bin/bash 3 ############################################################# 4 # File Name: if_panduan_2.sh 5 # Version: V1.0 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-06 11:27:32 9 # Description: 10 ############################################################# 11 12 read -p "请输入第一个整数:" NUM1 13 expr 1 + $NUM1 &>/dev/null 14 if [ $? -eq 2 ] 15 then 16 echo "$NUM1 不是整数 " 17 exit 2 18 fi 19 20 read -p "请输入第二个整数:" NUM2 21 expr 1 + $NUM2 &>/dev/null 22 if [ $? -eq 2 ] 23 then 24 echo "$NUM2 不是整数 " 25 exit 2 26 fi 27 28 # 判断输入数值大小 29 if [ $NUM1 -eq $NUM2 ] 30 then 31 echo "$NUM1 = $NUM2" 32 exit 33 else 34 if [ $NUM1 -gt $NUM2 ] 35 then 36 echo "$NUM1 > $NUM2" 37 exit 38 else 39 if [ $NUM1 -lt $NUM2 ] 40 then 41 echo "$NUM1 < $NUM2" 42 fi 43 fi 44 fi
<p>
<span class="cnblogs_code_collapse">View Code 双分支</span></div> </div>
<p>
<strong><em>示例脚本三:</em></strong>
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('15e8b7a9-c191-4ccc-bfea-30c671e206b8')">
<img id="code_img_closed_15e8b7a9-c191-4ccc-bfea-30c671e206b8" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-11.gif" alt="Shell编程基础篇-下" alt="" /><img id="code_img_opened_15e8b7a9-c191-4ccc-bfea-30c671e206b8" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('15e8b7a9-c191-4ccc-bfea-30c671e206b8',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-11.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" /></p>
<div id="cnblogs_code_open_15e8b7a9-c191-4ccc-bfea-30c671e206b8" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn panduan1]<span style="color: #008000;">#</span><span style="color: #008000;"> cat if_panduan_3.sh </span>
2 #!/bin/bash
3 #############################################################
4 # File Name: if_panduan_3.sh
5 # Version: V1.0
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-06 11:27:32
9 # Description:
10 #############################################################
11
12 read -p "请输入第一个整数:" NUM1
13 expr 1 + $NUM1 &>/dev/null
14 NUM1_FH=$?
15 if [ $NUM1_FH -eq 2 ]
16 then
17 echo "$NUM1 不是整数 "
18 exit 2
19 else
20 read -p "请输入第二个整数:" NUM2
21 expr 1 + $NUM2 &>/dev/null
22 NUM2_FH=$?
23 if [ $NUM2_FH -eq 2 ]
24 then
25 echo "$NUM2 不是整数 "
26 exit 2
27 fi
28 fi
29
30 # 判断输入数值大小
31 if [ $NUM1 -eq $NUM2 ]
32 then
33 echo "$NUM1 = $NUM2"
34 exit
35 elif [ $NUM1 -gt $NUM2 ]
36 then
37 echo "$NUM1 > $NUM2"
38 exit
39 elif [ $NUM1 -lt $NUM2 ]
40 then
41 echo "$NUM1 < $NUM2"
42 fi
<p>
<span class="cnblogs_code_collapse">View Code 多分支</span></div> </div>
<h3>
<span id="124_2100M3">1.2.4 【练习题2】系统内存低于100M邮件报警,加入计划任务,3分钟检查一次。</span>
</h3>
<div>
<p class="a4">
对于开发程序而言,一般来说应该遵循下面的3步法则。
</p>
<div style="mso-element: para-border-div; border: solid windowtext 2.0pt; padding: 1.0pt 4.0pt 1.0pt 4.0pt; mso-border-shadow: yes; background: #F2F2F2; mso-shading: windowtext; mso-pattern: gray-5 auto;">
<p class="a">
<span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">(</span><span lang="EN-US">1</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">)分析需求</span>
</p>
<p class="a">
<span lang="EN-US"> </span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">明白开发需求,是完成程序的大前提,因此,分析需求至关重要,<em>一切不以需求为主的程序开发,都是耍流氓的!</em></span>
</p>
<p class="a">
<span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">(</span><span lang="EN-US">2</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">)设计思路</span>
</p>
<p class="a">
<span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">设计思路就是根据需求,把需求进行拆解,分模块逐步实现,例如本题可以分为如下几步:</span>
</p>
<p class="a" 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;">
<span lang="EN-US">1</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">)获取当前系统剩余内存的值(先在命令行实现)。</span>
</p>
<p class="a" 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;">
<span lang="EN-US">2</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">)配置邮件报警(可采用第三方邮件服务器)。</span>
</p>
<p class="a" 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;">
<span lang="EN-US">3</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">)判断取到的值是否小于</span><span lang="EN-US">100MB</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">,如果小于</span><span lang="EN-US">100MB</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">,就报警(采用</span><span lang="EN-US">if</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">语句)。</span>
</p>
<p class="a" 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;">
<span lang="EN-US">4</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">)编码实现</span><span lang="EN-US">Shell</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">脚本。</span>
</p>
<p class="a" 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;">
<span lang="EN-US">5</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">)加入</span><span lang="EN-US">crond</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">定时任务,每三分钟检查一次。</span>
</p>
<p class="a">
<span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">(</span><span lang="EN-US">3</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">)编码实现</span>
</p>
<p class="a">
<span lang="EN-US"> </span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">编码实现就是具体的编码及调试过程,工作中很可能需要先在测试环境下调试,调试好了,再发布到生产环境中。</span>
</p>
</div>
</div>
<p>
第一步 先配置邮件服务,保证能够发生邮件
</p>
<div>
<div class="cnblogs_code">
<pre>echo <span style="color: #800000;">'</span><span style="color: #800000;">set [email protected] smtp=smtp.znix.top [email protected] smtp-auth-password=****** smtp-auth=login</span><span style="color: #800000;">'</span> >> /etc/<span style="color: #000000;">mail.rc</span></pre>
</div>
</div>
<div>
<pre><span>发送测试邮件发送</span></pre>
<div class="cnblogs_code">
<pre>echo <span style="color: #800000;">"</span><span style="color: #800000;">`date +%F_%T`</span><span style="color: #800000;">"</span> |mail -s <span style="color: #800000;">"</span><span style="color: #800000;">这是测试邮件</span><span style="color: #800000;">"</span> [email protected]</pre>
</div>
</div>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208162738046-1664484551.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<p>
第二步编写检查脚本
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]<span style="color: #008000;">#</span><span style="color: #008000;"> cat mem_info.sh </span><span style="color: #008000;">
#!/bin/bash
##############################################################
# File Name: mem_info.sh
# Version: V1.0
# Author: clsn
# Organization: http://blog.znix.top
# Created Time : 2017-12-06 8:42:50
# Description:
##############################################################
Mem=free -m |awk <span style="color: #800000;">'</span><span style="color: #800000;">NR==2{print $NF}</span><span style="color: #800000;">'</span><span style="color: #000000;">
host=hostname
Ip=hostname -<span style="color: #000000;">I
Date=date +%F_%<span style="color: #000000;">T
mail_file=/tmp/mail.s
dest_user=[email protected]
if [ $Mem -lt 100 ] then echo "发生时间: $Date" >$mail_file echo "发生主机: $host 主机IP地址: $Ip " » $mail_file echo "内存余量: $Mem M" » $mail_file mail -s "【警告】内存不足了呀!" $dest_user < $mail_file fi
<p>
第三步测试脚本(可以修改判断的值)
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]<span style="color: #008000;">#</span><span style="color: #008000;"> sh mem_info.sh</span></pre>
</div>
</div>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208162936265-2085195319.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<p>
第四步 脚本测试成功,写入定时任务
</p>
<div>
<div class="cnblogs_code">
<pre>[root@clsn panduan1]<span style="color: #008000;">#</span><span style="color: #008000;"> crontab -l </span><span style="color: #008000;">
# 检查内存剩余大小 */3 * * * * /bin/sh /server/scripts/panduan/mem_info.sh &>/dev/null
<p>
至此,一个监控脚本就写好了
</p>
<h3>
<span id="125_3nginx">1.2.5 【练习题3】模拟编写启动nginx脚本</span>
</h3>
<p>
脚本内容
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('b768078f-902b-4e05-9fe3-51e820e1a369')">
<img id="code_img_closed_b768078f-902b-4e05-9fe3-51e820e1a369" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-11.gif" alt="Shell编程基础篇-下" alt="" /><img id="code_img_opened_b768078f-902b-4e05-9fe3-51e820e1a369" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('b768078f-902b-4e05-9fe3-51e820e1a369',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-11.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" /></p>
<div id="cnblogs_code_open_b768078f-902b-4e05-9fe3-51e820e1a369" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]<span style="color: #008000;">#</span><span style="color: #008000;"> cat nginx.sh </span>
2 #!/bin/bash
3 ##############################################################
4 # File Name: nginx.sh
5 # Version: V1.0
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-05 20:56:06
9 # Description:
10 ##############################################################
11 . /etc/init.d/functions
12
13 StartCheck=netstat -lntup |grep -c 80<span style="color: #000000;">
14 StartCMD=’/application/nginx/sbin/nginx ’
15 StopCMD=’/application/nginx/sbin/nginx -s stop ’
16 StatusCheck=netstat -lntp|grep -<span style="color: #000000;">c nginx
17 ReloadCMD=’/application/nginx/sbin/nginx -s reload’
18 CheckCMD=’/application/nginx/sbin/nginx -t’
19 UsaGe="Usage: $0 {start|stop|status|reload|check}"
20
21
22 COMMAND=$1
23
24 if [ $# -ne 1 ]
25 then
26 echo $UsaGe && exit 2
27 fi
28
29 if [ $COMMAND = start ]
30 then
31 if [ $StartCheck == 1 ]
32 then
33 action "启动Nginx失败,端口被占用" /bin/false
34 else
35 $StartCMD
36 action "启动Nginx" /bin/true
37 fi
38 elif [ $COMMAND = stop ]
39 then
40 $StopCMD &>/dev/null
41 if [ $? -eq 0 ]
42 then
43 action "停止Nginx" /bin/true
44 else
45 action "停止Nginx" /bin/false
46 fi
47 elif [ $COMMAND = status ]
48 then
49 if [ $StatusCheck -eq 1 ]
50 then
51 echo "nginx 正在运行…"
52 else
53 echo "Nginx 未运行."
54 fi
55 elif [ $COMMAND = reload ]
56 then
57 $ReloadCMD
58 action "reload" /bin/true
59 elif [ $COMMAND = check ]
60 then
61 $CheckCMD
62 else
63 echo $UsaGe
64 exit 2
65 fi
<p>
<span class="cnblogs_code_collapse">View Code nginx 启动脚本</span></div> </div>
<p style="text-align: left;">
<strong><em>脚本执行过程:</em></strong>
</p>
<p style="text-align: center;">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208163415937-1835896831.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<h3>
<span id="126_5WebMySQL">1.2.6 【练习题5】Web及MySQL服务异常监测案例</span>
</h3>
<p>
用if条件语句实现对Nginx Web服务以及MySQL数据库服务是否正常进行检测,如果服务未启动,则启动相应服务。
</p>
<p>
<em><span style="text-decoration: underline;">脚本编写思路:</span></em>
</p>
<p>
判断web服务器正常 ↓
</p>
<div>
<div class="cnblogs_code">
<pre> 1、进程 ps -ef |<span style="color: #000000;">grep [n]ginx
2、端口 netstat ss losf telnet nc nmap 3、curl 页面 返回值 4、curl check.html 的内容
<p>
判断mysql服务器正常 ↓
</p>
<div>
<div class="cnblogs_code">
<pre> 1<span style="color: #000000;">、端口 netstat ss losf telnet nc nmap
</span>2、进程 ps -ef |<span style="color: #000000;">grep [m]ysql
</span>3<span style="color: #000000;">、mysql 登录访问看一下
</span>4、mysql insert一个数据 select一个数据</pre>
</div>
</div>
<p>
<strong><em>web</em></strong><strong><em>服务监控脚本示例</em></strong>
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('a18b7e09-d8dd-4ac3-b7f2-d5fdda5f1675')">
<img id="code_img_closed_a18b7e09-d8dd-4ac3-b7f2-d5fdda5f1675" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-11.gif" alt="Shell编程基础篇-下" alt="" /><img id="code_img_opened_a18b7e09-d8dd-4ac3-b7f2-d5fdda5f1675" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('a18b7e09-d8dd-4ac3-b7f2-d5fdda5f1675',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-11.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" /></p>
<div id="cnblogs_code_open_a18b7e09-d8dd-4ac3-b7f2-d5fdda5f1675" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]<span style="color: #008000;">#</span><span style="color: #008000;"> cat web_check.sh </span>
2 #!/bin/bash
3 #############################################################
4 # File Name: web_check.sh
5 # Version: V1.0
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-07 10:53:38
9 # Description:
10 #############################################################
11 . /etc/init.d/functions
12
13 JinChen=ps -ef |grep -<span style="color: #000000;">c [n]ginx
14 QiDong=’/server/scripts/nginx.sh start’
15 StatuS=curl -I -w <span style="color: #800000;">"</span><span style="color: #800000;">%{http_code}\n</span><span style="color: #800000;">"</span> -o /dev/null -s 10.0.0.180<span style="color: #000000;">
16 StatuS2=curl -s 10.0.0.180/check.html|grep -<span style="color: #000000;">c ok
17 StartNginx=’/server/scripts/nginx.sh start’
18
19 if [ $JinChen -ge 2 ]
20 then
21 if [ "$StatuS" -eq 200 ]
22 then
23 if [ "$StatuS2" -eq 1 ]
24 then
25 action "Nginx 服务运行正常" /bin/true
26 else
27 action "请检查chenk.html文件!" /bin/false
28 fi
29 else
30 action "请检查首页文件!" /bin/false
31 fi
32 else
33 action "Nginx 未正常运行!" /bin/false
34 $StartNginx
35 fi
<p>
<span class="cnblogs_code_collapse">View Code web 监控脚本内容</span></div> </div>
<p>
<strong><em>脚本执行过程:</em></strong>
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208163354593-104008784.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<h2>
<span id="13_case">1.3 case条件结构语句</span>
</h2>
<h3>
<span id="131_case">1.3.1 case语法结构</span>
</h3>
<div>
<div class="cnblogs_code">
<pre>case <span style="color: #800000;">"</span><span style="color: #800000;">字符串变量</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">in</span><span style="color: #000000;">
值1) 指令1 ;; 值2) 指令2 ;; 值*) 指令 esac
<h3>
<span id="132_caseif">1.3.2 case与if的对比</span>
</h3>
<p>
<em>case</em><em>书写方式</em>
</p>
<div>
<div class="cnblogs_code">
<pre>case $name <span style="color: #0000ff;">in</span><span style="color: #000000;">
值1) 指令1 ;; 值2) 指令2 ;; *) 指令 esac
<p>
<em>if</em><em>书写方式</em>
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">if</span> [ $name == <span style="color: #800000;">"</span><span style="color: #800000;">值1</span><span style="color: #800000;">"</span><span style="color: #000000;"> ]
then
指令1
elif [ $name == "值2" ]
then
指令2
else
指令
fi
<h3>
<span id="133_case">1.3.3 case值的书写方式</span>
</h3>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;">apple)
echo </span>-e <span style="color: #800000;">"</span><span style="color: #800000;">$RED_COLOR apple $RES</span><span style="color: #800000;">"</span><span style="color: #000000;">
;;</span></pre>
</div>
</div>
<p>
也可以这样写,输入2种格式找同一个选项
</p>
<div>
<div class="cnblogs_code">
<pre>apple|<span style="color: #000000;">APPLE)
echo </span>-e <span style="color: #800000;">"</span><span style="color: #800000;">$RED_COLOR apple $RES</span><span style="color: #800000;">"</span><span style="color: #000000;">
;;</span></pre>
</div>
</div>
<h3>
<span id="134_case">1.3.4 case语句小结</span>
</h3>
<p>
? case语句就相当于多分支的if语句。case语句的优势是更规范、易读。
</p>
<p>
? case语句适合变量的值少,且为固定的数字或字符串集合。(1,2,3)或(start,stop,restart)。
</p>
<p>
? 系统服务启动脚本传参的判断多用case语句,多参考rpcbind/nfs/crond脚本;菜单脚本也可以使用case
</p>
<h3>
<span id="135_1case">1.3.5 【练习题1】使用case编写一个菜单脚本</span>
</h3>
<p>
<em><span style="text-decoration: underline;">脚本内容</span></em>
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('0d52846f-26fd-46fd-afdf-0606418b3eed')">
<img id="code_img_closed_0d52846f-26fd-46fd-afdf-0606418b3eed" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-11.gif" alt="Shell编程基础篇-下" alt="" /><img id="code_img_opened_0d52846f-26fd-46fd-afdf-0606418b3eed" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('0d52846f-26fd-46fd-afdf-0606418b3eed',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-11.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" /></p>
<div id="cnblogs_code_open_0d52846f-26fd-46fd-afdf-0606418b3eed" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn case]<span style="color: #008000;">#</span><span style="color: #008000;"> cat menu.sh </span>
2 #!/bin/bash 3 ############################################################# 4 # File Name: menu.sh 5 # Version: V1.0 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-07 14:47:36 9 # Description: 10 ############################################################# 11 12 13 cat<<EOF 14 1. install rsync 15 2. install nfs 16 3. install mysql 17 4. install all 18 EOF 19 20 read -p ’请输入你的选择:’ chs 21 22 case $chs in 23 1) 24 echo install rsync success 25 ;; 26 2) 27 echo install nfs success 28 ;; 29 3) 30 echo install mysql success 31 ;; 32 4) 33 echo install rsync/nfs/mysql success 34 ;; 35 *) 36 echo "你输入有误" 37 esac
<p>
<span class="cnblogs_code_collapse">View Code 菜单脚本内容</span></div> </div>
<p>
脚本执行过程:
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208163659093-2017315512.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<h3>
<span id="136_2">1.3.6 【练习题2】输出带有颜色的水果菜单</span>
</h3>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('7d229ac0-60bc-476b-b857-973697ebbfab')">
<img id="code_img_closed_7d229ac0-60bc-476b-b857-973697ebbfab" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-11.gif" alt="Shell编程基础篇-下" alt="" /><img id="code_img_opened_7d229ac0-60bc-476b-b857-973697ebbfab" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('7d229ac0-60bc-476b-b857-973697ebbfab',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-11.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" /></p>
<div id="cnblogs_code_open_7d229ac0-60bc-476b-b857-973697ebbfab" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]<span style="color: #008000;">#</span><span style="color: #008000;"> cat fruits.sh </span>
2 #!/bin/bash 3 ############################################################# 4 # File Name: fruits.sh 5 # Version: V1.0 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-06 21:23:31 9 # Description: 10 ############################################################# 11 cat <<EOF 12 ======================= 13 1.apple 14 2.pear 15 3.banana 16 4.exit 17 ======================= 18 EOF 19 read -t 10 -p "你要看什么水果:" Menu_Num 20 21 case "$Menu_Num" in 22 1) 23 echo -e "\033[31m apple \033[0m" 24 ;; 25 2) 26 echo -e "\033[32m pear \033[0m" 27 ;; 28 3) 29 echo -e "\033[33m banana \033[0m" 30 ;; 31 4) 32 exit 33 esac
<p>
<span class="cnblogs_code_collapse">View Code 水果菜单脚本</span></div> </div>
<p>
脚本执行过程:
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208163759874-1750258440.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<h3>
<span id="137">1.3.7 写脚本规范及注意事项</span>
</h3>
<p>
1.变量名称不能和系统已经存在的命令等重复 free == > Free
</p>
<p>
2. 判断单位要统一
</p>
<p>
3. 脚本一行不超过一屏的一半。
</p>
<p>
4. 能写成变量的内容尽量写成变量
</p>
<h2>
<span id="14">1.4 练习题</span>
</h2>
<h3>
<span id="141_1Memcached">1.4.1 【练习题1】监控Memcached缓存服务是否正常</span>
</h3>
<div style="mso-element: para-border-div; border: solid windowtext 2.0pt; padding: 1.0pt 4.0pt 1.0pt 4.0pt; mso-border-shadow: yes; background: #F2F2F2; mso-shading: windowtext; mso-pattern: gray-5 auto;">
<p class="a" style="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: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;"> 监控</span><span lang="EN-US">Memcached</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">缓存服务是否正常,模拟用户(</span><span lang="EN-US">web</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">客户端)检测。</span>
</p>
<p class="a" style="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: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;"> 使用</span><span lang="EN-US">nc</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">命令加上</span><span lang="EN-US">set/get</span><span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">来模拟检测。</span>
</p>
</div>
<div>
<p class="a4">
memecached安装:http://www.cnblogs.com/clsn/p/7999510.html
</p>
</div>
<p>
脚本内容:
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('c99863b5-8926-4932-9772-cc2a4c73bb88')">
<img id="code_img_closed_c99863b5-8926-4932-9772-cc2a4c73bb88" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-11.gif" alt="Shell编程基础篇-下" alt="" /><img id="code_img_opened_c99863b5-8926-4932-9772-cc2a4c73bb88" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('c99863b5-8926-4932-9772-cc2a4c73bb88',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-11.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" /></p>
<div id="cnblogs_code_open_c99863b5-8926-4932-9772-cc2a4c73bb88" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]<span style="color: #008000;">#</span><span style="color: #008000;"> vim memcache_check.sh </span>
2 #!/bin/bash
3 #############################################################
4 # File Name: memcache_check.sh
5 # Version: V1.0
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-07 15:50:03
9 # Description:
10 #############################################################
11 . /etc/init.d/functions
12 . /etc/init.d/run # 函数 jingdutiao 使用
13 MemPort=netstat -lntp |grep -c 0.0.0.0:11211<span style="color: #000000;">
14 Set_Key=’printf “set clsn2017 0 10 8\r\nclsn2018\r\n”|nc 10.0.0.180 11211’
15 Get_Key=’printf “get clsn2017\r\n” |nc 10.0.0.180 11211 |grep -c clsn2018’
16 ReStart=’systemctl restart memcached.service’
17
18 if [ $MemPort -eq 1 ]
19 then
20 $Set_Key
21 $Get_Key
22 if [ $? -ne 1 ]
23 then
24 action "Memcached 运行正常!" /bin/true
25 else
26 action "Memcached 服务异常!" /bin/false
27 fi
28 else
29 action "服务未启动!" /bin/false
30 $ReStart
31 jingdutiao
32 fi
<p>
<span class="cnblogs_code_collapse">View Code 监控 memcached脚本</span></div> </div>
<p>
脚本执行过程
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208164010109-1318773961.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<h3>
<span id="142_2casersync">1.4.2 【练习题2】使用(case)编写rsync管理脚本</span>
</h3>
<p>
写网络服务独立进程模式下Rsync的系统启动脚本,例如:/etc/init.d/rsyncd {start|stop|restart}。
</p>
<p>
<strong>要求:</strong>
</p>
<p>
1.要使用系统函数库技巧。
</p>
<p>
2.要用函数,不能一坨?的方式。
</p>
<p>
3.在centos 6中 可被chkconfig管理。
</p>
<p>
<span style="background-color: #ff0000; color: #ffffff;"><strong>注意:</strong></span>
</p>
<p>
服务的停止操作和启动操作之间要有间隔时间,使用sleep 1
</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: #000000;">pkill 进程
sleep 1
start 服务
<p>
rsync服务启动脚本
</p>
</div>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('c6ea408f-53a4-49cd-8ba7-12a50142bfbc')">
<img id="code_img_closed_c6ea408f-53a4-49cd-8ba7-12a50142bfbc" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-11.gif" alt="Shell编程基础篇-下" alt="" /><img id="code_img_opened_c6ea408f-53a4-49cd-8ba7-12a50142bfbc" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('c6ea408f-53a4-49cd-8ba7-12a50142bfbc',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-11.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" /></p>
<div id="cnblogs_code_open_c6ea408f-53a4-49cd-8ba7-12a50142bfbc" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]<span style="color: #008000;">#</span><span style="color: #008000;"> cat rsyncd </span>
2 #!/bin/bash
3 #############################################################
4 # File Name: rsync_admin.sh
5 # Version: V1.0
6 # Author: clsn
7 # Organization: http://blog.znix.top
8 # Created Time : 2017-12-07 18:39:37
9 # Description:
10 #############################################################
11
12 . /etc/init.d/functions
13 . /etc/init.d/run
14
15 Rsync_Port=netstat -lntup |grep -c 0.0.0.0:873<span style="color: #000000;">
16 Rsync_COM1=’rsync –daemon’
17 Rsync_COM2=’pkill rsync’
18
19 Rsync_Start() {
20 Rsync_Port1=netstat -lntup |grep -c 0.0.0.0:873<span style="color: #000000;">
21 if [ $Rsync_Port1 -ne 0 ]
22 then
23 action "服务已启动" /bin/false
24 exit 3
25 else
26 $Rsync_COM1
27 #action “Rsync 启动” /bin/true
28 QiDong
29 fi
30 }
31
32 Rsync_Stop() {
33 Rsync_Port2=netstat -lntup |grep -c 0.0.0.0:873<span style="color: #000000;">
34 if [ $Rsync_Port2 -eq 0 ]
35 then
36 action "服务未启动" /bin/false
37 exit 3
38 else
39 $Rsync_COM2
40 #action “Rsync 停止” /bin/true
41 TingZhi
42 fi
43 }
44
45 Rsync_Status() {
46 if [ $Rsync_Port -eq 1 ]
47 then
48 echo "Rsync 服务运行中…"
49 else
50 echo "Rsync 服务未运行"
51 fi
52 }
53
54 Rsync_Restart() {
55 Rsync_Stop
56 Rsync_Start
57 }
58 COMMAND=$1
59
60 case "$COMMAND" in
61 start)
62 Rsync_Start
63 ;;
64 stop)
65 Rsync_Stop
66 ;;
67 status)
68 Rsync_Status
69 ;;
70 restart|reload|force-reload)
71 Rsync_Restart
72 ;;
73 *)
74 echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
75 exit 2
76 esac
<p>
<span class="cnblogs_code_collapse">View Code rsync 服务管理脚本</span></div> </div>
<p>
脚本执行过程
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208164208499-688494750.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<h3>
<span id="143_3">1.4.3 【练习题3】菜单自动化软件部署脚本实践</span>
</h3>
<p>
<strong><em>综合实例:</em></strong>打印选择菜单,按照选择一键安装不同的Web服务。
</p>
<div>
<div style="mso-element: para-border-div; border: solid windowtext 2.0pt; padding: 1.0pt 4.0pt 1.0pt 4.0pt; mso-border-shadow: yes; background: #F2F2F2; mso-shading: windowtext; mso-pattern: gray-5 auto;">
<p class="a">
<span style="font-family: '微软雅黑',sans-serif; mso-ascii-font-family: 宋体; mso-hansi-font-family: 宋体;">示例菜单:</span>
</p>
<p class="a">
<span lang="EN-US">[root@oldboy scripts]# sh menu.sh</span>
</p>
<p class="a">
<span lang="EN-US"> 1.[install lamp]</span>
</p>
<p class="a">
<span lang="EN-US"> 2.[install lnmp]</span>
</p>
<p class="a">
<span lang="EN-US"> 3.[exit]</span>
</p>
<p class="a">
<span lang="EN-US"> pls input the num you want:</span>
</p>
</div>
</div>
<p>
<strong>要求:</strong>
</p>
<p>
1、当用户输入1时,输出“start installing lamp.提示”然后执行/server/scripts/lamp.sh,脚本内容输出"lampis installed"后退出脚本,工作中就是正式lamp一键安装脚本;
</p>
<div>
<div class="cnblogs_code">
<pre>echo <span style="color: #800000;">'</span><span style="color: #800000;">echo lampis installed</span><span style="color: #800000;">'</span> > /server/scripts/<span style="color: #000000;">lamp.sh
chmod +x /server/scripts/lamp.sh
<p>
2、当用户输入2时,输出“start installing lnmp.提示” 然后执行/server/scripts/lnmp.sh输出"lnmpis installed"后退出脚本,工作中就是正式lnmp一键安装脚本;
</p>
</div>
<div>
<div class="cnblogs_code">
<pre>echo <span style="color: #800000;">'</span><span style="color: #800000;">echo lnmpis installed</span><span style="color: #800000;">'</span> > /server/scripts/<span style="color: #000000;">lnmp.sh
chmod +x /server/scripts/lnmp.sh
<p>
3、当输入3时,退出当前菜单及脚本;
</p>
<p>
4、当输入任何其它字符,给出提示“Input error”后退出脚本;
</p>
<p>
5、要对执行的脚本进行相关的条件判断,例如:脚本文件是否存在,是否可执行等判断。
</p>
<p>
<strong><em><span style="text-decoration: underline;">脚本 </span></em></strong><strong><em><span style="text-decoration: underline;">内容</span></em></strong>
</p>
<div>
<div class="cnblogs_code" onclick="cnblogs_code_show('53d075b1-ff7a-4ab9-b90b-57d10b8d1254')">
<img id="code_img_closed_53d075b1-ff7a-4ab9-b90b-57d10b8d1254" class="code_img_closed" src="https://clsn.io/wp-content/uploads/2018/03/ContractedBlock-11.gif" alt="Shell编程基础篇-下" alt="" /><img id="code_img_opened_53d075b1-ff7a-4ab9-b90b-57d10b8d1254" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('53d075b1-ff7a-4ab9-b90b-57d10b8d1254',event)" data-original="https://clsn.io/wp-content/uploads/2018/03/ExpandedBlockStart-11.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" /></p>
<div id="cnblogs_code_open_53d075b1-ff7a-4ab9-b90b-57d10b8d1254" class="cnblogs_code_hide">
<pre><span style="color: #008080;"> 1</span> [root@clsn scripts]<span style="color: #008000;">#</span><span style="color: #008000;"> cat menu.sh </span>
2 #!/bin/bash 3 ############################################################# 4 # File Name: menu.sh 5 # Version: V1.0 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-07 19:29:30 9 # Description: 10 ############################################################# 11 12 cat <<EOF 13 1.[install lamp] 14 2.[install lnmp] 15 3.[exit] 16 EOF 17 18 read -p "please input the num you want:" Num 19 20 case "$Num" in 21 1) 22 if [ -f /server/scripts/lamp.sh ] 23 then 24 echo -e "\033[36mstart installing lamp\033[0m" 25 /server/scripts/lamp.sh 26 else 27 echo -e "\033[5;41;37m /server/scripts/lamp.sh 文件不存在 \033[0m" 28 fi 29 ;; 30 2) 31 if [ -f /server/scripts/lnmp.sh ] 32 then 33 echo -e "\033[36mstart installing lnmp\033[0m" 34 /server/scripts/lnmp.sh 35 else 36 echo -e "\033[5;41;37m /server/scripts/lnmp.sh 文件不存在 \033[0m" 37 fi 38 ;; 39 3) 40 exit 41 ;; 42 *) 43 echo -e "\033[31mInput error \033[0m" 44 exit 2 45 esac
<p>
<span class="cnblogs_code_collapse">View Code 自动化软件部署脚本</span></div> </div>
<p>
执行过程
</p>
<p align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208164356609-1960744069.gif" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<h3>
<span id="144_if_case">1.4.4 if 与 case 对比</span>
</h3>
<p>
if 语句类似黑名单,需要把这种错误场景封堵
</p>
<p>
case 语句类似白名单,只要把正确结果列完整即可
</p>
<h2>
<span id="15">1.5 其他补充说明</span>
</h2>
<h3>
<span id="151_linux">1.5.1 linux中产生随机数的方法</span>
</h3>
<div>
<div class="cnblogs_code">
<pre>[root@clsn scripts]<span style="color: #008000;">#</span><span style="color: #008000;"> echo $RANDOM </span>
29291 [root@clsn scripts]# echo $RANDOM 5560 [root@clsn scripts]# echo $RANDOM 2904
<h3>
<span id="152_echo">1.5.2 echo 命令输出带颜色字符</span>
</h3>
<p>
<strong><em># </em></strong><strong><em>彩色字体</em></strong>
</p>
<div>
<div class="cnblogs_code">
<pre>echo -e <span style="color: #800000;">"</span><span style="color: #800000;">\033[30m 黑色字 clsn \033[0m</span><span style="color: #800000;">"</span><span style="color: #000000;">
echo -e "\033[31m 红色字 clsn \033[0m" echo -e "\033[32m 绿色字 clsn \033[0m" echo -e "\033[33m 黄色字 clsn \033[0m" echo -e "\033[34m 蓝色字 clsn \033[0m" echo -e "\033[35m 紫色字 clsn \033[0m" echo -e "\033[36m 天蓝字 clsn \033[0m" echo -e "\033[37m 白色字 clsn \033[0m"
<p>
<em>效果示意图</em>
</p>
<p style="text-align: center;" align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208164435124-419408806.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<p>
<strong><em># </em></strong><strong><em>彩色底纹</em></strong>
</p>
<div>
<div class="cnblogs_code">
<pre>echo -e <span style="color: #800000;">"</span><span style="color: #800000;">\033[40;37m 黑底白字 clsn \033[0m</span><span style="color: #800000;">"</span><span style="color: #000000;">
echo -e "\033[41;37m 红底白字 clsn \033[0m" echo -e "\033[42;37m 绿底白字 clsn \033[0m" echo -e "\033[43;37m 黄底白字 clsn \033[0m" echo -e "\033[44;37m 蓝底白字 clsn \033[0m" echo -e "\033[45;37m 紫底白字 clsn \033[0m" echo -e "\033[46;37m 天蓝白字 clsn \033[0m
<p>
<em>效果示意图</em>
</p>
<p style="text-align: center;" align="center">
<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20171208164512390-921646908.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Shell编程基础篇-下" alt="" />
</p>
<p>
<strong><em># </em></strong><strong><em>特效字体</em></strong>
</p>
<div>
<div class="cnblogs_code">
<pre>\033<span style="color: #000000;">[0m 关闭所有属性
</span>033[1m 设置高亮度 </span>033[4m 下划线 </span>033[5m 闪烁 </span>033[7m 反显 </span>033[8m 消隐 </span>033[30m — \033[37m 设置前景色 </span>033[40m — \033[47m 设置背景色 </span>033[nA 光标上移 n 行 </span>033[nB 光标下移 n 行 </span>033[nC 光标右移 n 行 </span>033[nD 光标左移 n 行 </span>033[y;xH 设置光标位置 </span>033[2J 清屏 </span>033[K 清除从光标到行尾的内容 </span>033展开# cat –help 用法:cat [选项]… [文件]… 将[文件]或标准输入组合输出到标准输出。
-A, –show-all 等于-vET -b, –number-nonblank 对非空输出行编号 -e 等于-vE -E, –show-ends 在每行结束处显示"$" -n, –number 对输出的所有行编号 -s, –squeeze-blank 不输出多行空行 -t 与-vT 等价 -T, –show-tabs 将跳格字符显示为^I -u (被忽略) -v, –show-nonprinting 使用^ 和M- 引用,除了LFD和 TAB 之外
没有效果; 此命令不做任何操作。
退出状态:
总是成功。</span></pre>
<li>
<a href="#12_if">1.2 if条件语句</a><ul>
<li>
<a href="#121">1.2.1 三种语法</a>
</li>
<li>
<a href="#122_if">1.2.2 if条件语句小结</a>
</li>
<li>
<a href="#123_12ifnbsp">1.2.3 【练习题1】输入2个数字,比较大小(使用if语句结合条件表达式实现) </a>
</li>
<li>
<a href="#124_2100M3">1.2.4 【练习题2】系统内存低于100M邮件报警,加入计划任务,3分钟检查一次。</a>
</li>
<li>
<a href="#125_3nginx">1.2.5 【练习题3】模拟编写启动nginx脚本</a>
</li>
<li>
<a href="#126_5WebMySQL">1.2.6 【练习题5】Web及MySQL服务异常监测案例</a>
</li>
</ul>
</li>
<li>
<a href="#13_case">1.3 case条件结构语句</a><ul>
<li>
<a href="#131_case">1.3.1 case语法结构</a>
</li>
<li>
<a href="#132_caseif">1.3.2 case与if的对比</a>
</li>
<li>
<a href="#133_case">1.3.3 case值的书写方式</a>
</li>
<li>
<a href="#134_case">1.3.4 case语句小结</a>
</li>
<li>
<a href="#135_1case">1.3.5 【练习题1】使用case编写一个菜单脚本</a>
</li>
<li>
<a href="#136_2">1.3.6 【练习题2】输出带有颜色的水果菜单</a>
</li>
<li>
<a href="#137">1.3.7 写脚本规范及注意事项</a>
</li>
</ul>
</li>
<li>
<a href="#14">1.4 练习题</a><ul>
<li>
<a href="#141_1Memcached">1.4.1 【练习题1】监控Memcached缓存服务是否正常</a>
</li>
<li>
<a href="#142_2casersync">1.4.2 【练习题2】使用(case)编写rsync管理脚本</a>
</li>
<li>
<a href="#143_3">1.4.3 【练习题3】菜单自动化软件部署脚本实践</a>
</li>
<li>
<a href="#144_if_case">1.4.4 if 与 case 对比</a>
</li>
</ul>
</li>
<li>
<a href="#15">1.5 其他补充说明</a><ul>
<li>
<a href="#151_linux">1.5.1 linux中产生随机数的方法</a>
</li>
<li>
<a href="#152_echo">1.5.2 echo 命令输出带颜色字符</a>
</li>
<li>
<a href="#154">1.5.4 排错技巧</a>
</li>
<li>
<a href="#155_shell">1.5.5 shell 脚本段注释方法</a>
</li>
<li>
<a href="#156">1.5.6 其他补充</a>
</li>
</ul>
</li>
<li>
<a href="#16">1.6 参考文献</a>
</li>
- 原文作者:惨绿少年
- 原文链接:https://clsn.io/clsn/lx494.html
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。