bash代码中意外令牌“完成”附近的语法错误

时间:2022-07-14 23:03:23

I am looking for some working shell code for auto restart mysql servers if it is not working. here are some code witch i foound in google search, listen 3306 port, if it is not working, restart. if can not restart, reboot the server.

我正在寻找一些自动重启mysql服务器的工作shell代码,如果它不工作。这里有一些代码巫婆我在谷歌搜索,听3306端口,如果它不工作,重启。如果无法重启,请重启服务器。

Can this code work? If not, is anyone can share me a working code? If yes, i met syntax error near unexpected tokendone' in bash code`, how to solve it? thanks.

这段代码可以工作吗?如果没有,是否有人可以与我分享工作代码?如果是的话,我在bash代码中遇到了意外的tokendone'附近的语法错误,如何解决?谢谢。

PORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`
MYSQLIP=`ifconfig eth0|awk '/inet/{print $2}'|cut -c 6-`
while [ `whoami` == "root" ]
do
  if [ "$PORT" == "3306" ];then
    echo "mysql is 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" ]
do
  if [ "$PORT" == "3306" ];then
    echo "mysql is running......"
  else
    service mysql restart
       if [ "$PORT" == "3306" ];then
         :
       else
           reboot
       fi
  fi
break
done

1 个解决方案

#1


1  

Your condition test must execute some sort of code if true.

如果为true,您的条件测试必须执行某种代码。

It looks like you want to reboot if "$PORT" is not "3306".

如果“$ PORT”不是“3306”,您似乎想要重新启动。

You should only use break when testing a condition, otherwise the loop will only execute once.

您应该只在测试条件时使用break,否则循环只会执行一次。

The PORT variable will not update unless you call the code that sets it again after you need it to update.

PORT变量不会更新,除非您在需要更新后再调用设置它的代码。

Also, you don't need to use grep when you are using awk.

此外,使用awk时不需要使用grep。

#!/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" ]
do
  callport
  if [ "$PORT" == "3306" ];then
    echo "mysql is running......"
    break
  else
    service mysql restart
    callport
       if [ "$PORT" != "3306" ];then
           reboot
       fi
  fi
done

#1


1  

Your condition test must execute some sort of code if true.

如果为true,您的条件测试必须执行某种代码。

It looks like you want to reboot if "$PORT" is not "3306".

如果“$ PORT”不是“3306”,您似乎想要重新启动。

You should only use break when testing a condition, otherwise the loop will only execute once.

您应该只在测试条件时使用break,否则循环只会执行一次。

The PORT variable will not update unless you call the code that sets it again after you need it to update.

PORT变量不会更新,除非您在需要更新后再调用设置它的代码。

Also, you don't need to use grep when you are using awk.

此外,使用awk时不需要使用grep。

#!/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" ]
do
  callport
  if [ "$PORT" == "3306" ];then
    echo "mysql is running......"
    break
  else
    service mysql restart
    callport
       if [ "$PORT" != "3306" ];then
           reboot
       fi
  fi
done