文章目录
- 1. 特殊变量补充
- 2. 变量扩展-变量子串
-
- 2.1 获取变量字符的长度
- 2.2 给变量设置默认值
- 3. 命令
-
- 3.1 dirname
- 3.2 basename
- 3.3 cut
- 4. 条件测试命令:[]
-
- 4.1 逻辑运算符
- 4.2 文件测试
- 4.3 案例:书写脚本-检查文件类型
- 4.4 逻辑运算
- 4.5 案例:书写脚本-检查服务是否正在运行或是开机自启动
- 4.6 增强版条件测试命令:[[]]
- 4.7 面试题:[]与[[]]的区别?
- 5. 字符串比较
-
- 5.1 案例:书写脚本-检查服务是否正在运行和开机自启动
- 6. 多分支:if、case
-
- 6.1 案例-统计根分区磁盘使用率,60-70输出警告,70-80输出需要处理,80-95输出需要及时处理,965以上输出立刻处理
- 6.2 案例:判断系统类型
- 7. 踩坑记录
-
- 8. 思维导图
1. 特殊变量补充
- $$:获取当前脚本的pid
- $_:获取上一个命令的最后一个参数;在命令行中可以使用Esc+.快捷键
2. 变量扩展-变量子串
2.1 获取变量字符的长度
[root@aliyun-ubuntu ~]
[root@aliyun-ubuntu ~]
skx
[root@aliyun-ubuntu ~]
3
2.2 给变量设置默认值
格式 |
说明 |
${para:-word} |
变量para没定义或为空时,word作为默认值,不修改原内容 |
${para:=word} |
变量para没定义或为空时,word作为默认值,修改原内容 |
[root@aliyun-ubuntu ~]
[root@aliyun-ubuntu ~]
root
[root@aliyun-ubuntu ~]
[root@aliyun-ubuntu ~]
[root@aliyun-ubuntu ~]
skx
[root@aliyun-ubuntu ~]
skx
3. 命令
3.1 dirname
[root@aliyun-ubuntu ~]
/etc
[root@aliyun-ubuntu ~]
/etc/ssh
3.2 basename
[root@aliyun-ubuntu ~]
passwd
[root@aliyun-ubuntu ~]
lxd
3.3 cut
[root@aliyun-ubuntu ~]
[root@aliyun-ubuntu ~]
5
[root@aliyun-ubuntu ~]
5678
4. 条件测试命令:[]
[ ]或test,用于检查文件属性、字符串比较、数值比较等逻辑判断。它是 Shell 脚本中常见的条件判断语法之一。
4.1 逻辑运算符
- 逻辑运算符不能在[]内使用
- &&:并且,前面命令执行成功后才执行后面的命令
- ||:或者,前一个命令执行失败后才执行后面的命令
4.2 文件测试
选项 |
说明 |
-f |
文件是否为普通文件 |
-d |
文件是否为目录 |
-x |
文件是否有执行权限 |
-s |
文件大小大于0 |
-h/-L |
文件是否为软链接 |
[root@aliyun-ubuntu ~]
file
[root@aliyun-ubuntu ~]
dir
[root@aliyun-ubuntu ~]
not dir
4.3 案例:书写脚本-检查文件类型
[root@aliyun-ubuntu /server/scripts]
export LANG=en.US_UTF-8
file=$1
if [ $# -eq 0 ];then
echo "Usage:$0 file/dir"
exit 1
fi
if [ -h $file ];then
echo "$file is symbolic"
exit 0
fi
if [ -f $file ];then
if [ -x $file ];then
mode="has permisson"
else
mode="has not permission"
fi
if [ -s $file ];then
size="size not is 0"
else
size="size is 0"
fi
echo "${file} is file;permission:${mode};size:${size}"
exit 0
fi
if [ -d $file ];then
echo "$file is directory"
exit 0
fi
echo "$file is other type file"
[root@aliyun-ubuntu /server/scripts]
Usage:check_type.sh file/dir
[root@aliyun-ubuntu /server/scripts]
/sbin is symbolic
[root@aliyun-ubuntu /server/scripts]
./check_type.sh is file;permission:has not permission;size:size not is 0
[root@aliyun-ubuntu /server/scripts]
/usr/ is directory
[root@aliyun-ubuntu /server/scripts]
./test is other type file
4.4 逻辑运算
逻辑运算符 |
说明 |
-a |
并且 |
-o |
或者 |
! |
取反 |
4.5 案例:书写脚本-检查服务是否正在运行或是开机自启动
[root@aliyun-ubuntu /server/scripts]
name=$1
if [ $# -eq 0 ];then
echo "Usage:$0 server name"
exit 1
fi
running=`systemctl is-active $name`
enable=`systemctl is-enabled $name`
if [ ${running}="active" -a ${enable}="enabled" ];then
echo "$name is running and enabled"
else
echo "$name is not running or enabled"
fi
[root@aliyun-ubuntu /server/scripts]
Usage:check_service2.sh server name
[root@aliyun-ubuntu /server/scripts]
sshd is running and enabled
4.6 增强版条件测试命令:[[]]
[[ ]]
,比 []
更强大,支持 &&
、||
、正则匹配等
[root@aliyun-ubuntu /server/scripts]
[root@aliyun-ubuntu /server/scripts]
1
[root@aliyun-ubuntu /server/scripts]
[root@aliyun-ubuntu /server/scripts]
1
[root@aliyun-ubuntu /server/scripts]
2
4.7 面试题:[]与[[]]的区别?
基本条件测试命令:[] |
扩展条件测试命令:[[]] |
无法使用正则 |
可以使用正则 |
只能使用选项比价大小:-eq、-lt、-gt…… |
可以使用选项,也能用字符:==、>=、<=…… |
逻辑符号只能用-a、-o、! |
逻辑符号能用选项,也能用&&、|| |
5. 字符串比较
符号 |
说明 |
= |
“字符串” = “字符串”,注意加双引号和空格 |
!= |
判断不相等 |
-z |
zero,判断变量是否为空格 |
-n |
not zero,判断变量是否不为空 |
[root@aliyun-ubuntu /server/scripts]
1
[root@aliyun-ubuntu /server/scripts]
[root@aliyun-ubuntu /server/scripts]
1
5.1 案例:书写脚本-检查服务是否正在运行和开机自启动
[root@aliyun-ubuntu /server/scripts]
export LANG=en.US_UTF-8
service=$1
if [ $# -eq 0 ];then
echo "Usage:$0 service name"
exit 1
fi
status_run=`systemctl is-active ${service}`
if [ ${status_run}="active" ];then
echo "${service} is running"
else
echo "${service} is not running"
fi
status_enabled=`systemctl is-enabled ${service}`
if [ ${status_enabled}="enabled" ];then
echo "${service} is enabled"
else
echo "${service} is not enabled"
fi
if [ ${status_run}="active" -a ${status_enabled}="enabled" ];then
echo "$service is active and enabled"
fi
[root@aliyun-ubuntu /server/scripts]
sshd is running
sshd is enabled
sshd is active and enabled
6. 多分支:if、case
6.1 案例-统计根分区磁盘使用率,60-70输出警告,70-80输出需要处理,80-95输出需要及时处理,965以上输出立刻处理
[root@aliyun-ubuntu /server/scripts]
export LANG=en.US_UTF-8
usage=`df -h / |awk -F '[ %]' 'NR==2{print $(NF-2)}'`
if [ $usage -gt 60 -a $usage -le 70 ];then
echo "Warning: Insufficient disk space ${usage}"
elif [ $usage -gt 70 -a $usage -le 80 ];then
echo "Warning: Insufficient disk space ${usage}"
elif [ $usage -gt 80 -a $usage -le 95 ];then
echo "Warning: Severe shortage of disk space ${usage}"
elif [ $usage -gt 95 ];then
echo "Warning: Disk space is about to run out ${usage}"
else
echo "Disk space is normal"
fi
[root@aliyun-ubuntu /server/scripts]
Disk space is normal
6.2 案例:判断系统类型
- bash运行脚本是一个子shell程序,里面的变量仅在脚本中生效
- source运行的脚本是在当前Shell环境,可以用来加载指定的变量
[root@aliyun-ubuntu /server/scripts]
export LANG=en.US_UTF-8
source /etc/os-release
case "$ID" in
kylin|rocky|centos) echo "$ID yum insatll package"
;;
ubuntu|debian) echo "$ID apt install package"
;;
*)
echo "other system"
esac
[root@aliyun-ubuntu /server/scripts]
ubuntu apt install package
7. 踩坑记录
1. 条件测试时,变量要加上双引号
变量建议加引号:
if [ "$var" = "hello" ]; then
if [ $var = "hello" ]; then
8. 思维导图
【金山文档】 思维导图 https://www.kdocs.cn/l/co3I7PtpTYQX