# 注意:shell脚本批量执行命令,不能只写一个函数,然后把所有命令复制进去,之前试过这样是不行的。必须要有一个判断命令执行成功与否的语句
# 简单的命令可以不加结果判断符号,但是遇到解压包、sed 的命令就不往下执行了。测试过
实例1、 shell脚本批量开启防火墙端口 [mysql@www ~]$ cat firewall.sh
#!/bin/bash
func1(){
firewall-cmd --zone=public --add-port=/tcp --permanent
[ $? -eq ] firewall-cmd --zone=public --add-port=/tcp --permanent
[ $? -eq ] firewall-cmd --zone=public --add-port=/tcp --permanent
[ $? -eq ] firewall-cmd --zone=public --add-port=/tcp --permanent
[ $? -eq ] firewall-cmd --zone=public --add-port=/tcp --permanent
[ $? -eq ] firewall-cmd --zone=public --add-port=/tcp --permanent
[ $? -eq ] firewall-cmd --zone=public --add-port=/tcp --permanent
[ $? -eq ] firewall-cmd --zone=public --add-port=/tcp --permanent
[ $? -eq ] firewall-cmd --reload
} func1 sudo ./firewall.sh > /dev/null #这个sudo就相当于脚本里面所有命令行写上了sudo 实例2、 # shell脚本安装python、pip 首先把pip-18.0.tar.gz 、Python-3.6..tgz 安装包放在 /usr/local 下面,按照顺序先安装pip,再安装python。不要先安装或只安装python,否则很容易出错, [root@bogon ~]# cat pip-python.sh
#!/bin/bash
func1(){
yum -y groupinstall "Development tools"
[ $? -eq ] && yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
} func2(){
yum remove pip -y && cd /usr/local/ && tar -xf pip-18.0.tar.gz && cd pip-18.0/ && python setup.py install
[ $? -eq ] && echo "安装完成pip"
} func3(){
cd /usr/local/ && tar -zxvf Python-3.6..tgz && mv Python-3.6. python3 && cd python3 && ./configure && make && make install
[ $? -eq ] && mv /usr/bin/python /usr/bin/python.bak
[ $? -eq ] && ln -s /usr/local/bin/python3 /usr/bin/python && ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[ $? -eq ] && whereis pip && /usr/local/bin/pip3. install --upgrade pip && /usr/local/bin/pip install paramiko
[ $? -eq ] && sed -i "1c #!/usr/bin/python2.7" /usr/bin/yum
[ $? -eq ] && sed -i "1c #!/usr/bin/python2.7" /usr/libexec/urlgrabber-ext-down
[ $? -eq ] && sed -i "1c #!/usr/bin/python2.7" /usr/sbin/firewalld
[ $? -eq ] && sed -i "1c #!/usr/bin/python2.7" /usr/sbin/firewalld
[ $? -eq ] && systemctl restart firewalld
[ $? -eq ] && echo "安装完成python"
} #脚本菜单函数 menu(){
clear
echo "# 1. 安装依赖包"
echo "# 2. 安装pip"
echo "# 3. 安装python"
echo "# 4. 退出程序"
} #功能选择函数
choice(){
read -p "Please choice a menu[1 2 3 4]:" select } #程序开始
while true
do
menu
choice
case $select in
)
func1;;
)
func2;;
)
func3;;
)
exit;;
*)
echo "choice error"
esac
done