mysql - syntax error near unexpected token `done' in bash code -
i looking working shell code auto restart mysql servers if not working. here code witch foound in google search, listen 3306 port, if not working, restart. if can not restart, reboot server.
can code work? if not, can share me working code? if yes, met syntax error near unexpected tokendone' in bash code`, how solve it? thanks.
port=`netstat -na|grep "listen"|grep "3306"|awk -f[:" "]+ '{print $5}'` mysqlip=`ifconfig eth0|awk '/inet/{print $2}'|cut -c 6-` while [ `whoami` == "root" ] if [ "$port" == "3306" ];then echo "mysql running......" else service mysql restart if [ "$port" == "3306" ];then else reboot fi fi break done modify code:
port=`netstat -na|grep "listen"|grep "3306"|awk -f[:" "]+ '{print $5}'` mysqlip=`ifconfig eth0|awk '/inet/{print $2}'|cut -c 6-` while [ `whoami` == "root" ] if [ "$port" == "3306" ];then echo "mysql running......" else service mysql restart if [ "$port" == "3306" ];then : else reboot fi fi break done
your condition test must execute sort of code if true.
it looks want reboot if "$port" not "3306".
you should use break when testing condition, otherwise loop execute once.
the port variable not update unless call code sets again after need update.
also, don't need use grep when using awk.
#!/bin/bash callport () { port=`netstat -na|awk -f[:" "]+ '/listen/ && /3306/ {print $5}'` } mysqlip=`ifconfig eth0|awk '/inet/{print $2}'|cut -c 6-` while [ `whoami` == "root" ] callport if [ "$port" == "3306" ];then echo "mysql running......" break else service mysql restart callport if [ "$port" != "3306" ];then reboot fi fi done
Comments
Post a Comment