简体   繁体   中英

Linux Script Nested if-statements giving syntax error

I am trying to nest multiple if-statements as the following:

#!/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

However it is giving me syntax complaints saying:

syntax error near unexpected token `fi'

on the very last 'fi'

Does anyone know why?

Thanks.

[EDIT] Full code has been posted.

You have an else statement and fi statement with nothing between them on lines 44-46 (just a comment between them). In bash, you need to have some statement in the body of that else block, or take the else block out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM