case模式匹配
case 语法结构
一、case 语法结构(字符串比较)
case 变量 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
模式3)
命令序列3
;;
*)
无匹配后命令序列
esac
示例1
编写脚本,由用户输入字符串,如果输入的字符串为Linux则显示Windows,为Windows则显示Linux,否则显示other
#!/bin/bash
read -p "Input string: " str
case $str in
windows)
echo "Linux..."
;;
linux)
echo "Windows..."
;;
*)
echo "Other...."
;;
esac
示例2
1.根据不同的系统版本,使用 if 配置yum源,
2.快速修改脚本为case
if 语句示例
os_version=`cat /etc/redhat-release | awk '{print $4}'|awk -F"." '{print $1}' `
if [ "$os_version" = "7" ];then
cat > /etc/yum.repos.d/centos7.repo <<-EOF
[centos]
name=centos7
baseurl=ftp://10.18.40.100/centos7u3
gpgcheck=0
EOF
yum makecache
yum repolist
echo "yum Configuration completed"
fi
if [ "$os_version" = "6" ];then
cat > /etc/yum.repos.d/centos6.repo <<-EOF
[centos]
name=centos6
baseurl=ftp://10.18.40.100/centos6u2
gpgcheck=0
EOF
yum makecache
yum repolist
echo "yum Configuration completed"
fi
if [ "$os_version" = "5" ];then
cat > /etc/yum.repos.d/centos5.repo <<-EOF
[centos]
name=centos5
baseurl=ftp://10.18.40.100/centos5u2
gpgcheck=0
EOF
yum makecache
yum repolist
echo "yum Configuration completed"
fi
case语句示例
os_version=`cat /etc/redhat-release | awk '{print $4}'|awk -F"." '{print $1}' `
case "$os_version" in
"7")
cat > /etc/yum.repos.d/centos7.repo <<-EOF
[centos]
name=centos7
baseurl=ftp://10.18.40.100/centos7u3
gpgcheck=0
EOF
yum makecache&>/dev/null
yum repolist&>/dev/null
echo "yum Configuration completed"
;;
6)
cat > /etc/yum.repos.d/centos6.repo <<-EOF
[centos]
name=centos6
baseurl=ftp://10.18.40.100/centos6u2
gpgcheck=0
EOF
yum makecache
yum repolist
echo "yum Configuration completed"
;;
5)
cat > /etc/yum.repos.d/centos5.repo <<-EOF
[centos]
name=centos5
baseurl=ftp://10.18.40.100/centos5u2
gpgcheck=0
EOF
yum makecache
yum repolist
echo "yum Configuration completed"
;;
*)
echo "没有您的版本"
esac
案例1
简单的模式匹配
邀请用户输入待删除的用户名,并二次询问是否删除
if 写法
#!/bin/bash
#name
#time
#1请输入删除的用户名:
read -p "please input a username : " user
#2输出用户ID
id $user &> /dev/null
#4判断用户是否存在
if [ $? -ne 0 ];then
echo "no such user: $user"
exit 1
fi
#3请用户确认是否删除
read -p "are you sure?[y/n]: " action
if [ "$action" = "y" -o "$action" = "Y" ] ;then
userdel -r $user
echo "$user is deleted!"
else
echo "thank you"
fi
case 写法
#!/bin/bash
#name
#time
#1请输入删除的用户名:
read -p "please input a username : " user
#2输出用户ID
id $user &> /dev/null
#4判断用户是否存在
if [ $? -ne 0 ];then
echo "no such user: $user"
exit 1
fi
#3请用户确认是否删除
read -p "are you sure?[y/n]: " action
#if [ "$action" = "y" -o "$action" = "Y" ] ;then
# userdel -r $user
# echo "$user is deleted!"
#else
# echo "thank you"
#fi
#5case写法
case "$action" in
Y|y|YES|yes)
userdel -r $user
echo "$user is deleted!"
;;
*)
echo "thank you"
;;
esac
简单的jumpserver
跳板主机
h)
q)
请选择要连接的主机[1-3]:
示例
定义跳板脚本
#!/usr/bin/bash
#定义目标主机IP
web1=192.168.122.152
web2=192.168.122.128
mysql1=192.168.122.88
#跳转菜单不退出
while :
do
#打印跳转菜单
cat <<EOF
1.WEB1
2.WEB2
3.MYSQL1
EOF
#读取用户输入
read -p "input number: " num
#判断用户选择
case $num in
1)
ssh alice@$web1
;;
2)
ssh alice@$web2
;;
*)
echo '123'
esac
done
跳板脚本美化
2 免密码登录
ssh-keygen
ssh-copy-id ip地址
3 使用户登录跳板机,立即进行跳转询问。
可以将登录脚本,加进登录默认启动中。
vim ~/.bash_profile
粘贴脚本地址。
5 拒绝用户使用快捷键退出,捕捉用户信号
#!/bin/bash/
trap "" HUP INT OUIT TSTP
6 美化脚本
+-----+
| |
+-----+
echo -en "\e[1;32minput number: \e[0m"
read num
系统管理工具箱
Command action
h 显示命令帮助
f 显示磁盘分区
d 显示磁盘挂载
m 查看内存使用
u 查看系统负载
q 退出程序
示例
定义脚本
1 输出菜单并测试。
vim systemmanage.sh
#!/usr/bin/bash
#打印菜单
cat <<-EOF
h. help
f. disk partation
d. filesystem mount
m. memory
u. system load
q.exit
EOF
# 读取用户输入,进行模式匹配
read -p "please input [h for help]: " action
case "$action" in
f)
fdisk -l
;;
d)
df -hT
;;
m)
free -m
;;
u)
uptime
;;
q)
exit
;;
"")
;;
*)
echo "error"
;;
esac
阶段总结
符号总结
( ) //子shell中执行: (cd /home;ls)
(( )) //比较数值,运算 C语言: ((1<2));echo $?
$( ) //命令替换,或者反撇``: touch file$(date)
$(( )) //整数运算: echo $((1+1))
{ } //集合: touch file{1..3}
${ } //字符串的提取和替换。: echo ${aaa-111}
[ ] //条件测试: [ -f file1.txt ]; echo $?
[[ ]] //条件测试,支持正则 : [[ 12 =~ [0-9]+$ ]] ;echo $ ?
$[ ] //整数运算: echo $[1+1]
概念总结
执行脚本:
# ./01.sh //<需要执行权限> 在子shell中执行
# bash 01.sh //不需要执行权限 在子shell中执行
# . 01.sh //不需要执行权限 在当前shell中执行
# source 01.sh //不需要执行权限 在当前shell中执行
提示:通常修改系统配置文件中如 /etc/profile 的PATH等变量后,使之在当前shell中生效
调试脚本的其他方法:
# sh -n 02.sh //仅调试脚本中的语法错误。
# sh -vx 02.sh //以调试的方式执行,查询整个执行过程
示例总结
判断用户输入是否是数字,否则退出
[root@localhost ~]# cat test02.sh
#!/bin/bash
#判断用户输入的是否是数字
read -p "请输入一个数值: " num
if [[ ! "$num" =~ ^[0-9]+$ ]];then
echo "你输入的不是数字,程序退出!!!"
exit
fi
echo ccc
判断用户输入的是否是数字,否则重输
[root@localhost ~]# cat test03.sh
#!/bin/bash
#判断用户输入的是否是数字
read -p "请输入一个数值: " num
while :
do
if [[ $num =~ ^[0-9]+$ ]];then
break
else
read -p "不是数字,请重新输入数值: " num
fi
done
echo "你输入的数字是: $num"