大家好,又见面了,我是你们的朋友全栈君。
(1)case 语法
代码语言:javascript复制case "变量" in
模式1) 命令序列1 ;;
模式2) 命令序列2 ;;
模式3) 命令序列3 ;;
*) 无匹配后命令序列
esac
(2)多系统配置yum源
代码语言:javascript复制#!/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)删除用户
代码语言:javascript复制#!/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
代码语言:javascript复制#!/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
代码语言:javascript复制trap :运行脚本的时候无法使用crtl c退出脚本
clear 每次登陆到后端服务器退出之后清屏
cat :打印菜单
echo -en "e[1;32mplease input a number: e[0m" :提示用户输入的时候打印颜色,-n表示下面的read用户输入不换行
,密钥登录:ssh-keygen:生成跳板机的公钥和私钥 ssh-copy-id 把跳板机的公钥发送给后端服务器
客户端登录到跳板机上每次启动这个脚本需要把脚本放入到~/.bashrc文件下面,脚本需要给执行权限
(5)系统工具箱
代码语言:javascript复制#!/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
代码语言:javascript复制#!/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
代码语言:javascript复制#!/bin/bash
php56() {
echo -e "e[1;31minstall php5.6 is successe[0m"
}
php66() {
echo -e "e[1;33minstall php6.6 is suceesse[0m"
}
总结:把安装php的各种版本一个文件,文件里面是函数安装php各种版本的函数,然后在入口文件使用. /server/scripts/php.sh加载这个文件,在定义函数调用文件中的函数功能即可
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168308.html原文链接:https://javaforall.cn