shell 变量
变量的类型
-
自定义变量
定义变量:
变量名=变量值 变量名必须以字母或下划线开头,区分大小写 ip1=192.168.2.115
引用变量:
$变量名 或 ${变量名}
查看变量:
echo $变量名 set(所有变量:包括自定义变量和环境变量)
取消变量:
unset 变量名
作用范围: 仅在当前shell中有效
-
环境变量
定义环境变量:
方法一 export back_dir2=/home/backup
方法二 export back_dir1 将自定义变量转换成环境变量
引用环境变量:
$变量名 或 ${变量名}
查看环境变量:
echo $变量名 env 例如env |grep back_dir2
取消环境变量:
unset 变量名
变量作用范围: 在当前shell和子shell有效
-
位置变量
$1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
-
预定义变量
$* 所有的参数 $@ 所有的参数 $# 参数的个数 $$ 当前进程的PID $? 上一个命令的返回值 0表示成功 $0 返回本脚本的文件名 定义或引用变量时注意事项: " " 弱引用 ' ' 强引用 ` ` 命令替换 等价于 $() 反引号中的shell命令会被先执行
变量的运算
-
整数运算
1. 方法一:expr expr 1 + 2 expr $num1 + $num2 + - \* / % 方法二:$(()) echo $(($num1+$num2)) + - * / % echo $((num1+num2)) echo $((5-3*2)) echo $(((5-3)*2)) sum=$((1+2)); echo $sum 方法三:$[] echo $[5+2] + - * / % echo $[5**2] 方法四:let let sum=2+3; echo $sum
- 小数运算
echo "2*4" |bc echo "2^4" |bc echo "scale=2;6/4" |bc
字符串操作
条件测试
格式
格式 :
格式1: test 条件表达式
格式2: [ 条件表达式 ]
格式3: [[ 条件表达式 ]]
文件测试 [ 操作符 文件或目录 ]
[ -e dir|file ] 目录或文件是否存在
[ -d dir ] 是否为目录
[ -f file ] 是否存在,而且是文件
[ -r file ] 当前用户对该文件是否有读权限
[ -x file ] execute
[ -w file ] write
[ -L file ] link
数值比较 [ 整数1 操作符 整数2 ]
[ 1 -gt 10 ] 大于
[ 1 -lt 10 ] 小于
[ 1 -eq 10 ] 等于
[ 1 -ne 10 ] 不等于
[ 1 -ge 10 ] 大于等于
[ 1 -le 10 ] 小于等于
字符串比较
[root@master ~]# [ "$USER" = "root" ];echo $?
0
[root@master ~]# [ "$USER" == "root" ];echo $?
0
使用正则的条件判断
[root@master ~]# [[ "$USER" =~ ^r ]];echo $? //使用正则
0
判断变量是不是数字:
[root@master ~]# num10=123
[root@master ~]# num20=ssss1114ss
[root@master ~]# [[ "$num10" =~ ^[0-9]+$ ]];echo $?
0
[root@master ~]# [[ "$num20" =~ ^[0-9]+$ ]];echo $?
() 子shell中执行
(()) 数值比较,运算 C语言
$() 命令替换
$(()) 整数运算
[] 条件测试
[[]] 条件测试,支持正则 =~
$[] 整数运算
if 语句
单分支结构
if 条件测试
then 命令序列
fi
双分支结构
if 条件测试
then 命令序列
else 命令序列
fi
多分支结构
if 条件测试1
then 命令序列
elif 条件测试2
then 命令序列
elif 条件测试3
then 命令序列]...
else 命令序列
fi
#判断当前年是否为闰年
year=$(date +%Y)
if [[ $[$year % 100] == 0 && $[$year % 400] == 0 ]] ;then
echo "ok"
elif [[ $[$year % 4] == 0 && $[$year % 100] != 0 ]];then
echo "ok"
else
echo "fail"
fi
case 语句
case 变量 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
模式3)
命令序列3
;;
*)
无匹配后命令序列
esac
#!/bin/bash
if [ -f /etc/init.d/$1 ] ;then
case $2 in
"start")
/etc/init.d/$1 start;;
"stop")
/etc/init.d/$1 stop;;
"restart")
/etc/init.d/$1 restart;;
"reload")
/etc/init.d/$1 reload;;
*)
echo "usage:/etc/init.d/$1 start|stop|restart|reload";;
esac
else
echo "service name error"
fi
for 循环语句
for 变量名 [ in 取值列表 ]
do
循环体
done
#!/bin/bash
#求100以内的素数
for((i=2;i<=100;i++))
do
yn="y"
for((y=2;y<=$[$i/2];y++))
do
if [ $[$i % $y] == 0 ];then
yn="n"
break
fi
done
if [ "$yn" = "y" ];then
echo $i
fi
done
while /until
while 条件测试
do
循环体
done
==当条件测试成立(条件测试为真),执行循环体
二、until语法结构
until 条件测试
do
循环体
done
==当条件测试成立(条件测试为假),执行循环体
#!/bin/bash
x=1
while((x<10));do
if [ $[$x%2] -eq 0 ];then
((x++));
continue
fi
echo $x
((x++));
done
数组
一、普通(索引)数组
定义数组:
方法一: 一次赋一个值 数组名[下标]=变量值
array1[0]=pear
array1[1]=apple
[root@master ~]# array1[2]=orange
[root@master ~]# array1[3]=peach
方法二: 一次赋多个值
[root@master ~]# array2=(tom jack alice)
[root@master ~]# array3=(`cat /etc/passwd`) 希望是将该文件中的每一个行作为一个元数赋值给数组array3
[root@master ~]# array4=(`ls /var/[ftp/Shell/for*`)](ftp/shell/for*`))
[root@master ~]# array5=(tom jack alice "bash shell")
查看数组:
# declare -a
declare -a array1='([0]="pear" [1]="apple" [2]="orange" [3]="peach")'
declare -a array2='([0]="tom" [1]="jack" [2]="alice")'
访问数组元数:
[root@master ~]# echo ${array1[0]} 访问数组中的第一个元数
[root@master ~]# echo ${array1[@]} 访问数组中所有元数 等同于 echo ${array1[*]}
[root@master ~]# echo ${#array1[@]} 统计数组元数的个数
[root@master ~]# echo ${!array2[@]} 获取数组元数的索引
[root@master ~]# echo ${array1[@]:1} 从数组下标1开始
[root@master ~]# echo ${array1[@]:1:2} 从数组下标1开始,访问两个元素
遍历数组:
方法一: 通过数组元数的个数进行遍历
方法二: 通过数组元数的索引进行遍历
二、关联数组
定义关联数组(必须显式声明,隐式声明会出问题):
申明关联数组变量
# declare -A ass_array1
# declare -A ass_array2
方法一: 一次赋一个值
数组名[索引]=变量值
ass_array1[index1]=pear
# ass_array1[index2]=apple
# ass_array1[index3]=orange
# ass_array1[index4]=peach
方法二: 一次赋多个值
# ass_array2=([index1]=tom [index2]=jack [index3]=alice [index4]='bash shell')
查看数组:
# declare -A
declare -A ass_array1='([index4]="peach" [index1]="pear" [index2]="apple" [index3]="orange" )'
declare -A ass_array2='([index4]="bash shell" [index1]="tom" [index2]="jack" [index3]="alice" )'
访问数组元数:
# echo ${ass_array2[index2]} 访问数组中的第二个元数
# echo ${ass_array2[@]} 访问数组中所有元数 等同于 echo ${array1[*]}
# echo ${#ass_array2[@]} 获得数组元数的个数
# echo ${!ass_array2[@]} 获得数组元数的索引
函数
格式
方法一:
函数名() {
函数要实现的功能代码
}
方法二:
function 函数名 {
函数要实现的功能代码
}
调用函数
函数名
函数名 参数1 参数2
注意:bash shell支持函数传递位置参数
例如:
function test1(){
sum=$[$1+$2]
}
test1 3 4
echo $sum
函数返回值 (bash shell支持使用return 返回值,返回值存储在变量$?中)
例如:
function test2(){
return 3
}
test2
echo $?
shell相互调用
source script_file_name
. script_file_name