1、变量定义
查看本机支持的shell
[root@VM_0_2_centos /]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
变量的定义
a=1
b=hello
c="hello world"
d='"hello world" 你好'
e=`ls`
- =左右不能有空格
- 如果内容内有空格,需要用单引号或者双引号
- 单引号和双引号的区别,单引号不支持转义,双引号支持转义
[[email protected] ~]$ a="hello testerhome"
[[email protected] ~]$ echo "$a"
hello testerhome
[[email protected] ~]$ echo '$a'
$a
2、变量使用
-
echo $a
-
echo ${a}
-
echo “$a”
-
使用$var 或 ${var}来访问变量,后者更严谨
-
变量不需要定义也可以使用,引用没定义的变量,默认为控制
3、预定义变量
- echo $PWD :显示当前路径
- echo $USER :显示当前用户
- echo $HOME :显示HOME目录的路径
- echo $PATH :显示系统环境变量
[~]$ echo $PWD
/home/21146916
[~]$ echo $HOME
/home/21146916
[~]$ echo $USER
21146916
[~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/21146916/.local/bin:/home/21146916/bin:/home/21146916/test
4、数组变量
使用()来定义数组变量,中间使用空格隔开
array=(1 2 3 4)
array=`ls`
# 打印第一个元素
array=(1 2 3 4 5)
echo ${array[0]}
1
# 打印所有元素
echo ${array[*]}
1 2 3 4 5
# 打印数组长度
echo ${#array[*]}
5
[~]$ array=`ls`
[~]$ echo ${array}
2.txt a baidu.html baidu.txt file1.txt lbtest leetcode nginx.log rand.sh test testerhome.sh testfunc1.sh testfunc2.sh test_func.sh testfunc.sh test_script.sh test.txt
5、特殊符号的使用
- 双引号用于括起一段字符串值,支持$var形式的变量替换
[~]$ a="hello world"
[~]$ echo $a
hello world
- 单引号也表示其内容是字符串,不支持转义
[~]$ a="hello world"
[[email protected] ~]$ echo "$a"
hello world
[[email protected] ~]$ echo '$a'
$a
- \反斜线,某些情况表示转义
- $(ls),表示执行ls后的结果,与反引号类似,不过可以嵌套
[root@VM_0_2_centos /]# echo $(ls)
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- 反引号,代码命令的输出
[root@VM_0_2_centos /]# echo `ls`
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- $(())对变量操作,
[root@VM_0_2_centos /]# echo $((2+3))
5
[root@VM_0_2_centos /]# a=1
[root@VM_0_2_centos /]# b=3
[root@VM_0_2_centos /]# echo $((a+b))
4
- (())是整数的扩展,把里面的变量直接当整数处理
[root@VM_0_2_centos /]# ((a=2+3))
[root@VM_0_2_centos /]# echo $a
5
- ({1…10}),表示1到10,相当于 seq 10
[root@VM_0_2_centos /]# a=({1..10})
[root@VM_0_2_centos /]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
[root@VM_0_2_centos /]# echo ${a[@]}
1 2 3 4 5 6 7 8 9 10
[root@VM_0_2_centos /]# seq 10
1
2
3
4
5
6
7
8
9
10
6、变量的类型
# 字符串型
a="hello world"
# 数字型
b=1314
# 布尔型
c=true d=false
7、数字型变量操作
整数计算
[root@VM_0_2_centos /]# a=10
[root@VM_0_2_centos /]# echo $((a+10))
20
[root@VM_0_2_centos /]# echo $((a-10))
0
[root@VM_0_2_centos /]# echo $((a*10))
100
[root@VM_0_2_centos /]# echo $((a/10))
1
浮点数计算请使用awk "BEGIN{print 1/3}"
[root@VM_0_2_centos /]# awk "BEGIN{print 3/5}"
0.6
[root@VM_0_2_centos /]# echo $((3/5))
0
8、字符串操作
- 从变量中提取值 ${value:offset}
[root@VM_0_2_centos /]# var="hello world"
[root@VM_0_2_centos /]# echo ${var:2}
llo world
- 字符串的替换:${value/pattern/string}
[root@VM_0_2_centos /]# var="hello world"
[root@VM_0_2_centos /]# echo ${var/world/python}
hello python
# 全局替换
[root@VM_0_2_centos /]# var="hello world world"
[root@VM_0_2_centos /]# echo ${var//world/python}
hello python python
9、布尔变量
- 命令输出$?
- 任务命令都会有返回值
- 0:表示正确
- 非0:表示错误
[root@VM_0_2_centos /]# ls |echo $?
0
10、判断类型
- 算术判断
- 字符串判断
- 逻辑判断
- shell内置判断
算术判断
- [ 2 -eq 2 ] 表示是相等; equal
[root@VM_0_2_centos home]# [ 2 -eq 3 ];echo $?
1
[root@VM_0_2_centos home]# [ 2 -eq 2 ];echo $?
0
- [ 2 -ne 1 ] 表示不相等; not equal
[root@VM_0_2_centos home]# [ 2 -ne 2 ];echo $?
1
[root@VM_0_2_centos home]# [ 2 -ne 3 ];echo $?
0
- [ 3 -gt 1 ] 表示大于; greater than
- [ 3 -ge 3 ] 表示大于等于;greater than equal
- [ 2 -lt 3 ] 表示小于; less than
- [ 2 -le 2 ] 表示小于等于; less than equal
字符串判断 - [ string1 = string2 ]如果两字符串相同,则结果为真
[root@VM_0_2_centos home]# [ "a" == "a" ];echo $?
0
[root@VM_0_2_centos home]# [ "a" == "b" ];echo $?
1
- [ string1 != string2 ] 如果两字符串不相同,则结果为真
- [ -n string ] 如果字符串不是空,则结果为真
[root@VM_0_2_centos home]# [ -n "" ];echo $?
1
[root@VM_0_2_centos home]# [ -n ];echo $?
0
- [ -z string ] 如果字符串是空,则结果为真
- [[ “xxxx” == x* ]] 在表达式中表⽰0或者多个字符
- [[ xxx == x?? ]] 在表达式中表⽰单个字符
逻辑判断 - [ 2 -eq 2 a 3 -gt 1 ] 表示与;and
- [ 2 -eq 2 o 3 -gt 1 ] 表示或;or
- [[ 2 -eq 2 && 3 -gt 1 ]] 表示与;
- [[ 2 -eq 2 || 3 -gt 1 ]] 表示或;
- [ ! 2 -eq 3 ] 表示非
内置判断 - -e file: 判断文件是否存在
- -d file; 判断是否为目录
- -f file: 判断是否为文件
- -r file: 判断文件是否可读
- -x file: 判断文件是否可执行
- -w file: 判断文件是否可写
- -s file: 判断文件长度是否为0