文章目录
- 二、变量与引用
- 1、变量命名
- 2、变量类型
- 3、变量定义
- 4、常用变量
- 4.1 内置通配符
- 4.2 内置变量
- 4.3 自定义变量
- 4.4 环境变量
- 4.5 变量的作用域
-
- 4.6 位置变量
- 4.5.1 位置变量的左移操作
- 4.5.2 执行脚本父目录绝对路径的获取方式
- 4.7 预定义变量
- 4.8 脚本变量案例
- 4.9 \$*与\$@的区别
- 5、变量赋值与替换
- 5.1 变量和引号
- 5.2 只读变量
- 5.3 变量运算
- 5.3.1 常见运算符
- 5.3.2 常见运算命令
- 5.3.3 变量子串说明
- 5.3.4 操作案例
- 6、输入与输出
- 6.1 read输入
-
- 6.2 echo输出
- 6.2.1 命令格式
- 6.2.2 操作案例
- 6.2.3 结果输出到指定文件
- 6.2.4 引号的输出
- 6.2.7 彩色输出
- 6.2.8 在脚本中使用颜色输出
- 6.2.8.1 定义颜色变量
- 6.2.8.2 定义颜色动作
- 6.2.9 使用tput修改颜色
- 7、$( ) $(( )) $[ ] $[[ ]] ${ }的区别
- 7.1 $( ) 与\` \`命令的替换
- 7.2 ${ }变量替换
- 7.2.1 变量替换
- 7.2.2 变量截取
- 7.2.3 匹配删除
- 7.3 $ [ ] 与$(( )) 整数运算
二、变量与引用
变量是程序设计语言中的一个可以变化的量,变化的是变量的值。变量几乎所有的程序设计语言都有定义,并且其涵义也基本一致。在程序执行的过程中,保存数据的内存空间的内容可能会不断地发生变化,但是代表内存地址的变量名却保持不变。
1、变量命名
在shell中对于变量名称的规范如下:
- 变量名可以由字母、数字、下划线组成,只能以字母与下划线开头;
- 变量名的长度没有直接的规定,尽可能使用较短的字符串作为变量名。
2、变量类型
shell是一种动态类型语言和弱类型语言,在shell中,变量的数据类型不需要声明,变量的数据类型会根据不同的操作有所变化,shell中的变量是不分数据类型的,统一按照字符串存储。
弱类型语言与强类型语言(php
、C
、Python
都是强类型语言)
- 强类型和弱类型主要是站在变量类型处理的角度进行分类的;
- 强类型是指不允许隐式变量类型转换,弱类型则允许隐式类型转换;
3、变量定义
在shell中,当用户第一次使用某个变量名时,就同时定义了该变量名称,在变量的作用域内都可以使用此变量。
[root@localhost shell]
bash: 3: command not found...
[root@localhost shell]
bash: a: command not found...
[root@localhost shell]
[root@localhost shell]
0
[root@localhost shell]
3
[root@localhost ~]
[root@localhost ~]
4、常用变量
主要有:通配符、内置变量、自定义变量、环境变量、全局变量、局部变量、位置变量、预定义变量。
4.1 内置通配符
在Linux系统中,通配符是一种特殊语句,主要有星号*
和问号?
,用来模糊搜索文件。当查找文件夹时,可以使用它来代替一个或多个真正字符;当不知道真正字符或者懒得输入完整名字时,常常使用通配符代替一个或多个真正的字符。
?
代表单个任意字符;*
代表多个任意字符。[]
匹配括号内任意一个字符;[-]
代表某个范围内的单个字符;[*]
匹配不是中括号的字符;
案例演示:
[root@localhost shell]
[root@localhost shell]
a b c tt.cc
[root@localhost shell]
tt.cc
[root@localhost shell]
tt.cc
[root@localhost shell]
a b
[root@localhost shell]
a b
[root@localhost shell]
c tt.cc
4.2 内置变量
内置变量:系统本来就存在的变量,可直接使用的。在执行脚本的时候,可通过带参数传入脚本内部。
常见内置变量有:
-
USER
:当前系统登录用户;
-
UID
: 当前用户的UID;
-
HOME
: 当前用户的家目录;
-
SHELL
: 当前使用的shell类型 /bin/bash;
-
BASHPID
: 当前bash的进程编号;
-
HISTSIZE
: 缓冲区记录命令历史的数量;
-
MAIL
: 当前用户的邮件信息储存目录 /var/spool/mail/root
;
-
PATH
: 当前系统环境变量;
-
LANG
: 系统使用的语言和编码字符集 en_US.UTF-8
;
-
RANDOM
: 生成随机数,生成范围0~32767
;
-
PS1
: 命令提示符;
-
OSTYPE
: 操作系统类型 linux-gun;
-
HOSTTYPE
: 主机类型 x86_64;
-
MACHTYPE
: 机器类型 x86_64-redhat-linux-gnu;
[root@localhost ~]
root
[root@localhost shell]
0
[root@localhost shell]
/root
[root@localhost shell]
/bin/bash
[root@localhost shell]
2821
[root@localhost shell]
1000
[root@localhost shell]
/var/spool/mail/root
[root@localhost shell]
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost shell]
3412
[root@localhost shell]
[\u@\h \W]\$
[root@localhost shell]
linux-gnu
[root@localhost shell]
x86_64
[root@localhost shell]
x86_64-redhat-linux-gnu
4.3 自定义变量
根据自己喜好所定义的变量。
4.4 环境变量
环境变量是程序(操作系统命令和应用程序)的执行都需要运行环境,分为临时变量和永久变量。
[root@localhost shell]
[root@localhost shell]
export TEST_PATH=/etc
export PATH=$PATH:$TEST_PATH
[root@localhost ~]
[root@localhost shell]
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost shell]
[root@localhost shell]
/home/backup
4.5 变量的作用域
4.5.1 全局变量
全局变量可以在脚本中定义,也可以在函数中定义,脚本中定义的变量都是全局变量,其作用域从被定义开始一直到shell脚本结束或者被显示删除。
4.5.2 局部变量
与全局变量相比,局部变量的使用范围较小,通常仅限于某个程序段访问,在shell语言中,可以在函数内部通过local
关键字定义局部变量,函数参数也是局部变量。
[root@localhost ~]
function test(){
local aaa=123
echo "本地变量:$aaa"
}
test
echo "---------------"
echo "全局变量:$aaa"
[root@localhost ~]
本地变量:123
---------------
全局变量:
4.6 位置变量
位置变量顾名思义就是脚本后面所附加的具体位置参数,如sh test.sh 1 2 3 ...
,其中1、2、3分别对应的就是位置1的变量,位置2的变量,位置3的变量,以此类推。
$1、$2、$3、$4、$5、$6、$7、$8、${9}
4.5.1 位置变量的左移操作
在某些脚本文件中,若定义了某个位置参数后,则该参数后续不会被使用,因此可以使用shift
命令左移位置变量,默认移动距离为1,使得变量的数字不用人为记忆就可以常用,写代码较为方便。
[root@localhost ~]
echo "位置1的变量是:$1"
echo "位置2的变量是:$2"
shift 1
echo "位置3的变量是:$3"
shift 3
echo "位置7的变量是:$7"
[root@localhost ~]
位置1的变量是:a
位置2的变量是:b
位置3的变量是:d
位置7的变量是:k
4.5.2 执行脚本父目录绝对路径的获取方式
使用dirname
可以直接获取脚本所处父目录的绝对路径
[root@localhost ~]
.
[root@localhost ~]
/usr/local
注:dirname
与pwd
的区别说明:
[root@localhost ~]
[root@localhost lib]
echo "dirname $0"
path=$(dirname $0)
echo "$path"
pwd
[root@localhost lib]
dirname /root/test.sh
/root
/usr/local/lib
4.7 预定义变量
Linux系统默认预先定义的位置变量
$0
$*
$@
$#
$$
$!
$?
4.8 脚本变量案例
位置变量
[root@localhost ~]
echo "脚本名称是:$0"
echo "第2个参数是:$2"
echo "所有参数是:$*"
echo "所有参数是:$@"
echo "参数个数是:$#"
echo "当前进程PID是:$$"
[root@localhost ~]
脚本名称是:test.sh
第2个参数是:b
所有参数是:a b c d e f g
所有参数是:a b c d e f g
参数个数是:7
当前进程PID是:4607
[root@localhost ~]
0
预定义变量
[root@localhost shell]
ping -c2 $1 &> /dev/null
if [ $? = 0 ];then
echo "host $1 is ok!"
else
echo "host $1 is down!"
fi
[root@localhost shell]
host 10.81.10.55 is ok!
[root@localhost shell]
0
4.9 $*与$@的区别
[root@localhost shell]
[root@localhost shell]
I am test command
[root@localhost shell]
I am
test
command
5、变量赋值与替换
5.1 变量和引号
shell语言中一共有三种引号,分别是单引号、双引号、反引号。
- 单引号:字符作为普通字符出现,强转义;
- 双引号:除了
$
、\
、'
、"
四个符号外,其余字符作为普通字符对待; - 反引号:引号内的内容被解释的shell命令;
[root@localhost shell]
user is root
[root@localhost shell]
user is $USER
[root@localhost shell]
user is root
[root@localhost shell]
user is `whoami`
[root@localhost ~]
aaa"bbb
5.2 只读变量
将变量配置成为readonly
类型,该变量不可被更改内容,也不能unset
(不建议使用此操作);
readonly [-fap] [变量定义]
演示案例:
[root@localhost ~]
[root@localhost ~]
declare -r aaa="123"
[root@localhost ~]
-bash: unset: aaa: cannot unset: readonly variable
只读变量删除方法参考链接
5.3 变量运算
5.3.1 常见运算符
+
-
:加 减*
/
%
:乘 除 取余**
:幂次方++
--
:增加 减少!
&&
||
:非 与 或<
<=
>
>=
:小于 小于等于 大于 大于等于==
!=
=
:相等 不等 相当于<<
>>
:左移 右移~
|
&
^
:取反 按位异或 按位与 按位或=
+=
-+
*=
/+
%=
:赋值运算符,a+=1
相当于a=a+1
[root@localhost ~]
2
[root@localhost ~]
64
[root@localhost ~]
3
[root@localhost ~]
0
[root@localhost ~]
1
5.3.2 常见运算命令
(())
:用于整数运算的常用运算符;let
:用于整数运算,类似于(())
;expr
:可用于整数运算;bc
:Linux下的一个计算器程序,适用于整数与小数运算;$[]
:用于整数运算;awk
:用于整数与小数运算;declare
:定义变量值和属性,-i 参数定义整形变量,做运算;
[root@localhost ~]
2
[root@localhost ~]
3
[root@localhost shell]
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2+2
4
1+1
2
^C
(interrupt) Exiting bc.
[root@localhost shell]
i=1
while [ $i -le 3 ]
do
echo "$i"
let i++
done
[root@localhost shell]
1
2
3
5.3.3 变量子串说明
${parameter}
:返回变量$parameter
的内容;${#parameter}
:返回变量$parameter
内容的长度(按字符计算,索引从1开始),适用特殊变量;${parameter:offset}
:在变量${parameter}
中,从位置offset
之后开始提取子串到结尾;${parameter:offset:length}
:在变量${parameter}
中,从位置offset
之后开始提取长度为length
的子串;${parameter#word}
:从变量${parameter}
开头开始删除最短匹配的word
子串;${parameter##word}
:从变量${parameter}
开头开始删除最长匹配的word
子串;${parameter%word}
:从变量${parameter}
结尾开始删除最短匹配的word
子串;${parameter%%word}
:从变量${parameter}
结尾开始删除最长匹配的word
子串;${parameter/pattern/string}
:使用string
代替第一个匹配的pattern;
${parameter//pattern/string}
:使用string
代替所有匹配的pattern
;
[root@localhost shell]
[root@localhost shell]
testfile.tar.gz
[root@localhost shell]
15
[root@localhost shell]
le.tar.gz
[root@localhost shell]
tar.gz
[root@localhost shell]
gz
[root@localhost shell]
testfile.tar
[root@localhost shell]
testfile
[root@localhost shell]
testfile.zip.gz
${parameter:-word}
: 如果parameter
的变量值为空或未赋值,则会返回word
字符串并替代变量的值用途:如果变量未定义,则返回备用的值,防止变量为空值或因未定义而导致异常。
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
hello
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
hello
[root@localhost ~]
[root@localhost ~]
hello
[root@localhost ~]
${parameter:=word}
:如果parameter
的变量值为空或未赋值,则设置这个变量值为word
,并返回其值。位置变量和特殊变量不适用。
[root@localhost ~]
[root@localhost ~]
world
[root@localhost ~]
world
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
hello
[root@localhost ~]
hello
${parameter:?word}
:如果parameter
变量值为空或未赋值,那么word
字符串将被作为标准错误输出,否则输出变量的值。
[root@localhost ~]
-bash: var1: world
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
hello
[root@localhost ~]
hello
${parameter:+word}
:如果parameter
变量值为空或未赋值,则什么都不做,否则word
字符串将替代变量的值。
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
world
[root@localhost ~]
hello
5.3.4 操作案例
基本要求:删除7天前的过期数据备份,规定path
则删除path
路径下的过期文件,若不规定path
,则删除/tmp
目录下过期文件
[root@localhost shell]
find ${path:-/tmp} -name "*.tar.gz" -type f -mtime +7 | xargs rm -f
[root@localhost shell]
6、输入与输出
在Linux中输入与输出的方式种类较多,此处介绍两种最常用的输入与输出方法,read
输入与echo
输出;
6.1 read输入
在Linux系统中,read
命令用于从标准输入读取数值,这个命令可以用来读取键盘的输入值,因此常用于shell编程中的输入语法;
6.1.1 命令格式
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
-a
:后跟一个变量,该变量会被认为是个数组,然后给其赋值,默认是以空格为分割符;-d
:后面跟一个标志符,其实只有其后的第一个字符有用,作为结束的标志;-p
:后面跟提示信息,即在输入前打印提示信息;-e
:在输入的时候可以使用命令补全功能;-n
:后跟一个数字,定义输入文本的长度,如-n2
意味限制输入字符为2
个字符;-r
:屏蔽 \
,若没有该选项,\
作为一个转义字符,否则 \
就是个正常的字符无需进行转义(\
);-s
:安静模式,在输入字符时不再屏幕上显示,例如login时输入密码无回显,等同于使用stty ‐echo
关闭回显功能,使用stty echo
打开回显功能;-t
:后面跟秒数,定义输入字符的等待时间,如-t 3
意味3秒内输入,常用于条件判断语句中的定时输入;-u
:后面跟fd
,从文件描述符中读入,该文件描述符可以是exec新开启的;
6.1.2 操作案例
- 使用
read
命令输入一个信息;
#!/bin/bash
echo "输入一个信息:"
read aaa
echo "你输入的信息是:$aaa"
输出结果:
[root@localhost ~]
输入一个信息:
aaa
你输入的信息是:aaa
- 使用
-p
参数提供一个提示信息;
#!/bin/bash
read -p "输入一个信息:" aaa
echo "你输入的信息是:$aaa"
输出结果:
[root@localhost ~]
输入一个信息:aaa
你输入的信息是:aaa
- 使用
-t
参数指定输入时间限制;
#!/bin/bash
if read -t 5 -p "输入一个信息:" aaa;then
echo "你输入的信息是 $aaa"
else
echo "输入超时!"
fi
输出结果:
[root@localhost ~]
输入一个信息:输入超时!
- 使用
-n
参数指定输入字符长度;
#!/bin/bash
read -n1 -p "Do you want to continue [Y/N]?" answer
case $answer in
Y | y)
echo "fine ,continue";;
N | n)
echo "ok,good bye";;
*)
echo "error choice";;
esac
输出结果:
[root@localhost ~]
Do you want to continue [Y/N]?yfine ,continue
- 使用
-s
参数指定输入信息不得回显在终端上(数据是显示的,read
命令将文本颜色设置为背景颜色从而导致无法查看),常用于密码输入等位置;
#!/bin/bash
read -s -p "请输入密码:" pass
echo -e "\n密码是 $pass"
输出结果:
[root@localhost ~]
请输入密码:
密码是 aaa
- 使用
read
命令读取文件中的每一行内容;
#!/bin/bash
count=1
cat test.txt | while read line
do
echo "Line $count:$line"
count=$[ $count + 1 ]
done
echo "finish"
输出结果:
[root@localhost ~]
Line 1:this is test
Line 2:this is test
finish
- 使用
-e
参数,输入时可以使用tab
键补全内容;
[root@localhost ~]
输入文件名:1.
1.py 1.sh
输入文件名:1.py
6.2 echo输出
Linux的echo
命令在shell编程中十分常见,echo
命令的功能是在终端上打印出一段文字;
6.2.1 命令格式
echo [--help][--version] 或 echo [Options][字符串]
使用-e
参数后,echo
可以支持如下转义序列:
\a
:发出警告声;\b
:删除前一个字符;\c
:不产生进一步输出,只输出 \c
之前的内容;\f
:换行,但下一行内容起始位置和上一行从 \f
处换行的位置对齐,效果等同于 \v
;\n
:换行,下一行内容和上一行内容对齐;\r
:换行,光标移至行首,输出 \r
后面的内容;\v
:换行,但下一行内容起始位置和上一行从 \v
处换行的位置对齐,效果等同于 \f
;\t
:插入tab
水平制表符;\
:反斜线,插入 \
反斜线字符,若转义的内容如 \t
,系统会将转义后的反斜杠与 t
进行结合,生成为 \t
(制表符),从而实现tab
水平制表功能,因此就需要将 \t
分别转义,即 \\ + \t
(第一个\
均为转义符号);\nnn
:插入nnn
(八进制)所代表的ASCII字符;\0NNN
:八进制值表示的字节NNN
(1到3个数字);\xHH
:十六进制值表示的字节NNN
(1到2个数字);
6.2.2 操作案例
- 使用
-n
参数
[root@localhost ~]
aaabbb
ccc
- 使用
-e
参数
[root@localhost ~]
this is test
[root@localhost ~]
aaa[root@localhost ~]
[root@localhost ~]
this is
test
[root@localhost ~]
this is
test
[root@localhost ~]
ests is t
[root@localhost ~]
this is t est
[root@localhost ~]
this \is test
[root@localhost ~]
this is \test
6.2.3 结果输出到指定文件
使用重定向>
与追加重定向>>
指定到对应文件的路径(绝对路径或相对路径);
echo "this is test" > /tmp/test.txt
6.2.4 引号的输出
引号的输出:若是需要输出双引号,需要将双引号包含在单引号内,或使用转义符号\
进行转义;
不加引号:字符串原样输出,变量会被替换,除了 \
和 空格会产生区别。
[root@localhost ~]
Hello "World"
[root@localhost ~]
Hello "World"
[root@localhost ~]
Hello 'World'
[root@localhost ~]
Hello 'World'
[root@localhost shell]
a b
[root@localhost shell]
\a \b
6.2.7 彩色输出
echo
命令支持修改字体类型,字体背景颜色与字体颜色,转义序列\033
可以用来改变字体属性,若要转义序列生效,必须使用echo -e
选项;
echo -e " \033[颜色1;颜色2m 要展示的文字 \033[0m "
颜色的转义代码:
[0m: 正常
[1m: 粗体
[4m: 字体加上下划线
[7m: 逆转前景和背景色
[8m: 不可见字符
[9m: 跨行字体
[30m: 灰色字体
[31m: 红色字体
[32m: 绿色字体
[33m: 棕色字体
[34m: 蓝色字体
[35m: 紫色字体
[36m: 浅蓝色字体
[37m: 浅灰字体
[38m: 黑色字体
[40m: 黑色背景
[41m: 红色背景
[42m: 绿色背景
[43m: 棕色背景
[44m: 蓝色背景
[45m: 紫色背景
[46m: 浅蓝色背景
[47m: 浅灰色背景
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
6.2.8 在脚本中使用颜色输出
脚本中使用颜色的方式有两种:定义颜色变量、定义颜色动作
6.2.8.1 定义颜色变量
使用echo -e
调用,此方式是将颜色定义为变量,使用命令调用对应变量即可完成颜色设置;
#!/bin/bash
RED='\E[1;31m'
GREEN='\E[1;32m'
YELOW='\E[1;33m'
BLUE='\E[1;34m'
PINK='\E[1;35m'
RES='\E[0m'
echo -e "${RED}Red color${RES}"
echo -e "${YELOW}Yelow color${RES}"
echo -e "${BLUE}Blue color${RES}"
echo -e "${GREEN}Green color${RES}"
echo -e "${PINK}Pink color${RES}"
6.2.8.2 定义颜色动作
直接调用使用,将echo
也定义在变量内,此方式不需要过多的输入echo
;
#!/bin/bash
SETCOLOR_SUCCESS="echo -en \\E[1;32m"
SETCOLOR_FAILURE="echo -en \\E[1;31m"
SETCOLOR_WARNING="echo -en \\E[1;33m"
SETCOLOR_NORMAL="echo -en \\E[0;39m"
$SETCOLOR_SUCCESS && echo SUCCESS
$SETCOLOR_FAILURE && echo FAILURE
$SETCOLOR_WARNING && echo WARNING
$SETCOLOR_NORMAL && echo NORMAL
6.2.9 使用tput修改颜色
前面所学习的颜色修改方法都需要转义序列来实现,转义序列比较难以记忆,因此就需要一个更简单的方法来完成颜色的修改;tput
命令会利用terminfo
数据库中的信息,来控制终端,如光标控制、更改文本属性、文本涂色、控制屏幕等操作;
tput setab
:用于设置背景色;tput setaf
:用于设置前景色;
数值与颜色的匹配方法:0
- 黑色、1
- 红色、2
- 绿色、 3
- 黄色、4
- 蓝色、5
- 洋红色、6
- 青色、7
- 白色
aaa=$(tput setaf 1)
bbb=$(tput setaf 2
ccc=$(tput setaf 3)
ddd=$(tput setaf 4)
eee=$(tput setaf 5)
fff=$(tput setaf 6)
ggg=$(tput setaf 7)
hhh=$(tput sgr 0)
echo -e "${aaa}test \n${bbb}test \n${ccc}test \n${ddd}test \n${eee}test \n${fff}test \n${ggg}test${hhh}"
7、$( ) $(( )) $[ ] $[[ ]] ${ }的区别
7.1 $( ) 与` `命令的替换
在shell编程中,$( )
与` `均可以表示命令的替换操作;将反引号或者括号内的命令进行执行,并将执行的结果返回到当前的命令中;
- ` `反引号容易与单引号 ’ ’ 混淆,因此推荐使用
$( )
进行命令替换操作;在多层次的复合替换命令中,反引号需要进行转义操作,而$( )
不需要转义操作; $( )
并不支持所有的类Unix系统,但是反引号支持全部的类Unix系统;
操作案例
[root@localhost ~]
date
[root@localhost ~]
Fri Apr 29 15:56:10 CST 2022
[root@localhost ~]
Fri Apr 29 15:56:15 CST 2022
[root@localhost ~]
Fri Apr 29 15:57:10 CST 2022
[root@localhost ~]
date
[root@localhost ~]
Fri Apr 29 15:57:32 CST 2022
7.2 ${ }变量替换
默认情况下,
v
a
r
与
var与
var与{var}是没有区别的,但是${ }的使用可以使得变量名称的范围更加精确;
[root@localhost shell]
[root@localhost shell]
linux
[root@localhost shell]
[root@localhost ~]
linuxB
7.2.1 变量替换
[root@localhost ~]
test
[root@localhost ~]
test
${var:-word}
:如果变量var
为空或已经被删除(unset
),则返回word
,不改变变量var
的值;若变量var
的值不为空,则不返回word
也不改变原值;
[root@localhost ~]
word
[root@localhost ~]
${var:=word}
:如果变量var
为空或已经被删除(unset
),则返回word
,并将变量var
的值设置为word
;若变量var
的值不为空,则不返回word
也不改变原值(该操作是一种提供默认值的常见方式);
[root@localhost ~]
word
[root@localhost ~]
word
${var:?word}
:如果变量var
为空或已经被删除(unset
)那么将word
送到标准错误输出,可以利用此特性来检测是否设置了变量的值;${var:+word}
:如果变量var
被定义,则返回word
,不改变变量var
的值;若变量var
为空,不返回word
也不改变原值;
[root@localhost ~]
word
[root@localhost ~]
test
7.2.2 变量截取
语法格式:
${变量名:起始位置:长度}
案例演示:
[root@localhost ~]
[root@localhost ~]
6789
[root@localhost ~]
456
[root@localhost ~]
345678
[root@localhost ~]
34567
7.2.3 匹配删除
语法格式:
#*chr
:表示删除从左向右第一个遇到的字符chr
及其左侧的字符;##*chr
:表示删除从左向右最后一个遇到的字符chr
及其左侧的字符;(贪婪模式)%chr*
:表示删除从右向左第一个遇到的字符chr
及其左侧的字符;%%chr*
:表示删除从右向左最后一个遇到的字符chr
及其左侧的字符;(贪婪模式)
参数说明:
*
:匹配所有字符;%
:最短尾匹配;%%
:最大尾匹配;%
:从右往左;\#
:从左往右;
[root@localhost ~]
[root@localhost ~]
d1/d2/d3/d4/my.test.txt
[root@localhost ~]
my.test.txt
[root@localhost ~]
test.txt
[root@localhost ~]
txt
[root@localhost ~]
/d1/d2/d3/d4
[root@localhost ~]
[root@localhost ~]
/d1/d2/d3/d4/my.test
[root@localhost ~]
/d1/d2/d3/d4/my
7.3 $ [ ] 与$(( )) 整数运算
$ [ ]
与 $(( ))
都是进行数学运算的,支持+
-
*
/
%
:分别为 “加、减、乘、除、取模”;需要注意的是,bash
只能做整数运算,对于浮点数运算是当作字符串进行处理的;
[root@localhost ~]
[root@localhost ~]
14
[root@localhost ~]
8
[root@localhost ~]
1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)