Shell编程基础篇-上
1.1 前言
1.1.1 为什么学Shell
1.1.2 什么是shell
这种对话方式可以是:
1.1.3 什么是Shell脚本
shell脚本擅长处理纯文本类型的数据,而linux中,几乎所有的配置文件,日志,都是纯文本类型文件
1.1.4 脚本语言的种类
一、编译型语言
定义:指用专用的编译器,针对特定的操作平台(操作系统)将某种高级语言源代码一次性翻译成可被硬件平台直接运行的二进制机器码(具有操作数,指令、及相应的格式),这个过程叫做编译(./configure make makeinstall );编译好的可执行性文件(.exe),可在相对应的平台上运行(移植性差,但运行效率高)。。
二、解释型语言
定义:指用专门解释器对源程序逐行解释成特定平台的机器码并立即执行的语言;相当于把编译型语言的编译链接过程混到一起同时完成的。
三、脚本语言
定义:为了缩短传统的编写-编译-链接-运行(edit-compile-link-run)过程而创建的计算机编程语言。
其他常用的脚本语句种类
Shell脚本与php/perl/python语言的区别和优势?
PHP、Python优势在于开发运维工具以及web界面的管理工具,web业务的开发等。处理一键软件安装、优化,报警脚本。常规业务的应用等php/python也是能够做到的。但是开发效率和复杂比用shell就差很多了。
系统环境说明
1.1.5 系统中的shell
查看系统中的命解释器
常用操作系统的默认shell
bash****版本
这是自由软件,您可以自由地更改和重新发布。 在法律允许的范围内没有担保.
bash ****破壳漏洞
#解决办法 升级当前的bash版本 yum install update bash
sh与bash 的关系
/bin与 /user/bin 的关系
1.2 脚本书写规范
1.2.1 脚本统一存放目录
1.2.2 选择解释器
==> /etc/init.d/netconsole <== #!/bin/bash
==> /etc/init.d/network <== #! /bin/bash
1.2.3 编辑脚本使用vim
使用 .vimrc 文件,能够快速的生成开头的注释信息
func SetTitle() if expand("%:e") == ’sh’ call setline(1,"#!/bin/bash") call setline(2, "##############################################################") call setline(3, "# File Name: ".expand("%")) call setline(4, "# Version: V1.0") call setline(5, "# Author: clsn") call setline(6, "# Organization: http://blog.znix.top") call setline(7, "# Created Time : ".strftime("%F %T")) call setline(8, "# Description:") call setline(9, "##############################################################") call setline(10, "") endif endfunc
使用后的效果
1.2.4 文件名规范
名字要有意义,并且结尾以 .sh 结束
1.2.5 开发的规范和习惯小结
1.3 shell脚本的执行
1.3.1 执行脚本的办法
source 与 . (点) ****的作用
soucre__命令
. (__点)
1.3.2 sh 于 source的区别
面试题:
问sh test.sh后echo $user返回的结果空_ ?
1.4 Shell的变量
1.4.1 什么是变量
变量可以分为两类:环境变量(全局变量)和普通变量(局部变量)
环境变量也可称为全局变量,可以在创建他们的Shell及其派生出来的任意子进程shell中使用,环境变量又可分为自定义环境变量和Bash内置的环境变量
普通变量也可称为局部变量,只能在创建他们的Shell函数或Shell脚本中使用。普通变量一般是由开发者用户开发脚本程序时创建的。
特殊变量
1.4.2 环境变量
输出一个系统中的 环境变量
1.4.3 普通变量
定义普通变量实践
1.4.4 export命令
标记每个 NAME 名称为自动导出到后续命令执行的环境。如果提供了 VALUE 则导出前将 VALUE 作为赋值。
export****命令的说明
# 定义变量
# 当前窗口查看
# 编写测试脚本
# 使用sh执行
1
# 使用source 执行
1.4.5 环境变量相关配置文件
四文件读取顺序(CentOS6和7都一样)
验证四文件读取顺序的方法
1.4.6 环境变量的知识小结
1.4.7 变量中引号的使用
1.4.8 普通变量的要求
变量使用反引号赋值
[root@clsn scripts]# file=ls
[root@clsn scripts]# echo $file
clsn_test.sh panduan.sh quanju.sh yhk.sh
使用${}
编写脚本测试${}
[root@clsn scripts]# sh bianliang.sh
2017年 12月 05日 星期二 09:11:19 CSTday
1.4.9 定义变量名技巧
示例:
1.5 特殊变量
1.5.1 位置变量
常用的特殊位置参数说明
<td style="width: 85.12%; border-top: 1pt solid #5b9bd5; border-right: 1pt solid #5b9bd5; border-bottom: 1pt solid #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: center;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; times new roman"4times new roman";times new roman";color: white;">作用说明</span></strong>
</p>
</td>
<td style="width: 85.12%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">获取当前执行的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">shell</span><span style="font-size: 10.0pt; font-family: 宋体;">脚本的文件名,如果执行脚本带路径那么就包括脚本路径。</span>
</p>
</td>
<td style="width: 85.12%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">获取当前执行的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">shell</span><span style="font-size: 10.0pt; font-family: 宋体;">脚本的第</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">n</span><span style="font-size: 10.0pt; font-family: 宋体;">个参数值,</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">n=1..9</span><span style="font-size: 10.0pt; font-family: 宋体;">,当</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">n</span><span style="font-size: 10.0pt; font-family: 宋体;">为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;"></span><span style="font-size: 10.0pt; font-family: 宋体;">时表示脚本的文件名,如果</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">n</span><span style="font-size: 10.0pt; font-family: 宋体;">大于</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">9</span><span style="font-size: 10.0pt; font-family: 宋体;">用大括号括起来</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">{10}</span><span style="font-size: 10.0pt; font-family: 宋体;">,参数以空格隔开。</span>
</p>
</td>
<td style="width: 85.12%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">获取当前执行的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">shell</span><span style="font-size: 10.0pt; font-family: 宋体;">脚本后面接的参数的总个数</span>
</p>
</td>
<td style="width: 85.12%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">获取当前</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">shell</span><span style="font-size: 10.0pt; font-family: 宋体;">的所有传参的参数,不加引号同</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$@;</span><span style="font-size: 10.0pt; font-family: 宋体;">如果给</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$*</span><span style="font-size: 10.0pt; font-family: 宋体;">加上双引号,例如</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">: </span><span style="font-size: 10.0pt; font-family: 宋体;">“</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$*</span><span style="font-size: 10.0pt; font-family: 宋体;">”</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">,</span><span style="font-size: 10.0pt; font-family: 宋体;">则表示将所有的参数视为单个字符串,相当于“</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$1$2$3</span><span style="font-size: 10.0pt; font-family: 宋体;">”。</span>
</p>
</td>
<td style="width: 85.12%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">获取当前</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">shell</span><span style="font-size: 10.0pt; font-family: 宋体;">的所有传参的参数,不加引号同</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$*;</span><span style="font-size: 10.0pt; font-family: 宋体;">如果给</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$@</span><span style="font-size: 10.0pt; font-family: 宋体;">加上双引号,例如</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">: </span><span style="font-size: 10.0pt; font-family: 宋体;">“</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$@</span><span style="font-size: 10.0pt; font-family: 宋体;">”</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">,</span><span style="font-size: 10.0pt; font-family: 宋体;">则表示将所有参数视为不同的独立字符串,相当于“</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$1</span><span style="font-size: 10.0pt; font-family: 宋体;">”</span> <span style="font-size: 10.0pt; font-family: 宋体;">“</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$2</span><span style="font-size: 10.0pt; font-family: 宋体;">”</span> <span style="font-size: 10.0pt; font-family: 宋体;">“</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$3</span><span style="font-size: 10.0pt; font-family: 宋体;">”</span> <span style="font-size: 10.0pt; font-family: 宋体;">“……”,这是将参数传递给其他程序的最佳方式,因为他会保留所有内嵌在每个参数里的任何空白。</span>
</p>
</td>
$0,$1.$2 ~ ****参数实践
$# ****参数实践
echo $0 echo "第一个参数:" $1 echo "第二个参数:" $2 echo "第10个参数:" ${10} echo "第11个参数:" ${11} echo "参数个数:" $#
[root@clsn scripts]# sh chanshu.sh 55 2 3 4 5 6 7 8 9 10 11 112 chanshu.sh 第一个参数: 55 第二个参数: 2 第10个参数: 10 第11个参数: 11 参数个数: 12
_$ ****参数实践_*
$* 与 $@ 对比实践
[root@clsn scripts]# for i in $;do echo $i ;done I am handsome boy.. [root@clsn scripts]# for i in $@;do echo $i ;done I am handsome boy.. [root@clsn scripts]# for i in “$@";do echo $i ;done I am handsome boy.. [root@clsn scripts]# for i in “$";do echo $i ;done I am handsome boy..
1.5.2 进程状态变量
Shell进程的特殊状态变量说明
<td style="width: 452.15pt; border-top: 1pt solid #5b9bd5; border-right: 1pt solid #5b9bd5; border-bottom: 1pt solid #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" width="603">
<p style="margin-bottom: .0001pt; text-align: center;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; times new roman"4times new roman";times new roman";color: white;">作用说明</span></strong>
</p>
</td>
</tr>
<tr>
<td style="width: 70.65pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" width="94">
<p style="margin-bottom: .0001pt; text-align: center;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$?</span></strong>
</p>
</td>
<td style="width: 452.15pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" width="603">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">获取执行上一个指令的执行状态返回值(</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;"></span><span style="font-size: 10.0pt; font-family: 宋体;">为成功,非零为失败</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">)</span><span style="font-size: 10.0pt; font-family: 宋体;">,这个变量最常用</span>
</p>
</td>
</tr>
<tr>
<td style="width: 70.65pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; padding: 0cm 5.4pt;" width="94">
<p style="margin-bottom: .0001pt; text-align: center;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$$</span></strong>
</p>
</td>
<td style="width: 452.15pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="603">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">获取当前执行的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">Shell</span><span style="font-size: 10.0pt; font-family: 宋体;">脚本的进程号(</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">PID)</span><span style="font-size: 10.0pt; font-family: 宋体;">,这个变量不常用,了解即可</span>
</p>
</td>
</tr>
<tr>
<td style="width: 70.65pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" width="94">
<p style="margin-bottom: .0001pt; text-align: center;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$!</span></strong>
</p>
</td>
<td style="width: 452.15pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" width="603">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">获取上一个在后台工作的进程的进程号(</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">PID)</span><span style="font-size: 10.0pt; font-family: 宋体;">,这个变量不常用,了解即可</span>
</p>
</td>
</tr>
<tr>
<td style="width: 70.65pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; padding: 0cm 5.4pt;" width="94">
<p style="margin-bottom: .0001pt; text-align: center;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$_</span></strong>
</p>
</td>
<td style="width: 452.15pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="603">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">获取在此之前执行的命令或脚本的最后一个参数,这个变量不常用,了解即可</span>
</p>
</td>
</tr>
进程参数实践
1.5.3 echo参数说明
<td style="width: 345.85pt; border-top: 1pt solid #5b9bd5; border-right: 1pt solid #5b9bd5; border-bottom: 1pt solid #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" valign="top" width="461">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 宋体; times new roman"4times new roman";times new roman";color: white;">参数说明</span></strong>
</p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="236">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">-n</span></strong>
</p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="461">
<p style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体;">不要追加换行</span>
</p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; padding: 0cm 5.4pt;" valign="top" width="236">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">-e</span></strong>
</p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="461">
<p style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体;">启用下列反斜杠转义的解释</span>
</p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="236">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">-E</span></strong>
</p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="461">
<p style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体;">显式地抑制对于反斜杠转义的解释</span>
</p>
</td>
</tr>
<tr>
<td style="width: 522.8pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; background: #5b9bd5; padding: 0cm 5.4pt;" colspan="2" valign="top" width="697">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; color: white;">`echo' </span></strong><strong><span style="font-size: 10.0pt; font-family: 宋体; times new roman"4times new roman";times new roman"; color: white;">对下列反斜杠字符进行转义:</span></strong>
</p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="236">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">\n</span></strong>
</p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="461">
<p style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体;">换行</span>
</p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; padding: 0cm 5.4pt;" valign="top" width="236">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">\r</span></strong>
</p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="461">
<p style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体;">回车</span>
</p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="236">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">\t</span></strong>
</p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="461">
<p style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体;">横向制表符</span>
</p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; padding: 0cm 5.4pt;" valign="top" width="236">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">\b</span></strong>
</p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="461">
<p style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体;">退格</span>
</p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; background: #deeaf6; padding: 0cm 5.4pt;" valign="top" width="236">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">\v</span></strong>
</p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="461">
<p style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体;">纵向制表符</span>
</p>
</td>
</tr>
<tr>
<td style="width: 176.95pt; border-right: 1pt solid #9cc2e5; border-bottom: 1pt solid #9cc2e5; border-left: 1pt solid #9cc2e5; border-top: none; padding: 0cm 5.4pt;" valign="top" width="236">
<p style="margin-bottom: .0001pt;">
<strong><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">\c</span></strong>
</p>
</td>
<td style="width: 345.85pt; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="461">
<p style="margin-bottom: .0001pt;">
<span style="font-size: 10.0pt; font-family: 宋体;">抑制更多的输出</span>
</p>
</td>
</tr>
1.6 定义变量的方式
1.6.1 三种定义变量的方式
1.6.2 read命令说明
在命令行中使用
在脚本中使用
echo $clsn
执行结果
read命令的帮助说明
1.6.3 定义方法实践
直接赋值方法
echo $hobby ls $ethFile [root@clsn scripts]# sh bianliang.sh bianliang.sh chanshu.sh clsn.sh clsn_test.sh panduan.sh quanju.sh xiugaizhuji.sh yhk.sh /etc/sysconfig/network-scripts/ifcfg-eth0
传参 (****传递参数)
echo $hobby ls $ethFile [root@clsn scripts]# sh bianliang.sh clsn /etc/hostname clsn /etc/hostname
交互式设置变量 read
1.6.4 写一个交互脚本,实现能够定义主机名及IP地址
脚本内容****↓
read -p "请输入主机名:" Hostname read -p "请输入IP地址的主机位:" HostIP
hostnamectl set-hostname $Hostname
sed -i "s#${Now_eth}#${HostIP}#g" $ethFile
read -p "是否重启服务器:{yes/no}" REboot
if [ $REboot == yes ] then echo "系统将在10秒后重启!" shutdown -r 10 else echo "请稍后手动重启系统!" fi
脚本测试结果
1.7 变量的子串
1.7.1 变量子串说明
<td style="width: 60.74%; border-top: 1pt solid #5b9bd5; border-right: 1pt solid #5b9bd5; border-bottom: 1pt solid #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: center;" align="center">
<strong><span style="font-size: 10.0pt; font-family: 宋体; times new roman"4times new roman";times new roman";color: white;">说明</span></strong>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">返回变量</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">$parameter</span><span style="font-size: 10.0pt; font-family: 宋体;">的内容</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体; times new roman"4times new roman";times new roman";background: yellow;">返回变内容的长度(按字符</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif; background: yellow;">)</span><span style="font-size: 10.0pt; font-family: 宋体; times new roman"4times new roman";times new roman";background: yellow;">,也适用于特殊变量</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">在变量</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">${parameter}</span><span style="font-size: 10.0pt; font-family: 宋体;">中,从位置</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">offset</span><span style="font-size: 10.0pt; font-family: 宋体;">之后开始提取子串到结尾</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">在变量</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">${parameter}</span><span style="font-size: 10.0pt; font-family: 宋体;">中,从位置</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">offset</span><span style="font-size: 10.0pt; font-family: 宋体;">之后开始提取长度为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">length</span><span style="font-size: 10.0pt; font-family: 宋体;">的子串</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">从变量</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">${parameter}</span><span style="font-size: 10.0pt; font-family: 宋体;">开头开始删除最短匹配的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">word</span><span style="font-size: 10.0pt; font-family: 宋体;">子串</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">从变量</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">${parameter}</span><span style="font-size: 10.0pt; font-family: 宋体;">开头开始删除最长匹配的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">word</span><span style="font-size: 10.0pt; font-family: 宋体;">子串</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">从变量</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">${parameter}</span><span style="font-size: 10.0pt; font-family: 宋体;">结尾开始删除最短匹配的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">word</span><span style="font-size: 10.0pt; font-family: 宋体;">子串</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">从变量</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">${parameter}</span><span style="font-size: 10.0pt; font-family: 宋体;">结尾开始删除最长匹配的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">word</span><span style="font-size: 10.0pt; font-family: 宋体;">子串</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">使用</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">string</span><span style="font-size: 10.0pt; font-family: 宋体;">代替第一个匹配的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">pattern</span>
</p>
</td>
<td style="width: 60.74%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" valign="top" width="60%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">使用</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">string</span><span style="font-size: 10.0pt; font-family: 宋体;">代替所有匹配的</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">pattern</span>
</p>
</td>
计算变赋值的长度
real 0m0.002s user 0m0.002s sys 0m0.000s [root@clsn scripts]# time echo ${#clsn} 20
real 0m0.000s user 0m0.000s sys 0m0.000s
截取变量中的字符
替换变量内容
有关上述匹配删除的小结
1.7.2 Shell的特殊扩展变量说明
<td style="width: 85.42%; border-top: 1pt solid #5b9bd5; border-right: 1pt solid #5b9bd5; border-bottom: 1pt solid #5b9bd5; border-left: none; background: #5b9bd5; padding: 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<strong><span style="font-size: 10.0pt; font-family: 宋体; times new roman"4times new roman";times new roman"; color: white;">说</span></strong><strong><span style="font-size: 10.0pt; font-family: 宋体; times new roman"4times new roman";times new roman"; color: white;">明</span></strong>
</p>
</td>
<td style="width: 85.42%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">如果</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">parameter</span><span style="font-size: 10.0pt; font-family: 宋体;">的变量值为空或未赋值,则会返回</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">word</span><span style="font-size: 10.0pt; font-family: 宋体;">字符串并替代变量的值用途</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">.</span><span style="font-size: 10.0pt; font-family: 宋体;">如果变量未定义,则返回备用的值,防止变量为空值或因未定义而导致异常</span>
</p>
</td>
<td style="width: 85.42%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">如果</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">parameter</span><span style="font-size: 10.0pt; font-family: 宋体;">的变量值为空或未赋值,则设置这个变量值为</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">word,</span><span style="font-size: 10.0pt; font-family: 宋体;">并返回其值。位置变量和特殊变量不适用用途:基本同上一个</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">${parameter>word}</span><span style="font-size: 10.0pt; font-family: 宋体;">,但该变量又额外给</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">parameter</span><span style="font-size: 10.0pt; font-family: 宋体;">变量赋值了</span>
</p>
</td>
<td style="width: 85.42%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; background: #DEEAF6; padding: 0cm 5.4pt 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">如果</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">parameter</span><span style="font-size: 10.0pt; font-family: 宋体;">变量值为空或未赋值,那么</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">word</span><span style="font-size: 10.0pt; font-family: 宋体;">字符串将被作为标准错误输出,否则输出变量的值。用途:用于捕捉由于变量未定义而导致的错误,并退出程序</span>
</p>
</td>
<td style="width: 85.42%; border-top: none; border-left: none; border-bottom: solid #9CC2E5 1.0pt; border-right: solid #9CC2E5 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="85%">
<p style="margin-bottom: .0001pt; text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: 10.0pt; font-family: 宋体;">如果</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">parameter</span><span style="font-size: 10.0pt; font-family: 宋体;">变量值为空或未赋值,则什么都不做,否则</span><span style="font-size: 10.0pt; font-family: 'Times New Roman',serif;">word</span><span style="font-size: 10.0pt; font-family: 宋体;">字符串将替代变量的值</span>
</p>
</td>
特殊变量实践
脚本内容
测试结果
/mnt /mnt
/tmp
至此shell中的变量就都介绍完了
1.8 变量的数值计算
1.8.1 仅支持整数的运算
echo $((****数学运算表达式))
示例:
记忆方法:++,–
let****命令
[root@clsn scripts]# i=1 [root@clsn scripts]# let i=i+1 [root@clsn scripts]# echo $i 2
expr ****命令
非整数返回值为2 示例:
$[]****运算符
typeset****命令进行运算
1.8.2 可以进行小数运算的命令
bc ****命令
交互模式测试bc命令
python ****命令
awk ****命令
1.8.3 运算相关练习题
1.8.3.1 【练习题】实现一个加减乘除等功能的计算器
echo $a + $b =$(($a+$b)) echo $a - $b =$(($a-$b)) echo $a </span>* $b =$(($a*$b)) echo $a / $b =$(($a/$b))
脚本执行过程:
精简方法
脚本执行过程:
1.8.3.2 【练习题】打印结果1+2+3+4+5+6+7+8+9+10=55
脚本执行结果
1.9 补充说明
1.9.1 参考文献
<li>
<a href="#12">1.2 脚本书写规范</a><ul>
<li>
<a href="#121">1.2.1 脚本统一存放目录</a>
</li>
<li>
<a href="#122">1.2.2 选择解释器</a>
</li>
<li>
<a href="#123_vim">1.2.3 编辑脚本使用vim</a>
</li>
<li>
<a href="#124">1.2.4 文件名规范</a>
</li>
<li>
<a href="#125">1.2.5 开发的规范和习惯小结</a>
</li>
</ul>
</li>
<li>
<a href="#13_shell">1.3 shell脚本的执行</a><ul>
<li>
<a href="#131">1.3.1 执行脚本的办法</a>
</li>
<li>
<a href="#132_sh_source">1.3.2 sh 于 source的区别</a>
</li>
</ul>
</li>
<li>
<a href="#14_Shell">1.4 Shell的变量</a><ul>
<li>
<a href="#141">1.4.1 什么是变量</a>
</li>
<li>
<a href="#142">1.4.2 环境变量</a>
</li>
<li>
<a href="#143">1.4.3 普通变量</a>
</li>
<li>
<a href="#144_export">1.4.4 export命令</a>
</li>
<li>
<a href="#145">1.4.5 环境变量相关配置文件</a>
</li>
<li>
<a href="#146">1.4.6 环境变量的知识小结</a>
</li>
<li>
<a href="#147">1.4.7 变量中引号的使用</a>
</li>
<li>
<a href="#148">1.4.8 普通变量的要求</a>
</li>
<li>
<a href="#149">1.4.9 定义变量名技巧</a>
</li>
</ul>
</li>
<li>
<a href="#15">1.5 特殊变量</a><ul>
<li>
<a href="#151">1.5.1 位置变量</a>
</li>
<li>
<a href="#152">1.5.2 进程状态变量</a>
</li>
<li>
<a href="#153_echo">1.5.3 echo参数说明</a>
</li>
</ul>
</li>
<li>
<a href="#16">1.6 定义变量的方式</a><ul>
<li>
<a href="#161">1.6.1 三种定义变量的方式</a>
</li>
<li>
<a href="#162_read">1.6.2 read命令说明</a>
</li>
<li>
<a href="#163">1.6.3 定义方法实践</a>
</li>
<li>
<a href="#164_IP">1.6.4 写一个交互脚本,实现能够定义主机名及IP地址</a>
</li>
</ul>
</li>
<li>
<a href="#17">1.7 变量的子串</a><ul>
<li>
<a href="#171">1.7.1 变量子串说明</a>
</li>
<li>
<a href="#172_Shell">1.7.2 Shell的特殊扩展变量说明</a>
</li>
</ul>
</li>
<li>
<a href="#18">1.8 变量的数值计算</a><ul>
<li>
<a href="#181">1.8.1 仅支持整数的运算</a>
</li>
<li>
<a href="#182">1.8.2 可以进行小数运算的命令</a>
</li>
<li>
<a href="#183">1.8.3 运算相关练习题</a><ul>
<li>
<a href="#1831nbsp">1.8.3.1 【练习题】实现一个加减乘除等功能的计算器</a>
</li>
<li>
<a href="#1832nbsp1234567891055">1.8.3.2 【练习题】打印结果1+2+3+4+5+6+7+8+9+10=55</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href="#19">1.9 补充说明</a>
</li>
<li>
<a href="#191">1.9.1 参考文献</a>
</li>
- 原文作者:惨绿少年
- 原文链接:https://clsn.io/clsn/lx520.html
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。