4

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.

2
  • 2
    Can you post your actual code? Most likely you have a syntax error due to missing one of the then statements, or too many fis or something similar Commented Nov 18, 2011 at 16:35
  • Hi Jarek, thanks for the quick reply. Full code has been posted. Commented Nov 18, 2011 at 16:41

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.