测试开发必备技能之shell脚本编程二【多测师】
Shell脚本练习
1,启动/关闭/重启/查看状态Nginx脚本;
[root@localhost xiaoshu5]# vim xiaoshu5.sh
#!/bin/bash
#
if [ "$1" == "start" ];then
service nginx start
echo "start"
elif [ "$1" == "stop" ];then
service nginx stop
echo "stop"
elif [ "$1" == "restart" ];then
service nginx stop
echo "stop"
service nginx start
echo "start"
elif [ "$1" == "status" ];then
pid=`ps -ef|grep nginx|grep -v grep|awk "{print $5}"`
if [ "$pid" != "" ];then
echo "running"
else
echo "not running"
fi
else
echo "Usage[start|stop|restart|status]"
fi
2,写一个shell脚本,自动备份数据库生成的文件后并打包,自动传输到一台远程主机,然后把备份的文件和包都删除;
1) 写好shell脚本:
#! /bin/bash
time=`date +%Y%m%d%H`
mysqldump -cp --user=root --password='123456' cms | gzip > ~/mysql_shell_bak/cms_$time.sql.gz ;
umask 177
name=`scp ~/mysql_shell_bak/cms_$time.sql.gz root@192.168.0.157:~/backup`
name1=`rm -rf ~/mysql_shell_bak/cms_$time.sql.gz`
2) 设置进入远程主机无密钥:
3) 设置好之后就可以不用远程主机的密码直接连接了;