2013-04-21 147 views
0

我写了一个脚本来检查ntp服务。它更新它,然后启动服务。运行shell脚本时出错

有三件事情我做下面的脚本:

  1. 检查服务是否配置正确,就是这样运行。然后,从脚本中退出 。即什么都不做。
  2. 如果服务配置正确,那么只需启动服务并退出脚本。
  3. 如果服务未配置,则停止服务 - >配置 服务 - >启动服务。

从上面的场景中,场景1和场景3工作正常。但在测试场景-2,它引发错误下面:

〜#SH ntpConfigure.sh ntpConfigure.sh:行3:NTP_CONF.BAK =的/ etc/ntp.conf.backup:找不到 NTPD没有运行 /etc/init.d/ntpd未运行 服务器time.myCompany.com /etc/ntp.conf中配置 服务已配置,启动NTP服务 开始NTPD ntpConfigure.sh:行77:echo服务启动成功:未找到

任何想法为什么它会抛出这个错误?我查了一切,但我的代码看起来不错。功能正常工作与上述错误。有任何想法吗?

代码:

#VERIFY_NTPQ="/bin/ntpq -p" 
NTP_CONF="/etc/ntp.conf" 
NTP_CONF.BAK="/etc/ntp.conf.backup" 
NTPQ_SERVICE="/etc/init.d/ntpd" 
TOUCH="/bin/touch" 
GREP="/bin/grep" 
CP="/bin/cp" 
CHKCONFIG="/bin/chkconfig" 

$NTPQ_SERVICE status 
if [ $? -eq 0 ] 
then 
     isSERVICE_RUNNING=true 
     echo "$NTPQ_SERVICE is running" 
else 
     isSERVICE_RUNNING=false 
     echo "$NTPQ_SERVICE is NOT running" 
fi 

$TOUCH $NTP_CONF 
$GREP "server" $NTP_CONF 
if [ $? -eq 0 ] 
then 
     isSERVICE__CONFIGURED=true 
     echo "$NTP_CONF is configured" 
else 
     isSERVICE__CONFIGURED=false 
     echo "$NTP_CONF is NOT configured" 
fi 

if $isSERVICE_RUNNING && $isSERVICE__CONFIGURED 
then 
     echo "ntp service is already configured, nothing to do" 
     exit 0 
elif $isSERVICE__CONFIGURED 
then 
     echo "Service is configured already, starting ntp service" 
     $NTPQ_SERVICE start 
     if [ $? -eq 0 ] 
     then 
       echo" Service is started successfully" 
       exit 0 
     else 
       echo "Failed to start service" 
       exit 1 
     fi 
else 
     echo "$NTP_CONF not configured, Configuring $NTP_CONF" 
     echo "Stopping ntp service " 
     $NTPQ_SERVICE stop 
     echo "Taking backup of $NTP_CONF file to $NTP_CONF.BAK" 
     $CP $NTP_CONF $NTP_CONF.BAK 
     echo "Updating $NTP_CONF file with the required configurations" 
     echo "restrict 127.0.0.1" > $NTP_CONF 
     echo "restrict default kod nomodify notrap" >> $NTP_CONF 
     echo "driftfile /etc/ntp.drift" >> $NTP_CONF 
     echo "server time.myCompany.com" >> $NTP_CONF 
     if [ $? -eq 0 ] 
     then 
       echo "$NTP_CONF is configued properly" 
       echo "Starting ntp service" 
       $NTPQ_SERVICE start 
       if [ $? -eq 0 ] 
       then 
         echo "ntp service configured and started successfully" 
         echo "Configuring ntp service to start at bootup" 
         $CHKCONFIG ntpd on 
         exit 0 
       else 
         echo "Failed to start ntp service" 
         exit 1 
       fi 
     else 
       echo "Failed to configure ntp service" 
       exit 1 
     fi 
fi 
+0

'回声 “' - >'回声”' – blue 2013-04-21 21:54:20

+1

壳牌变量名可以只包含字母数字和下划线,并且不能以数字开头。您正在尝试在第3行分配的变量包含一个点,以防止赋值(以及将来的任何引用)成功发生。 – michaelb958 2013-04-21 21:59:58

+0

感谢Michael提供的信息。我会相应地更改我的脚本。 – 2013-04-21 22:09:08

回答

1

您拼写错误的命令: echo"echo "在管线77

+0

非常感谢。有效。不知何故,我错过了它,偶然发现它。再次感谢,我的代码现在正在工作。 – 2013-04-21 22:00:25

+0

很高兴能帮到你!你应该接受我的回答,并在你的问题的评论中听取michaelb的建议:D – blue 2013-04-21 22:01:49