一、变量的命名
变量名必须以字母或下划线开头,由字母、数字、或下划线组成,变量名的长度不能超过255个字符。
二、变量的分类
变量的分类主要为以下几种
1.用户自定义变量
2. 环境变量
3. 位置参数变量
4. 预定义变量
三、用户自定义变量
用户自定义变量是“局部变量”,只能在当前的Shell中生效
- 定义变量
变量名=变量值
变量名=‘变量值’
变量名=“变量值”
[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# c="hello"
[root@localhost mnt]# a="westos-$c"
[root@localhost mnt]# echo $a
westos-hello
-
变量调用
$变量名 或 ${变量名} [root@localhost mnt]# echo $a 1 [root@localhost mnt]# echo $b 2
-
变量叠加
[root@localhost mnt]# a=${a}666
[root@localhost mnt]# echo $a
1666
4.删除变量
unset 变量名称
[root@localhost mnt]# unset a
[root@localhost mnt]# echo $a
5.查看变量
env:查看所有的环境变量
set:查看所有的变量(包括用户自定义变量)
四、环境变量
环境变量是“全局变量”,在当前Shell和这个Shell的所有子Shell中生效
五、位置变量
$0:获取脚本文件名,如果执行时包含路径,则输出脚本路径
[root@localhost mnt]# vim hello.sh
[root@localhost mnt]# cat hello.sh
#!/bin/bash
echo $0
[root@localhost mnt]# sh hello.sh
hello.sh
[root@localhost mnt]#
2.$n
n为数字,$0代表命令本身,$1-
9
代
表
第
一
到
第
九
个
参
数
,
十
以
上
的
参
数
需
要
用
大
括
号
包
含
,
如
9代表第一到第九个参数,十以上的参数需要用大括号包含,如
9代表第一到第九个参数,十以上的参数需要用大括号包含,如{10}。
[root@localhost mnt]# vim hello.sh
[root@localhost mnt]# cat hello.sh
#!/bin/bash
echo $1 $2
[root@localhost mnt]# sh hello.sh haha nihao
haha nihao
[root@localhost mnt]# sh hello.sh li nihao
li nihao
[root@localhost mnt]# echo \${1..10} > hello.sh
[root@localhost mnt]# cat hello.sh
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[root@localhost mnt]# cat hello.sh
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[root@localhost mnt]# sh hello.sh {1..10}
1 2 3 4 5 6 7 8 9 10
3.$#
这个变量代表命令行中的所有参数的个数
[root@localhost mnt]# cat hello.sh
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[root@localhost mnt]# vim hello.sh
[root@localhost mnt]# cat hello.sh
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
echo $#
[root@localhost mnt]# sh hello.sh {1..50}
1 2 3 4 5 6 7 8 9 10
50
4.$?:
表示上条命令执行结果的返回值
0表示执行成功
非0表示执行失败
六.read命令
接受键盘输入
参数说明:
-p “提示信息”: 在等待read输入时,输出的提示信息
-t 秒数:read命令会一直等待用户输入,使用此参数可以指定等待时间
-n 字符数:read命令只接受指定的字符数,就会继续执行
-s : 隐藏输入的数据,适用于机密信息的输入
[root@localhost mnt]# read str
westos hello
[root@localhost mnt]# echo $str
westos hello
七.变量的数值计算
1)expr命令:expr命令一般用于整数值,但也可用于字符串
[root@localhost mnt]# a=222
[root@localhost mnt]# expr $a + 10
232
[root@localhost mnt]# expr $a - 10
212
[root@localhost mnt]# expr $a \* 10 \为转义字符
2220
[root@localhost mnt]# expr $a / 10
22
[root@localhost mnt]# expr $a % 10
2
2)$[ ]
和$(())
表达式
[root@localhost mnt]# echo $[a+10]
232
[root@localhost mnt]# echo $[a-10]
212
[root@localhost mnt]# echo $[a*10]
2220
[root@localhost mnt]# echo $[a/10]
22
[root@localhost mnt]# echo $[a%10]
2
[root@localhost mnt]# echo $((a+10))
232
3)let命令(let命令在执行后会保存新的值)
[root@localhost mnt]# a=50
[root@localhost mnt]# let a+=10
[root@localhost mnt]# echo $a
60
[root@localhost mnt]# let a-=10
[root@localhost mnt]# echo $a
50
[root@localhost mnt]# let a/=10
[root@localhost mnt]# echo $a
5
[root@localhost mnt]# let a%=10
[root@localhost mnt]# echo $a
5
4)小数运算工具bc
[root@localhost mnt]# echo "scale=2;2.46*4.55" | bc 其中的scale=2指的是保留2位小数
11.19
[root@localhost mnt]# echo "scale=4;2.42*4.55" | bc
11.0110
5)计算两个数的加减乘除
[root@localhost mnt]# vim jisuan.sh
[root@localhost mnt]# sh jisuan.sh
请输入两个整数:2 3
a+b=5
a-b=-1
a*b=6
a/b=0
a**b=8
a%b=2
[root@localhost mnt]# cat jisuan.sh
#!/bin/bash
read -t 5 -p "请输入两个整数:" a b
echo "a+b=$[a+b]"
echo "a-b=$[a-b]"
echo "a*b=$[a*b]"
echo "a/b=$[a/b]"
echo "a**b=$[a**b]"
echo "a%b=$[a%b]"
[root@localhost mnt]#
6)test命令
test "$a" == "$b" 等同于 [ "$a" == "$b" ]
[ "$a" = "$b" ] ##等于
[ "$a" != "$b" ] ##不等于
[ "$a" -eq "$b" ] ##等于
[ "$a" -ne "$b" ] ##不等于
[ "$a" -le "$b" ] ##小于等于
[ "$a" -ge "$b" ] ##大于等于
[ "$a" -gt "$b" ] ##大于
[ "$a" -lt "$b" ] ##小于
[ "$a" -ne "$b" -a "$a" -gt "$b" ] ##-a必须条件都满足
[ "$a" -ne "$b" -o"$a" -gt "$b" ] ##-a条件至少满足一个
[ -z "$a" ] ##是否为空
[ -e "file" ] ##是否存在
[ -f "file" ] ##普通文件
[ -b "file" ] ##块设备
[ -S "file" ] ##套接字
[ -c "file" ] ##字符设备
[ -L "file" ] ##软链接