Linux Script嵌套if语句给出语法错误

时间:2011-11-18 16:34:54

标签: linux bash if-statement

我正在尝试将多个if语句嵌套如下:

#!/bin/bash
# start_server.sh
# 
# Use this script to start the MarketDataTransmitter.
#
# Usage: ./start_server.sh    Starts the MarketDataTransmitter.

reset=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
cyan=$(tput setaf 6)
echo
directory=$(ls -l)
check_exist=$(awk -v a="$directory" -v b="MarketDataTransmitter" 'BEGIN { print index(a, b) }')
if [ "$check_exist" = "0" ]; then
  # MarketDataTransmitter is not present.
  echo "${red}[ERROR]${reset} Could not start ${yellow}MarketDataTransmitter${reset}."
  echo "        ${yellow}MarketDataTransmitter${reset} could not be found."
else
  # MarketDataTransmitter is present.
  processes=$(ps -ef | grep -i "MarketDataTransmitter" | grep -v "grep" | grep -v "bash" | awk '{ print $8 }')
  check_run=$(awk -v a="$processes" -v b="MarketDataTransmitter" 'BEGIN { print index(a, b) }')
  if [ "$check_run" = "0" ]; then
    # MarketDataTransmitter is not running.
    if [ -e "srv.log" ]; then
      if [ -s "srv.log" ]; then
        if [ -d "logs" ]; then
          date_time=$(date '+%Y%m%d_%H_%M_%S')
          new_log_name="srv_$date_time.log"
          mv srv.log $new_log_name
          mv $new_log_name logs
        else
          mkdir logs
          date_time=$(date '+%Y%m%d_%H_%M_%S')
          new_log_name="srv_$date_time.log"
          mv srv.log $new_log_name
          mv $new_log_name logs
        fi
      else
        echo "srv.log is empty and will be removed."
        rm -rf srv.log
      fi
    else
      # No srv.log but this is to start MarketDataTransmitter so we can ignore.
    fi
    ./MarketDataTransmitter > srv.log &
    echo "${yellow}MarketDataTransmitter${reset} has been started."
  else
    # MarketDataTransmitter is already running.
    echo "${red}[ERROR]${reset} Could not start ${yellow}MarketDataTransmitter${reset}."
    echo "        ${yellow}MarketDataTransmitter${reset} is already running."
  fi
fi
echo

但它给我语法投诉说:

意外标记“fi”附近的语法错误

在最后一个'fi'

有谁知道为什么?

感谢。

[编辑]已发布完整代码。

1 个答案:

答案 0 :(得分:6)

你有一个else语句和fi语句,第44-46行之间没有任何内容(只是它们之间的注释)。在bash中,你需要在else块的主体中​​有一些语句,或者取出else块。