运维Shell脚本小试牛刀(一)
运维Shell脚本小试牛刀(二)
运维Shell脚本小试牛刀(三)::$(cd $(dirname $0); pwd)命令详解
运维Shell脚本小试牛刀(四): 多层嵌套if...elif...elif....else fi_蜗牛杨哥的博客-CSDN博客
Cenos7安装小火车程序动画
运维Shell脚本小试牛刀(五):until循环
运维Shell脚本小试牛刀(六): Shell中的函数认知
运维Shell脚本小试牛刀(七):从函数文件中调用另外一个脚本文件中函数
一: case语句使用命令行参数
如通常根据命令行参数不同执行某个服务执行不同的分叉操作:备份数据库,web服务配置文件和一些其他文件到NAS存储设备等操作;
[root@www example10]# cat allInOutBackup.sh
#!/bin/bash -
#==================================================================================================================
#
#
# FILE: allInOneBackup.sh
# USAGE: ./allInOneBackup.sh
# DESCRIPTION:
# OPTIONS: -------
# REQUIREMENTS: ---------
#
# BUGS: ------
# NOTES: --------
# AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ORGANIZATION:
# CREATED: 2023-8-24 09:11:20
# REVISION: --------
#
#
#
#
#
#====================================================================================================================case $1 in
sql)
echo "Running mysql backup using mysqldump tools............................"
# Running the backup command or script....
;;
sync)
echo "Running backup using rsync tools......................................"
# Running backup using rsync tools.
;;
snap)
echo "Running snapshot backup on storage..................................."
# Running snapshot backup command.
;;*)
echo "Backup utility"
echo "Usage: `basename $0` (sql|sync|snap)"
echo " sql : Run MYSQL backup utility."
echo " sync : Run web server backup utility."
echo " snap : Run snapshot backup utility..."
;;esac
二: 测试分叉命令
注意: Linux下文件名时区分大小写的。同样case语句中的每个模式匹配也存在大小敏感的问题;当然如果我们想让脚本中的case语句对命令行参数的大小写不敏感,我们还是有办法解决的。一个比较简单的方法时使用如下命令开启 nocasematch选项(开启此选项后,当执行case或者"[[" 条件命令时,Shell以大小写不敏感的方式匹配模式):
shopt -s nocasematch
[root@www example10]# ./allInOutBackup.sh
Backup utility
Usage: allInOutBackup.sh (sql|sync|snap)
sql : Run MYSQL backup utility.
sync : Run web server backup utility.
snap : Run snapshot backup utility...
[root@www example10]# ./allInOutBackup.sh mysql
Backup utility
Usage: allInOutBackup.sh (sql|sync|snap)
sql : Run MYSQL backup utility.
sync : Run web server backup utility.
snap : Run snapshot backup utility...
[root@www example10]# ./allInOutBackup.sh sql
Running mysql backup using mysqldump tools............................
[root@www example10]# ./allInOutBackup.sh sync
Running backup using rsync tools......................................
[root@www example10]# ./allInOutBackup.sh snap
Running snapshot backup on storage...................................
[root@www example10]# ./allInOutBackup.sh SQL
Backup utility
Usage: allInOutBackup.sh (sql|sync|snap)
sql : Run MYSQL backup utility.
sync : Run web server backup utility.
snap : Run snapshot backup utility...
三: case使用命令行参数时忽略参数大小写
[root@www example10]# cat allInOutBackupperlower.sh
#!/bin/bash -
#==================================================================================================================
#
#
# FILE: allInOneBackup.sh
# USAGE: ./allInOneBackup.sh
# DESCRIPTION:
# OPTIONS: -------
# REQUIREMENTS: ---------
#
# BUGS: ------
# NOTES: --------
# AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ORGANIZATION:
# CREATED: 2023-8-24 09:11:20
# REVISION: --------
#
#
#
#
#
#====================================================================================================================# 忽略命令行参数的大小写
shopt -s nocasematch
case $1 insql)
echo "Running mysql backup using mysqldump tools............................"
# Running the backup command or script....
;;
sync)
echo "Running backup using rsync tools......................................"
# Running backup using rsync tools.
;;
snap)
echo "Running snapshot backup on storage..................................."
# Running snapshot backup command.
;;*)
echo "Backup utility"
echo "Usage: `basename $0` (sql|sync|snap)"
echo " sql : Run MYSQL backup utility."
echo " sync : Run web server backup utility."
echo " snap : Run snapshot backup utility..."
;;esac
四: 测试输出
[root@www example10]# ./allInOutBackupperlower.sh SQL
Running mysql backup using mysqldump tools............................