(7)case语句

(1)case 语法
case "变量" in
模式1) 命令序列1 ;;
模式2)    命令序列2 ;;
模式3)    命令序列3 ;;
*)  无匹配后命令序列
esac
(2)多系统配置yum源
#!/bin/bash 
cat << EOF
1.install Centos5 yum repo
2.install Centos6 yum repo
3.install Centos7 yum repo
EOF
clear_cache() {
    yum clean all
    yum makecache
}
[ -d /etc/yum.repos.d/bak/ ] || mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/ &>/dev/null
read -p "please input a number ,eg 1|2|3 ...." num
case "$num" in
        1)
        wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
        clear_cache
        ;;
        2)
        wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
        clear_cache
        ;;
        3)
        wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
        clear_cache
        ;;
        *)
        echo "error number!"
esac
(3)删除用户
#!/bin/bash
#case 判断删除用户
read -p "please input username:" user
id $user &>/dev/null
if [ $? -ne 0 ];then
        echo "no such user:$user!"
        exit 1
fi
read -p "Are you sure?[y|n]: " action
case "$action" in
        y|Y|yes|YES)
                userdel -r $user
                echo "$user is deleted"
                ;;
        *)
                echo "error"
esac
(4)模拟jumpserver

1195071-20180421212756693-1229704357.png

#!/bin/bash
trap "" HUP INT OUIT TSTP
web01=192.168.111.201
web02=192.168.111.202
clear           
while true
do
        cat <<-EOF
        +-----------------------------------+
        |         jumpserver                |
        |         1)connect---web01         |
        |         2)connect---web02         |
        |         3)out                     |
        +-----------------------------------+
        EOF
        echo -en "\e[1;32mplease input a number: \e[0m"
        read  num
        case "$num" in
                1)
                ssh $web01
                ;;
                2)
                ssh $web02
                ;;
                3)
                break
                ;;
                *)
                echo "error"
        esac
done
trap :运行脚本的时候无法使用crtl+c退出脚本
clear 每次登陆到后端服务器退出之后清屏
cat :打印菜单
echo -en "\e[1;32mplease input a number: \e[0m" :提示用户输入的时候打印颜色,-n表示下面的read用户输入不换行
,密钥登录:ssh-keygen:生成跳板机的公钥和私钥 ssh-copy-id  把跳板机的公钥发送给后端服务器
客户端登录到跳板机上每次启动这个脚本需要把脚本放入到~/.bashrc文件下面,脚本需要给执行权限
(5)系统工具箱
#!/bin/bash
#system_toolbox
menu() {

        cat <<-EOF
        +==========================================+
        |       h.help                             |
        |       f.disk partition                   |
        |       d.filesystem mount                 |
        |       m.memory                           |
        |       u.system load                      |
        |       q.exit                             |
        +==========================================+
        EOF
}
menu
trap "" HUP INT OUIT TSTP
clear
while true
do
        menu
        echo -en "please enter the options you need:"
        read options
        case "$options" in
                h) clear;menu ;;
                f) df -Th ;;
                d) fdisk -l ;;
                m) free -m ;;
                u) upload ;;
                q) break ;;
                "") ;;
                *) echo -e "\e[1;32merror options \e[0m"
        esac
done
echo -e "\e[1;32mfinish...... \e[0m"
(5)安装php
#!/bin/bash
menu() {
echo "##############################"
echo -e "\t1 php5.6"
echo -e "\t2 php6.6"
echo -e "\t3 quit"
echo "##############################"
}
. /server/scripts/php.sh
install_php56() {
        php56
}
install_php66() {
        php66
}
menu
while true
do
        echo -ne "version[1-2]:" 
        read version
        case "$version" in
                1) install_php56 ;;
                2) install_php66 ;;
                3) break        ;;
                "") ;;
                *) echo "error"
        esac
done

php.sh

#!/bin/bash
php56() {
        echo -e  "\e[1;31minstall php5.6 is success\e[0m"
}
php66() {
        echo -e  "\e[1;33minstall php6.6 is suceess\e[0m"
}

总结:把安装php的各种版本一个文件,文件里面是函数安装php各种版本的函数,然后在入口文件使用. /server/scripts/php.sh加载这个文件,在定义函数调用文件中的函数功能即可

转载于:https://www.cnblogs.com/lovelinux199075/p/8901791.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值