case语句开发
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ndL4imnZ-1678232504593)(http://book.luffycity.com/linux-book/%E8%B6%85%E5%93%A5%E5%B8%A6%E4%BD%A0%E5%AD%A6Shell/pic/image-20210317192301622.png)]
case语句用在当脚本中需要频繁使用if、elif、else时候,能够简化繁琐的if判断。
case语法
case "变量" in
值1)
代码1。。
;;
值2)
代码2。。
;;
*)
代码3。。
esac
解释:
case将获取的变量的值和表达式的条件值比较,匹配上了就执行对应的代码,到分号结束。
特殊理解下
case "找老公" in
家里十套房)
没问题,嫁了!
;;
家里有背景)
没问题,嫁了!
;;
男孩在努力的拼搏)
先加个微信,聊一聊!
;;
*)
我还有事,先走了!886
esac
图解
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KY8FttXL-1678232504596)(http://book.luffycity.com/linux-book/%E8%B6%85%E5%93%A5%E5%B8%A6%E4%BD%A0%E5%AD%A6Shell/pic/image-20210317201757380.png)]
case条件语句实践
猜用户输入的数字
[root@chaogelinux shell_program]# cat case_1.sh
#!/bin/bash
read -p "Please input a number:" num
case "$num" in
1)
echo "你输入的是1"
;;
2)
echo "你输入的是2"
;;
[3-9])
echo "你输入的是3~9的数字:$num"
;;
*)
echo "你输入的全不对,退下吧"
exit;
;;
esac
执行结果
[root@chaogelinux shell_program]# bash case_1.sh
Please input a number:2
你输入的是2
[root@chaogelinux shell_program]# bash case_1.sh
Please input a number:1
你输入的是1
[root@chaogelinux shell_program]# bash case_1.sh
Please input a number:3
你输入的是3~9的数字:3
[root@chaogelinux shell_program]# bash case_1.sh
Please input a number:5
你输入的是3~9的数字:5
[root@chaogelinux shell_program]# bash case_1.sh
Please input a number:0
你输入的全不对,退下吧
大家会发现,case语句,笔记if简单了
开发一个菜单脚本
学一个字体加颜色
[root@chaogelinux shell_program]# echo -e "\E[1;31m红色字\E[0m"
红色字
[root@chaogelinux shell_program]# echo -e "\033[31m红色字\033[0m"
红色字
有关linux输出颜色,大家可以自行搜索语法,无需记忆,搜索使用即可
脚本代码如下
[root@chaogelinux shell_program]# cat case_menu.sh
#!/bin/bash
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
# 帮助函数
usage(){
echo "Usage: $0 {1|2|3|4}"
exit 1
}
menu(){
cat <<END
1.apple
2.banana
3.orange
END
}
choose(){
read -p "请选择一种水果:" fruit
case "$fruit" in
1)
echo -e "${RED_COLOR}allple${RES}"
;;
2)
echo -e "${GREEN_COLOR}pear${RES}"
;;
3)
echo -e "${YELLOW_COLOR}banana${RES}"
;;
*)
usage
esac
}
main(){
menu
choose
}
main
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SefFExc9-1678232504597)(http://book.luffycity.com/linux-book/%E8%B6%85%E5%93%A5%E5%B8%A6%E4%BD%A0%E5%AD%A6Shell/pic/image-20210317205715211.png)]
Linux系统脚本范例
case主要还是用于起停脚本,适合变量值较少,且为固定数字或字符的情况。
如/etc/init.d/mysql
部分case语句代码
# source other config files
[ -f /etc/default/mysql ] && . /etc/default/mysql
[ -f /etc/sysconfig/mysql ] && . /etc/sysconfig/mysql
[ -f /etc/conf.d/mysql ] && . /etc/conf.d/mysql
case "$mode" in
'start')
# Start daemon
# Safeguard (relative paths, core dumps..)
cd $basedir
echo $echo_n "Starting MariaDB"
if test -x $bindir/mysqld_safe
then
# Give extra arguments to mysqld with the my.cnf file. This script
# may be overwritten at next upgrade.
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &
wait_for_ready; return_value=$?
# Make lock for RedHat / SuSE
if test -w "$lockdir"
then
touch "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "Couldn't find MariaDB server ($bindir/mysqld_safe)"
fi
;;
'stop')
# Stop daemon. We use a signal here to avoid having to know the
# root password.
if test -s "$mysqld_pid_file_path"
then
mysqld_pid=`cat "$mysqld_pid_file_path"`
if su_kill -0 $mysqld_pid ; then
echo $echo_n "Shutting down MariaDB"
su_kill $mysqld_pid
# mysqld should remove the pid file when it exits, so wait for it.
wait_for_gone $mysqld_pid "$mysqld_pid_file_path"; return_value=$?
else
log_failure_msg "MariaDB server process #$mysqld_pid is not running!"
rm "$mysqld_pid_file_path"
fi
# Delete lock for RedHat / SuSE
if test -f "$lock_file_path"
then
rm -f "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "MariaDB server PID file could not be found!"
fi
;;
'restart')
# Stop the service and regardless of whether it was
# running or not, start it again.
if $0 stop "$@"; then
if ! $0 start "$@"; then
log_failure_msg "Failed to restart server."
exit 1
fi
else
log_failure_msg "Failed to stop running server, so refusing to try to start."
exit 1
fi
;;
'reload'|'force-reload')
if test -s "$mysqld_pid_file_path" ; then
read mysqld_pid < "$mysqld_pid_file_path"
su_kill -HUP $mysqld_pid && log_success_msg "Reloading service MariaDB"
touch "$mysqld_pid_file_path"
else
log_failure_msg "MariaDB PID file could not be found!"
exit 1
fi
;;
'status')
# First, check to see if pid file exists
if test -s "$mysqld_pid_file_path" ; then
read mysqld_pid < "$mysqld_pid_file_path"
if su_kill -0 $mysqld_pid ; then
log_success_msg "MariaDB running ($mysqld_pid)"
exit 0
else
log_failure_msg "MariaDB is not running, but PID file exists"
exit 1
fi
else
# Try to find appropriate mysqld process
mysqld_pid=`pgrep -f $libexecdir/mysqld`
# test if multiple pids exist
pid_count=`echo $mysqld_pid | wc -w`
if test $pid_count -gt 1 ; then
log_failure_msg "Multiple MariaDB running but PID file could not be found ($mysqld_pid)"
exit 5
elif test -z $mysqld_pid ; then
if test -f "$lock_file_path" ; then
log_failure_msg "MariaDB is not running, but lock file ($lock_file_path) exists"
exit 2
fi
log_failure_msg "MariaDB is not running"
exit 3
else
log_failure_msg "MariaDB is running but PID file could not be found"
exit 4
fi
fi
;;
'configtest')
# Safeguard (relative paths, core dumps..)
cd $basedir
echo $echo_n "Testing MariaDB configuration syntax"
daemon=$bindir/mysqld
if test -x $libexecdir/mysqld
then
daemon=$libexecdir/mysqld
elif test -x $sbindir/mysqld
then
daemon=$sbindir/mysqld
elif test -x `which mysqld`
then
daemon=`which mysqld`
else
log_failure_msg "Unable to locate the mysqld binary!"
exit 1
fi
help_out=`$daemon --help 2>&1`; r=$?
if test "$r" != 0 ; then
log_failure_msg "$help_out"
log_failure_msg "There are syntax errors in the server configuration. Please fix them!"
else
log_success_msg "Syntax OK"
fi
exit $r
;;
'bootstrap')
if test "$_use_systemctl" == 1 ; then
log_failure_msg "Please use galera_new_cluster to start the mariadb service with --wsrep-new-cluster"
exit 1
fi
# Bootstrap the cluster, start the first node
# that initiate the cluster
echo $echo_n "Bootstrapping the cluster.. "
$0 start $other_args --wsrep-new-cluster
exit $?
;;
*)
# usage
basename=`basename "$0"`
echo "Usage: $basename {start|stop|restart|reload|force-reload|status|configtest|bootstrap} [ MariaDB server options ]"
exit 1
;;
esac