运行shell脚本时出错

时间:2013-04-21 21:50:18

标签: shell

我编写了一个检查ntp服务的脚本。它会更新它,然后启动服务。

我在下面的脚本中做了三件事:

  1. 检查服务是否配置正确并且是否正在运行。然后,只是 退出脚本。即什么都不做。
  2. 如果服务配置正确,则只需启动服务并退出脚本。
  3. 如果未配置服务,请停止服务 - >配置 服务 - >开始服务。
  4. 从上面的场景中,scenario-1和scenario-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
    

1 个答案:

答案 0 :(得分:1)

你误导了一个命令: 第{77}行echo"应为echo "