linux shell程序设计
shell程序设计语言
# 什么是shell
1.指的是shell这门语言,是一种特定语法风格,规范等
2.指的是专门用于解释执行shell这么语言的语法的应用程序,即shell解释器,我们常用的是bash
# 第一个shell脚本
echo “hello word”
一.shell脚本的运行方式
方式一:输入脚本的绝对路径或相对路径
- 注意必须执行的是一个可执行文件
- 文件添加可执行权限 chmod a+x xxx.sh
方式二:bash或sh命令
sh 文件名.sh
#注意 当脚本没有x权限时,root和文件所有者通过该方式可以执行
方式三:在脚本的路径前加“或source
source 文件名.sh
区别:
第一种和第二种会新开一个bash,不同bash中的变量无法共享
第三种是在同一个shell里执行
二、shell编程语法
1.注释
# 单行注释
以#开头的行就是注释,会被解释忽视
通过每一行加 # 来进行多行注释
# 多行注释
:<<EOF
注释内容...
注释内容...
注释内容...
EOF
;<<!
注释内容...
注释内容...
注释内容...
!
2.变量操作
- 语法定义格式
# 语法: 变量名=值
#等号的左右两边不能有空格
[root@master ~]# name="lowreed"
- 变量的引用
[root@master ~]# name="lowreed"
[root@master ~]# echo $name
lowreed
#建议使用${变量名}
- 删除变量
[root@master ~]# name="lowreed"
[root@master ~]# unset name
[root@master ~]# echo $name
[root@master ~]#
变量的使用场景
- 直接赋值
显示赋值 变量名=变量值
- 脚本运行传值
从调用脚本时传入的位置参数获取变量值:./a1.sh a1 a2 a3
需要用到$n获取第n个位置参数值,超过10需要用${n},如下
$0 $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
# 示例
[root@master soft]# cat a1.sh
#!/bin/bash
echo ${0}
echo $1
echo $2
echo $3
echo $4
echo $5
echo $6
echo $7
echo $8
echo $9
echo ${10}
echo ${11}
echo ${12}
- 用户交互传值
# read接收用户的输入,即键盘读入变量值
read -p "提示信息" 变量名
示例
red -p "请输入你的名字" name
echo $name
#输出 echo命令
#可以输出带颜色的文字
## 显示普通字符串
echo "hello world"
##显示转义字符
echo "\"Hello World\""
## 显示变量
name="zhangsan"
echo "$name Hello World"
## 显示换行
echo -e "OK! \n"
echo "Hello World"
## 显示不换行
echo -e "OK! \c"
echo "Hello World"
## 显示结果定向至文件
echo "Hello World" > myfile
## > 代表覆盖
# >> 追加写入
## 原样输出字符串
echo '$name\"'
## 显示命令执行结果
echo `date`
预定变量
| 参数处理 | 参数说明 |
|---|---|
| $# | 传递到脚本的参数个数 |
| $*/$@ | 以一个单字符串显示所有向脚本传递的参数。 |
| $$ | 脚本运行的当前进程ID号 |
| $! | 后台运行的最后一个进程的ID号 |
| $? | 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。 |
| $0 | 执行的文件名 |
3.变量
使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变。
[root@master soft]# x=999
[root@master soft]# readonly x
[root@master soft]# x=111
-bash: x: 只读变量
如何删除readonly变量
下载gdb
[root@master soft]# yum install gdb -y
删除
[root@master soft]# readonly x=123
[root@master soft]# cat << EOF | gdb
> attach $$
> call unbind_variable("a")
> detach
> EOF
4.基本数据类型
shell是一门解释类,弱类型,动态语言
数字类型
# int整型
定义:age=18
用于标识:年龄,等级,身份证号,qq号,个数
# float浮点型
定义:salary=30000.0
用于标识:工资,身高,体重
字符串类型
#在shell中,加了引号的字符就是字符串类型
定义:name='xiaohu'
用于标识:描述性的内容,如姓名,性别,国籍,种族
# 注意1:字符串包含空格必须加引号
[root@master soft]# msg="hello xiaohu"
[root@master soft]# msg=hello xiaohu
bash: xiaohu: 未找到命令...
# 注意2:连续的字符不加引号包含也是可以的,但我们还是强烈建议加上引号,规范一些
[root@master soft]# msg=hello
[root@master soft]# echo $msg
hello
[root@master soft]#
# 注意3:单引号与双引号是不同的
" " 弱引用,引号的特殊字符有意义
' ' 强引用,引号内所有特殊字符都取消意义
[root@master soft]# name=“xiaohu”
[root@master soft]# echo "${name} is good"
xiaohu is good
[root@master soft]# echo '${name} is good'
${name} is good
弱类型语言
[root@master soft]# x=10
[root@master soft]# y="3"
[root@master soft]# expr $x + $y
13
数组类型
数组分为两种
• 普通数组:只能使用整数作为数组索引
• 关联数组:可以使用字符串作为数组索引,需要用declare -A声明
- 普通数组
# 方式一:array=(元素1 元素2 元素3)
array=(lowreed 18 male)
# 方式二:array=([key1]=value1 [key2]=value2 [key3]=value3)
array=([1]=111 [0]="two" [2]=333)
# 方式三:依次赋值
array_new[0]=111
array_new[1]=222
array_new[2]="third"
5.变量值的相关操作
- 获取变量值的长度
[root@master lowreed]# name="huangjie"
[root@master lowreed]# echo ${#name}
8
- 切片
# ${paramter:offset:length}
[root@master soft]# echo ${msg:3} # 从3号索引开始,一直到最后
def
[root@master soft]# echo ${msg:3:2} # 从3号索引开始,往后数2个字符
de
[root@master soft]# echo ${msg::3} # 从0开始,往后数3个字符
abc
- 截断
# =================》一、砍掉左边的字符《=================
# 1.1 简单使用
[root@master soft]# url="www.shujia.com.cn"
[root@master soft]# echo ${url#www.}
shujia.com.cn
# 1.2 结合 #* 非贪婪,默认情况下*是非贪婪,尽可能地少“吃”字符
[root@master soft]# echo ${url#*w}
ww.shujia.com.cn
# 1.3 结合 ##* 贪婪,尽可能地多“吃”字符
[root@master soft]# echo ${url##*w} # *会尽可能多地吃掉字符,一直匹配到最远的那个w才停下来
.shujia.com.cn
# =================》二、砍掉右边的字符《=================
# 1.1 简单使用
[root@master soft]# url="www.shujia.com.cn"
[root@master soft]# echo ${url%.cn}
www.shujia.com
# 1.2 结合%.*非贪婪
[root@master soft]# echo ${url%.*}
www.shujia.com
# 1.3 结合%%.*贪婪
[root@master soft]# echo ${url%%.*}
www
三、运算符
1.算数运算符
| 运算符 | 说明 | 举例 |
|---|---|---|
| + | 加法 | 'expr $a + $b' 为 30。 |
| - | 减法 | 'expr $a-$b'结果为-10。 |
| * | 乘法 | 'expr $a * $b' 结果为 200。 |
| / | 除法 | 'expr$b/$a'结果为2。 |
| % | 取余 | 'expr $b % $a' 结果为0。 |
| = | 赋值 | a=$b将把变量b的值赋给a |
| == | 相等,用于比较两个数字,相同返回true | [$a == $b]返回false。 |
| != | 不相等,用于比较两个数字,不相同返回true | [$a != $b]返回true。 |
#浮点运算
bc
#整数运算
expr
$(())
$[]
let
bc 计算工具( yum install bc -y )
[root@master soft]# res=`echo "1+1" | bc`
[root@master soft]# echo $res
2
[root@master soft]# res=`echo "10%3" | bc`
[root@master soft]# echo $res
1
[root@master soft]# res=`echo "1.2+1.3" | bc`
[root@master soft]# echo $res
2.5
[root@master soft]# res=`echo "5.0+3.0" | bc`
[root@master soft]# echo $res
8.0
expr
[root@master soft]# res=`expr 5 / 3` # 不支持浮点计算
[root@master soft]# echo $res
1
[root@master soft]# res=`expr 1+1` # 注意:要有空格
[root@master soft]# echo $res
1+1
[root@master soft]# res=`expr 1 + 1`
[root@master soft]# echo $res
2
如果是乘法,如需要转义\*
[root@master soft]# echo `expr 3 \* 10`
30
[root@master soft]#
$[]
[root@master soft]# echo $[$num1+$num2] # 等同于 echo $[num1+num2]
333
[root@master soft]# echo $[1.3+3.1]
-bash: 1.3+3.1: 语法错误: 无效的算术运算符 (错误符号是 ".3+3.1")
let赋值 只支持赋值操作
[root@master soft]# let res=1+1
[root@master soft]# echo $res
2
[root@master soft]#
[root@master soft]# let res=50/5
[root@master soft]# echo $res
10
[root@master soft]# let c=1.3*3
-bash: let: c=1.3*3: 语法错误: 无效的算术运算符 (错误符号是 ".3*3"
2、文件测试运算符
#!/bin/bash
file="/var/node/test.sh"
if [ -r $file ]
then
echo "文件可读"
else
echo "文件不可读"
fi
#- r判断文件是否可读
#- w判断文件是否可写
#- x判断文件是否可以执行
#- f判断文件是否为普通文件
#- d判断文件是否是个目录
#- s判断文件是否不为空
3 字符串测试运算符
# == 判断两个字符串是否相等
[root@master soft]# [ "aaa" == "aaa" ];echo $?
0
# != 判断两个字符串是否不相等
[root@master soft]# [ "aaa" != "aaa" ];echo $?
1
# -z 判断字符串长度是否为0
[root@master soft]# ip=""
[root@master soft]# [ -z "$ip" ];echo $? # 注意引号
0
[root@master soft]# ip='1.1.1.1'
[root@master soft]# [ -z "$ip" ];echo $?
1
# -n 判断字符串长度是否不为0
[root@master soft]# [ -n "$ip" ];echo $? # 注意加引号
1
4 数值测试符(比较运算符)
| 运算符 | 说明 | 举例 |
|---|---|---|
| -eq | 检测两个数是否相等,相等返回true | [$a -eq $b ]返回 false。 |
| -ne | 检测两个数是否不相等,不相等返回true | [$a -ne $b ]返回 true。 |
| -gt | 检测左边的数是否大于右边的,如果是,返回true | [$a -gt $b ]返回 false. |
| -lt | 检测左边的数是否小于右边的,如果是,返回true | [$a -It $b ]返回 true。 |
| -ge | 检测左边的数是否大于等于右边的,如果是,返回true | [$a -ge $b ]返回 false。 |
| -le | 检测左边的数是否小于等于右边的,如果是,返回true | [$a -le $b ]返回 true. |
[root@master soft]# [ 10 -eq 10 ];echo $?
0
[root@master soft]# [ 10 -eq 10 -a 10 \> 3 ];echo $?
0
5 关系运算符
需要结合(())进行使用
[root@master soft]# x=100
[root@master soft]# (($x>10))
[root@master soft]# echo $?
0
[root@master soft]# (($x < 10));echo $?
1
[root@master soft]# (($x == 100));echo $?
0
[root@master soft]# (($x != 100));echo $?
1
[root@master soft]# (($x != 100)) && (("xiaohu" == "xiaohu"))
[root@master soft]# echo $?
1
[root@master soft]# (($x != 100)) || (("xiaohu" == "xiaohu"))
[root@master soft]# echo $?
0
[root@master soft]# (($x != 100 && "xiaohu" == "xiaohu"));echo $?
1
[root@master soft]# (($x != 100 || "xiaohu" == "xiaohu"));echo $?
0
6 赋值运算符
[root@master soft]# x=10
[root@master soft]# ((x%3))
[root@master soft]# echo $x
10
[root@master soft]# ((x%=3))
[root@master soft]# echo $x
1
# 相关括号:
格式1: test 条件表达式
格式2: [ 条件表达式 ] -eq -lt -gt -le -ge -ne
格式3: (()) 数值比较,运算 += -= %= == != < > <= >= && ||
格式4: [[ 条件表达式 ]],支持正则 =~
# 双中括号的正则
[root@master soft]# [[ "$USER" == "root" ]];echo $? # 注意内层[]中包含的内容必须左右两侧加空格
0
[root@master soft]# [[ "$USER" == "root" ]];echo $? # 一个等号也行两个等号也可以额
0
# 此外[[]]内部是可以使用正则的,注意:正则表达式不要加引号
[root@master soft]# [[ "$USER" =~ ^wyh$ ]];echo $? # 正则表达式不要加引号
1
[root@master soft]# [[ "$USER" =~ ^root$ ]];echo $? # 正则表达式不要加引号
0
[root@master soft]# [[ ! "$USER" =~ t$ ]] && echo 此时不是管理员登录 || echo 是管理员登录
是管理员登录
[root@master soft]# num1=123
[root@master soft]# [[ "$num1" =~ ^[0-9]+$ ]];echo $? # 判断是否是数字
0
[root@master soft]# [[ "$num1" =~ ^[0-9]+$ ]] && echo "是数字"
是数字
[root@master soft]# num2=abc123de
[root@master soft]# [[ "$num2" =~ ^[0-9]+$ ]];echo $?
1
[root@master soft]# [[ "$num2" =~ ^[0-9]+$ ]] || echo "num2不是数字"
num2不是数字
7 布尔运算符
| 运算符 | 说明 | 举例 |
|---|---|---|
| ! | 非运算,表达式为true则返回false,否则退回true。 | [ ! false ]返回 true。 |
| -o | 或运算,有一个表达式为true则返回true。 | [ $a -It 20 -o $b -gt 100 ]返回 true。 |
| -a | 与运算,两个表达式都为true才返回true. | [ $a -It 20 -a $b -gt 100 J 返回 false。 |
[ $a -It 20 -a $b -gt 100 ];echo $?
8 逻辑运算符
| 运算符 | 说明 | 举例 |
|---|---|---|
| && | 逻辑的AND | [[$a -It 100 && $b-gt 100 ]]返回 false |
| || | 逻辑的OR | [[$a -It 100 || $b -gt 100 ]]返回 true |
四、流程控制
1.if语句
if 条件
then
要执行的命令1
要执行的命令2
要执行的命令3
...
fi

浙公网安备 33010602011771号