Linux shell 知识心得02 基本数据类型与值操作+元字符
基本数据类型与值操作
一 数据类型介绍
什么是数据?为何要有多种类型的数据?
#数据即变量的值,如age=18,18则是我们保存的数据。
#变量的是用来反映/保持状态以及状态变化的,毫无疑问针对不同的状态就应该用不同类型的数据去标识
shell是一门解释型、弱类型、动态语言
概括地说,编程语言的划分方式有以下三种
1、编译型or解释型
2、强类型or弱类型
2.1 强类型语言: 数据类型不可以被忽略的语言
即变量的数据类型一旦被定义,那就不会再改变,除非进行强转。
在python中,例如:name = 'simon',这个变量name在被赋值的那一刻,数据类型就被确定死了,是字符型,值为'simon'。
2.2 弱类型语言:数据类型可以被忽略的语言
比如linux中的shell中定义一个变量,是随着调用方式的不同,数据类型可随意切换的那种,即shell对数据类型的概念没有那么强的要求
3、动态型or静态型
3.1 动态语言 :运行时才进行数据类型检查
即在变量赋值时,就确定了变量的数据类型,不用事先给变量指定数据类型
3.2 静态语言:需要事先给变量进行数据类型定义
所以综上所述,shell是一门解释型的弱类型动态语言
二 基本数据类型
数字
#int整型
定义:age=10
用于标识:年龄,等级,身份证号,qq号,个数
#float浮点型
定义:salary=3.1
用于标识:工资,身高,体重
字符串
#在shell中,加了引号的字符就是字符串类型
定义:name='egon'
用于标识:描述性的内容,如姓名,性别,国籍,种族
# 注意1:字符串包含空格必须加引号
[root@egon ~]# msg="hello egon"
[root@egon ~]# msg=hello egon
bash: egon: 未找到命令...
# 注意2:连续的字符不加引号包含也是可以的,但我们还是强烈建议加上引号,规范一些
[root@egon ~]# msg=hello
[root@egon ~]# echo $msg
hello
[root@egon ~]#
# 注意3:单引号与双引号是不同的
" " 弱引用,引号的特殊字符有意义
' ' 强引用,引号内所有特殊字符都取消意义
[root@egon ~]# name=“egon”
[root@egon ~]# echo "${name} is good"
egon is good
[root@egon ~]# echo '${name} is good'
${name} is good
shell是弱类型语言
[root@egon ~]# x=10
[root@egon ~]# y="3"
[root@egon ~]# expr $x + $y
13
数组介绍
# 1、什么是数组?
数组就是一系列元素的集合,一个数组内可以存放多个元素
# 2、为何要用数组?
我们可以用数组将多个元素汇总到一起,避免单独定义的麻烦
数组分为两种
- 普通数组:只能使用整数作为数组索引
- 关联数组:可以使用字符串作为数组索引,需要用declare -A声明
普通数组
=================声明普通数组=================
# 方式一:array=(元素1 元素2 元素3)
array=(egon 18 male)
# 方式二:array=([key1]=value1 [key2]=value2 [key3]=value3)
array=([0]=111 [1]="two" [2]=333)
# 方式三:依次赋值
array_new[0]=111
array_new[1]=222
array_new[2]="third"
# 方式四:利用执行命令的结果设置数组元素:array=($(命令)) 或者 array=(`命令`)
该方式会将命令的结果以空格为分隔符切成多个元素然后赋值给数组
[root@aliyun ~]# ls /test
a.txt b.txt
[root@aliyun ~]# array3=(`ls /test`)
[root@aliyun ~]# declare -a |grep array3
declare -a array3='([0]="a.txt" [1]="b.txt")'
# ps:查看声明过的数组
declare -a
=================访问普通数组=================
[root@egon ~]# ip_array=(1.1.1.1 2.2.2.2 3.3.3.3)
# 正向索引
[root@egon ~]# echo ${ip_array[0]}
1.1.1.1
[root@egon ~]# echo ${ip_array[1]}
2.2.2.2
[root@egon ~]# echo ${ip_array[2]}
3.3.3.3
[root@egon ~]#
# 负向索引
[root@egon ~]# echo ${ip_array[-1]}
3.3.3.3
[root@egon ~]# echo ${ip_array[-2]}
2.2.2.2
[root@egon ~]# echo ${ip_array[-3]}
1.1.1.1
关联数组
=================声明关联数组=================
[root@aliyun ~]# declare -A info
[root@aliyun ~]# info["name"]="egon"
[root@aliyun ~]# info["age"]=18
[root@aliyun ~]# info["gender"]="male"
[root@aliyun ~]#
[root@aliyun ~]# declare -A |grep info
declare -A info='([gender]="male" [name]="egon" [age]="18" )'
[root@aliyun ~]#
[root@aliyun ~]# echo ${info[*]}
male egon 18
[root@aliyun ~]#
[root@aliyun ~]# echo ${info["name"]}
=================访问关联数组=================
[root@egon ~]# info=([0]="egon" ["age"]=18 ["gender"]="male")
[root@egon ~]# echo ${info[0]}
egon
[root@egon ~]# echo ${info["age"]}
18
[root@egon ~]# echo ${info["gender"]}
male
[root@egon ~]#
ps:bash shell只支持一维数组,但数组元素个数没有限制。
三 变量值操作
3.1 获取变量值的长度
[root@localhost ~]# echo ${#url}
15
# 企业面试题:已知变量msg='hello world!',请统计出变量中包含的字符数量
# 方法一:
[root@egon /]# echo ${#msg}
12
# 方法二:
[root@egon /]# echo $msg | wc -L
12
# 方法三:
[root@egon /]# echo $msg|awk '{print length}'
12
# 方法四:
[root@egon ~]# expr length "$msg" #length是一个函数,注意因为msg的值有空格,所以$msg必须用引号包含
12
3.2 切片
${paramter:offset:length}
[root@egon /]# msg="abcdef"
[root@egon /]# echo ${msg:3} # 从3号索引开始,一直到最后
def
[root@egon /]# echo ${msg:3:2} # 从3号索引开始,往后数2个字符
de
[root@egon /]# echo ${msg::3} # 从0开始,往后数3个字符
abc
3.3 截断
# =================》一、砍掉左边的字符《=================
# 1.1 简单使用
[root@egon ~]# url="www.sina.com.cn"
[root@egon ~]# echo ${url#www.}
sina.com.cn
# 1.2 结合*=》非贪婪,默认情况下*是非贪婪,尽可能地少“吃”字符
[root@egon ~]# echo ${url#*w}
ww.sina.com.cn
# 1.3 结合*=》贪婪,尽可能地多“吃”字符
[root@egon ~]# echo ${url##*w} # *会尽可能多地吃掉字符,一直匹配到最远的那个w才停下来
.sina.com.cn
# =================》二、砍掉右边的字符《=================
# 1.1 简单使用
[root@egon ~]# url="www.sina.com.cn"
[root@egon ~]# echo ${url%.cn}
www.sina.com
# 1.2 结合*=》非贪婪
[root@egon ~]# echo ${url%.*}
www.sina.com
# 1.3 结合*=》贪婪
[root@egon ~]# echo ${url%%.*}
www
# =================》三、应用示例《=================
[root@egon ~]# hostname
egon.xxx.com
[root@egon ~]# echo $HOSTNAME
egon.xxx.com
[root@egon ~]# echo ${HOSTNAME%.*}
egon.xxx
[root@egon ~]# echo ${HOSTNAME%%.*}
egon
3.4 内容的替换
[root@egon ~]# url="www.sina.com.cn"
[root@egon ~]# echo ${url/sina/baidu}
www.baidu.com.cn
[root@egon ~]# echo ${url/n/N}
www.siNa.com.cn
[root@egon ~]# echo ${url//n/N} # 贪婪
www.siNa.com.cN
# 应用示例:批量修改文件名称
[root@egon shell]# touch egon_2020_{01..05}_linux.txt
[root@egon shell]# ls
egon_2020_01_linux.txt egon_2020_02_linux.txt egon_2020_03_linux.txt egon_2020_04_linux.txt egon_2020_05_linux.txt
[root@egon shell]# for i in `ls *linux.txt`;do mv $i ${i/_linux/};done
[root@egon shell]# ls
egon_2020_01.txt egon_2020_02.txt egon_2020_03.txt egon_2020_04.txt egon_2020_05.txt
3.5 变量的替代
${x:-临时变量信息}
${x:=新的变量信息}
${x:?没有设置变量提示信息}
${x:+有设置变量提示信息}
#1、${parameter-word}: 当调取变量没有定义过, 就返回word字符串信息
[root@egon ~]# unset name
[root@egon ~]# echo ${name}
[root@egon ~]# echo ${name-"egon"} # 没有定义过变量name,则使用-后的值
egon
[root@egon ~]#
[root@egon ~]# gender= # 定义过变量了,则使用变量的原值,哪怕变量的值为空值
[root@egon ~]# echo ${gender-"male"}
[root@egon ~]#
[root@egon ~]# age=18
[root@egon ~]# echo ${age-19} # 定义过变量了,则使用变量的原值
18
[root@egon ~]#
#2、${parameter:-word}: 当调取变量信息值为空时或未定义变量, 就返回word字符串信息
[root@egon ~]# unset x
[root@egon ~]# unset y
[root@egon ~]# unset z
[root@egon ~]#
[root@egon ~]# echo ${x:-aaa} # 没有定义过变量x,则使用-后的值
aaa
[root@egon ~]# y=
[root@egon ~]# echo ${y:-aaa} # 定义过变量y,但变量y的值为空值,则使用-后的值
aaa
[root@egon ~]# z=333
[root@egon ~]# echo ${z:-aaa} # 定义过变量了,并且变量有一个非空的原值,则使用变量的原值
333
#3、{parameter:=word}:当调取变量信息值为空时或未定义,则设置指定字符串为新的变量值
[root@egon /]# unset x
[root@egon /]# echo ${x:=123}
123
[root@egon /]# echo $x
123
#4、${parameter:?word}:当调取变量信息值为空时或未定义,指定为赋值的错误提示信息
[root@egon /]# unset x
[root@egon /]# echo ${x:?该变量没有定义过}
-bash: x: 该变量没有定义过
#5、${parameter:+word}:当调取变量信息值为空时或未定义,不做任何处理,否则word字符串将替代变量值
[root@egon /]# unset x
[root@egon /]# echo ${x:+哈哈哈}
[root@egon /]# x=123
[root@egon /]# echo ${x:+哈哈哈}
哈哈哈
3.6 let
# (1) 变量的值
[root@egon ~]# j=1
[root@egon ~]# let ++j
[root@egon ~]# echo $j
2
[root@egon ~]#
# (2) 表达式的值
[root@egon ~]# unset i
[root@egon ~]# unset j
[root@egon ~]#
[root@egon ~]# i=1
[root@egon ~]# j=1
[root@egon ~]#
[root@egon ~]# let x=i++ # 先把i赋值给x,然后再++
[root@egon ~]# let y=++j # 先++j,然后再把j的结果赋值给y
[root@egon ~]# echo $i
2
[root@egon ~]# echo $j
3
[root@egon ~]# echo $x
1
[root@egon ~]# echo $y
2
3.7 取命令的结果赋值给变量:
# ``与$()
` ` 命令替换 等价于 $() 反引号中的shell命令会被先执行
[root@localhost ~]# touch `date +%F`_file1.txt
[root@localhost ~]# touch $(date +%F)_file2.txt
[root@localhost ~]# disk_free3="df -Ph |grep '/$' |awk '{print $4}'" # 错误
[root@localhost ~]# disk_free4=$(df -Ph |grep '/$' |awk '{print $4}') # 正确
[root@localhost ~]# disk_free5=`df -Ph |grep '/$' |awk '{print $4}'` # 正确
元字符
元字符指的是能够被shell解释的特殊字符,每个特殊字符都有其特殊含义,这些字符一方面可用于变量值的运算、我们可以称之为运算符,另外一方面可以和shell命令配合使用来达到更高级的效果
一 算数运算符
运算符
-
-
-
-
/
-
%
算数运算符需要配合下述操作使用
# 浮点运算
bc
# 整数运算
expr
$(())
$[]
let
详解如下
-
1、bc是比较常用的linux计算工具了,而且支持浮点运算
[root@localhost ~]# res=`echo 1+1 | bc` [root@localhost ~]# echo $res 2 [root@localhost ~]# res=`echo 10 % 3 | bc` [root@localhost ~]# echo $res 1 [root@localhost ~]# res=`echo 1.2+1.3|bc` [root@localhost ~]# echo $res 2.5 [root@localhost ~]# res=`echo 5.0+3.0|bc` [root@localhost ~]# echo $res 8.0 [root@localhost ~]# res=`echo "scale=2;5.0/3.0"|bc` [root@localhost ~]# echo $res 1.66 [root@localhost ~]# res=`echo "scale=2;5.0/6.0"|bc` [root@localhost ~]# echo $res .83 -
2、expr不支持浮点数计算。而且要注意数字与运算符中的空格
[root@localhost ~]# res=`expr 5 / 3` # 不支持浮点计算 [root@localhost ~]# echo $res 1 [root@localhost ~]# res=`expr 1+1` # 注意:要有空格 [root@localhost ~]# echo $res 1+1 [root@localhost ~]# res=`expr 1 + 1` [root@localhost ~]# echo $res 2 如果是乘法,如需要转义\* [root@simon ~]# expr 3 \* 10 30 [root@simon ~]# -
3、$(()) 同expr,不支持浮点数运算
# 例如: [root@localhost ~]# echo $((1+1)) 2 [root@localhost ~]# echo $((1.0+2.0)) # 不支持浮点运算符 -bash: 1.0+2.0: 语法错误: 无效的算术运算符 (错误符号是 ".0+2.0") # 注意: echo $(($num1+$num2)) # 也可以简写为 echo $((num1+num2)) echo $(((5-3)*2)) # 可以嵌套括号 -
4、$[]同expr以及$(()),不支持浮点运算
[root@simon ~]# echo $[$num1+$num2] # 等同于 echo $[num1+num2] 333 [root@simon ~]# echo $[1.3+3.1] -bash: 1.3+3.1: 语法错误: 无效的算术运算符 (错误符号是 ".3+3.1") -
5、let 不支持浮点数运算,而且不支持直接输出,只能赋值
[root@localhost ~]# let res=1+1 [root@localhost ~]# echo $res 2 [root@localhost ~]# [root@localhost ~]# let res=50/5 [root@localhost ~]# echo $res 10 [root@localhost ~]# let c=1.3*3 -bash: let: c=1.3*3: 语法错误: 无效的算术运算符 (错误符号是 ".3*3" -
6、强调:整数与非整数之间运算会报错
[root@simon ~]# expr 1 + a expr: 非整数参数 [root@simon ~]# expr 1 + 1.3 expr: 非整数参数
练习题
#1、脚本案例: 编写小脚本, 可以实现2位数加减乘除运算
[root@simon shell]# cat a.sh
#!/bin/bash
a=$1
b=$2
[ $# -ne 2 ] && {
echo "请输入两位数字信息"
exit
}
echo "a-b=$((a-b))"
echo "a+b=$((a+b))"
echo "a*b=$((a*b))"
echo "a/b=$((a/b))"
# 2、脚本案例: 编写判断输入参数是否是整数信息脚本
#!/bin/bash
a=$1
expr $a + 0 &>/dev/null # 只有整型加0才不会报错,浮点数及字符与0详解都会报错
[ $? -eq 0 ] && echo "输入的是整数信息" || echo "输入的是非整数信息"
# 3、如何利用脚本计算1+2+3+4..10总和数值
方法一:
seq -s "+" 10|bc
方法二:
echo {1..10}|tr " " "+"|bc
方法三:
awk 'BEGIN{for (i=1; i<=10; i++) a=a+i; print a}'
二 测试运算符
测试命令:test,详细可用man test查询
测试符号:[],注意只有一层中括号,中括号内左右两侧必须要有空格
test与[]效果都一样,参数也都一样
2.1 测试文件状态
-
-d 目录
[root@simon ~]# test -d /etc/ ; echo $?0[root@simon ~]# [ -d /etc ];echo $? # 注意[]内左右两侧要有空格0ps:下面关于[]用法都与test一样,不再举例 -
-s 文件长度 > 0、非空
[root@simon ~]# touch a.txt[root@simon ~]# test -s a.txt ;echo $? # 文件为空则返回假1[root@simon ~]# test -s /etc/passwd ;echo $? # 文件不为空则返回假0 -
-f 标准文件
[root@simon ~]# test -f /etc/passwd;echo $?0 -
-w 可写
[simon@simon ~]$ touch a.txt[simon@simon ~]$ chmod a-w a.txt[simon@simon ~]$ [ -w a.txt ];echo $? # 返回假,注意,如果是root用户,无论如何都有写权限1 -
-r 可读
[simon@simon ~]$ ll a.txt -r--r--r--. 1 simon simon 292 8月 17 21:48 a.txt[simon@simon ~]$ test -r a.txt;echo $?0 -
-x 可执行
[simon@simon ~]$ ll a.txt -r--r--r--. 1 simon simon 292 8月 17 21:48 a.txt[simon@simon ~]$ test -x a.txt;echo $?1 -
-L 符号连接
[root@simon ~]# ll -d /lib64lrwxrwxrwx. 1 root root 9 7月 14 03:33 /lib64 -> usr/lib64[root@simon ~]# test -L /lib64;echo $?0 -
-u 文件有 suid 位设置
[root@simon ~]# ll /usr/bin/passwd -rwsr-xr-x. 1 root root 27832 6月 10 2014 /usr/bin/passwd[root@simon ~]# [ -u `which passwd` ];echo $?0[root@simon ~]# [ -u `which ls` ];echo $?1
2.2 字符串测试
-
== 两个字符串相等
[root@simon ~]# [ "aaa" == "aaa" ];echo $?0 -
!= 两个字符串不相等
[root@simon ~]# [ "aaa" != "aaa" ];echo $?1 -
-z 字符串长度为零
[root@simon ~]# ip=""[root@simon ~]# [ -z "$ip" ];echo $? # 注意引号0[root@simon ~]# ip='1.1.1.1'[root@simon ~]# [ -z "$ip" ];echo $?1 -
-n 字符串长度不为零
[root@simon ~]# [ -n "$ip" ];echo $? # 注意加引号1
2.3 测试数值
test与[]也可以测试数值,与 [[]]和(())效果一样,下一小节会详细介绍[[]]和(())
-
-eq 等于
-
-ne 不等于
-
-gt 大于
-
-lt 小于
-
-ge 大于等于
-
-le 小于等于
-
-a并且
-
-o或者
[root@simon ~]# [ 10 -eq 10 ];echo $?0[root@simon ~]# [ 10 -eq 10 -a 10 > 3 ];echo $?0# 1、示例:[root@simon ~]# disk_use=$(df -P |grep '/$' |awk '{print $5}' |awk -F% '{print $1}')[root@simon ~]# [ $disk_use -gt 10 ] && echo "warn......"warn......[root@simon ~]# [ $(id -u) -eq 0 ] && echo "当前是超级用户" || echo "you不是超级用户"当前是超级用户
三 关系运算符
<
>
<=
>=
==
!=
&&
||
上述关系运算符需要配合(())使用(注意(())属于C语言风格的比较),最终都是用来当if判断或者while循环的条件
[root@simon ~]# x=100[root@simon ~]# (($x>10))[root@simon ~]# echo $?0[root@simon ~]# (($x < 10));echo $?1[root@simon ~]# (($x == 100));echo $?0[root@simon ~]# (($x != 100));echo $?1[root@simon ~]# (($x != 100)) && (("simon" == "simon"))[root@simon ~]# echo $?1[root@simon ~]# (($x != 100)) || (("simon" == "simon"))[root@simon ~]# echo $?0[root@simon ~]# (($x != 100 && "simon" == "simon"));echo $?1[root@simon ~]# (($x != 100 || "simon" == "simon"));echo $?0
四 赋值运算符
-
=
[root@simon ~]# x=10[root@simon ~]# echo $x10 -
+=
[root@simon ~]# x=10[root@simon ~]# ((x%3))[root@simon ~]# echo $x10[root@simon ~]# ((x%=3))[root@simon ~]# echo $x1 -
*=
同上 -
/=
同上 -
%=
同上
五 补充
5.1 补充[[]]
[[]]与[]基本一样,不同的是[[]]支持正则匹配,不过要注意的是必须在内层中括号内左右两侧加空格
[root@simon ~]# [[ "$USER" == "root" ]];echo $? # 注意内层[]中包含的内容必须左右两侧加空格0[root@simon ~]# [[ "$USER" == "root" ]];echo $? # 一个等号也行两个等号也可以额0# 此外[[]]内部是可以使用正则的,注意:正则表达式不要加引号[root@simon ~]# [[ "$USER" =~ "^root$" ]];echo $? # 正则表达式不要加引号1[root@simon ~]# [[ "$USER" =~ ^root$ ]];echo $? # 正则表达式不要加引号0[root@simon ~]# [[ ! "$USER" =~ t$ ]] && echo 此时不是管理员登录 || echo 是管理员登录是管理员登录[root@simon ~]# num1=123[root@simon ~]# [[ "$num1" =~ ^[0-9]+$ ]];echo $? # 判断是否是数字0[root@simon ~]# [[ "$num1" =~ ^[0-9]+$ ]] && echo "是数字"是数字[root@simon ~]# num2=abc123de[root@simon ~]# [[ "$num2" =~ ^[0-9]+$ ]];echo $?1[root@simon ~]# [[ "$num2" =~ ^[0-9]+$ ]] || echo "num2不是数字"num2不是数字
企业面试题1: 传入两个数值信息, 自动让脚本显示数值比较情况
[root@simon shell]# cat num.sh #!/bin/bash[ $# -ne 2 ] && echo "只能传入两个整数" && exit[[ ! $1 =~ ^[0-9]+$ ]] && echo "参数1必须是整数" && exit[[ ! $2 =~ ^[0-9]+$ ]] && echo "参数2必须是整数" && exit[[ $1 -lt $2 ]] && echo "$1 < $2"[[ $1 -gt $2 ]] && echo "$1 > $2"[[ $1 -eq $2 ]] && echo "$1 = $2"[root@simon shell]# chmod +x num.sh [root@simon shell]# ./num.sh 17 3717 < 37
5.2 补充浮点数的比较
# 需要注意的是:bc的结果为1代表真,为0代表假[root@simon ~]# echo "10.3 >= 10.1" | bc1[root@simon ~]# echo "10.3 != 10.1" | bc1[root@simon ~]# echo "10.3 != 10.3" | bc0# 应用[root@simon ~]# x=10.3[root@simon ~]# y=10.1[root@simon ~]# [ `echo "$x >= $y" | bc` -eq 1 ] && echo "$x 大于 $y"10.3 大于 10.1
六 总结
条件测试:
格式1: test 条件表达式格式2: [ 条件表达式 ]格式3: (()) 数值比较,运算 C语言格式4: [[ 条件表达式 ]],支持正则 =~
结合$符号
$[] # 整数运算$(()) # 整数运算$() # 命令替换
其他
() # 子shell中执行
七 其他元字符
-
1、`` 与$():取命令的结果
[root@localhost ~]# echo `pwd`/root[root@localhost ~]# echo $(pwd)/root不一样的地方在于$()可以嵌套,而``不能嵌套[root@localhost ~]# echo $(ls $(pwd))# 练习[root@aliyun test]# touch $(date +%F)_bak.tar.gz[root@aliyun test]# ls2020-08-24_bak.tar.gz -
2、~家目录
-
3、.与..
-
4、!调用历史命令、取反
# 1、调用历史命令[root@simon ~]# !1066[root@simon ~]# !ls # 匹配最近的一次历史命令# 2、取反1:对命令的结果取反[root@simon ~]# ! pwd/root[root@simon ~]# echo $?1# 3、取反2:效果与^雷同[root@localhost ~]# touch /test/{1.txt,2.txt,a.txt,aaa_bbb.txt}[root@localhost ~]# find /test ! -name 1.txt/test/test/2.txt/test/a.txt/test/aaa_bbb.txt[root@localhost ~]# ls /test/[!0-9].txt # .txt前只有一个字符,但是非数字/test/a.txt[root@localhost ~]# ls /test/[^0-9].txt # .txt前只有一个字符,但是非数字/test/a.txt[root@aliyun test]# ls -a /etc/skel/.[!.]* -
5、@无特殊意义
-
6、#注释
-
4、$取变量值
[root@localhost ~]# x=1[root@localhost ~]# echo $x1 -
5、%、-、+运算符,注意%可以与jobs配合“kill %工作号”杀后台进程。-减号还有区间及cd -回到上一级的意思
# 数学运算# 1、bc是比较常用的linux计算工具了,而且支持浮点运算:[root@localhost ~]# res=`echo 1+1 | bc`[root@localhost ~]# echo $res2[root@localhost ~]# res=`echo 10 % 3 | bc`[root@localhost ~]# echo $res1[root@localhost ~]# res=`echo 1.2+1.3|bc`[root@localhost ~]# echo $res2.5[root@localhost ~]# res=`echo 5.0+3.0|bc`[root@localhost ~]# echo $res8.0[root@localhost ~]# res=`echo "scale=2;5.0/3.0"|bc`[root@localhost ~]# echo $res1.66[root@localhost ~]# res=`echo "scale=2;5.0/6.0"|bc`[root@localhost ~]# echo $res.83# 2、expr不支持浮点数计算。而且要注意数字与运算符中的空格[root@localhost ~]# res=`expr 5 / 3` # 不支持浮点计算[root@localhost ~]# echo $res1[root@localhost ~]# res=`expr 1+1` # 注意:要有空格[root@localhost ~]# echo $res1+1[root@localhost ~]# res=`expr 1 + 1`[root@localhost ~]# echo $res2[root@localhost ~]# res=`expr 5 \* 3` # 在expr中*号要转义才能用,否则报语法错误[root@simon ~]# echo $res15# 3、$(()) 同expr,不支持浮点数运算[root@localhost ~]# echo $((1+1))2[root@localhost ~]# echo $((1.0+2.0))-bash: 1.0+2.0: 语法错误: 无效的算术运算符 (错误符号是 ".0+2.0") # 4、$[]同expr以及$(()),不支持浮点运算[root@localhost ~]# x=1[root@localhost ~]# echo $[$x+1]2# 5、let 不支持浮点数运算,而且不支持直接输出,只能赋值[root@localhost ~]# let res=1+1[root@localhost ~]# echo $res2[root@localhost ~]# [root@localhost ~]# let res=50/5[root@localhost ~]# echo $res10[root@localhost ~]# let c=1.3*3-bash: let: c=1.3*3: 语法错误: 无效的算术运算符 (错误符号是 ".3*3" [root@aliyun test]# x=1[root@aliyun test]# let x+=10[root@aliyun test]# echo $x11 -
6、^同!一样
-
7、&后台运行
[root@localhost home]# echo "hello";sleep 3;echo "world" & -
8、*任意多个字符
[root@localhost ~]# touch 1.txt 2.txt aa.txt aaa.txt[root@localhost ~]# rm -rf *.txt[root@localhost ~]# touch 1.txt 2.txt aa.txt aaa.txt a1c.txt[root@localhost ~]# ls *.txt1.txt 2.txt a1c.txt aaa.txt aa.txt -
9、()在子shell中执行
[root@localhost ~]# (x=1)[root@localhost ~]# echo $x应用[root@localhost ~]# (umask 066;touch a.txt) # umask的设置只在子shell中有效[root@localhost ~]# ll a.txt -rw-------. 1 root root 0 8月 13 15:22 a.txt[root@localhost ~]# touch b.txt[root@localhost ~]# ll b.txt -rw-r--r--. 1 root root 0 8月 13 15:23 b.txt -
10、_下划线:无特殊意义,可以用于名字的声明
[root@localhost ~]# tar -czvf `date +%F_%H:%M:%S`_bak.tar.gz /etc/ -
11、=赋值,==判断相等性
[root@localhost ~]# [ 1 == 1 ] # 条件1 == 1的左右两边必须有空格[root@localhost ~]# echo $? # 判断上一条命令的结果是否为真,0=》true0 -
12、|管道:把一个进程的处理结果传递给另外一个进程
[root@localhost ~]# ps aux | grep python|管道命令的作用,是将左侧命令的标准输出转换为标准输入,提供给右侧命令作为参数。但是,大多数命令都不接受标准输入作为参数,只能直接在命令行输入参数,这导致无法用管道命令传递参数。比如echo命令就不接受管道传参。$ echo "hello world" | echoxargs命令的作用,是将标准输入转为命令行参数,例如$ echo "hello world" | xargs echohello world[root@localhost ~]# find /home/ -type d -name "test*" |xargs ls1.txt 2.txt 3.txt[root@localhost ~]# ls /home/test1.txt 2.txt 3.txt -
13、\转义特殊字符
[root@localhost ~]# mkdir a\ b.txt # 虽然可以,但不推荐[root@localhost ~]# ll总用量 0drwxr-xr-x. 2 root root 6 8月 13 15:35 a b.txt [root@localhost ~]# echo $RMB # 默认会当成变量[root@localhost ~]# echo '$RMB' # 取消特殊意义$RMB[root@localhost ~]# echo \$RMB # 取消特殊意义$RMB -
14、[]条件测试,后续会详细介绍
[root@localhost ~]# name="simon"[root@localhost ~]# [ $name == "simon" ];echo $?0[root@localhost ~]# name="adf"[root@localhost ~]# [ $name == "simon" ];echo $?1[root@localhost ~]# [ -d /test ];echo $?0[root@localhost ~]# -
15、引号
'' 强引用(在单引号中都视为普通字符)" " 弱引用 (在双引号中保留变量)[root@localhost ~]# x=111[root@localhost ~]# echo "$x"111[root@localhost ~]# echo '$x'$x[roo -
16、;与&&与||连接多条命令
[root@localhost home]# gagaga;ls # 不论前一条命令运行成功与否,都会执行后续命令bash: gagaga: 未找到命令...simon[root@localhost home]# gagaga && ls # 只有前一条命令执行成功,才会执行后续命令bash: gagaga: 未找到命令... [root@localhost home]# ls /test || mkdir /test # 前一条命令执行不成功才会执行后续命令0.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt -
17、:空命令,真值
[root@simon ~]# :[root@simon ~]# echo $?0 -
18、/路径分隔符
-
19、{}循环列表
[root@localhost home]# touch /test/{0..9}.txt[root@localhost home]# ls /test/0.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt[root@localhost ~]# touch {1..3}{a..d}.txt[root@localhost ~]# ls1a.txt 1b.txt 1c.txt 1d.txt 2a.txt 2b.txt 2c.txt 2d.txt 3a.txt 3b.txt 3c.txt 3d.txt[root@simon ~]# x=100[root@simon ~]# echo ${x}% # 控制变量名的范围100%[root@simon ~]# [root@simon ~]# echo $xrmb[root@simon ~]# echo ${x}rmb100rmb -
20、重定向
> >> 输出重定向< << 输入重定向> 覆盖 >> 追加[root@localhost home]# cat >> a.txt << EOF> 111> 222> 333> EOF0标准输入、1标准正确输出、2标准错误输出,&标准正确和错误输出[root@localhost home]# pwd 1>a.txt[root@localhost home]# cat a.txt /home[root@localhost home]# gagag 2>a.txt[root@localhost home]# cat a.txt bash: gagag: 未找到命令...[root@localhost home]# gagaga &>/dev/null< << 输入重定向[root@localhost ~]# mysql -uroot -p123 < bbs.sql[root@localhost home]# grep root < /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin [root@localhost home]# dd if=/dev/zero of=/a.txt bs=1M count=10记录了10+0 的读入记录了10+0 的写出10485760字节(10 MB)已复制,0.024387 秒,430 MB/秒[root@localhost home]# dd </dev/zero >/b.txt bs=1M count=10记录了10+0 的读入记录了10+0 的写出10485760字节(10 MB)已复制,0.0202365 秒,518 MB/秒[root@localhost home]# ll /a.txt -rw-r--r--. 1 root root 10485760 8月 13 16:02 /a.txt[root@localhost home]# ll /b.txt -rw-r--r--. 1 root root 10485760 8月 13 16:03 /b.txt -
21、?任意一个字符
[root@localhost ~]# ls ??.txtaa.txt[root@localhost ~]# ls a?c.txta1c.txt[root@localhost ~]# rm -rf *.txt -
22、范围中的任意一个字符 [12] [ac] [a-z] [0-9]
[root@localhost ~]# touch a1c a2c axc aXc axd[root@localhost ~]# ls a?ca1c a2c axc aXc[root@localhost ~]# ls a[1x]ca1c axc[root@localhost ~]# ls a[a-z]caxc aXc[root@localhost ~]# ls a[A-Z]c # 不区分大小写axc aXc[root@localhost ~]# ls a[x]caxc[root@localhost ~]# ls a[X]caXc[root@localhost ~]# ls a[0-9]ca1c a2c[root@localhost ~]# ls /dev/sd[a-z]*/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb1

浙公网安备 33010602011771号