LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-01-2010, 02:09 PM   #1
dnaqvi
Member
 
Registered: Oct 2009
Posts: 117

Rep: Reputation: 15
syntax error near unexpected token `else'


I wrote a script to start portal.
Its start portal (not a problem)
Only getting following message:

./WebPortaltest.sh: line 56: syntax error near unexpected token `else'
./WebPortaltest.sh: line 56: `else'

This is the else before second logic

{/}
#
else
if [ "$host" == "prd6" ] || [ "$host" == "stg6" ] ||
[ "$host" == "wprd6" ] || [ "$host" == "tst6" ]; {/}


Here is the whole script:
{/}
#------------------> web_portal_start.sh <-------------------#
# #
# Modification log #
# ---------------- #
# #
# ------Please add new entries at the top. Be proud #
# | of your work. Comment it! #
# V #
# DATE WHO DESCRIPTION #
# ---- --- ----------- #
# 01/12/10 tst initial release to start portal #
# #
#-------------------------------------------------------------#
#!/bin/bash
set -x
host=$(hostname);
dt=$(date +'%D %T')
logfile='PortalUp.log'
profile_path='/opt/IBM/WebSphere/wp_profile'
#
#--> Determine which portal to use based on host name <--
#--> set portal1 for host name prd5, stg5, <--
# wprd5, tst5, <--
# wstg5 or dev2 <--
#
#--> set portal2 for host name prd6, stg6, <--
# wprd6, or tst6 <--
#
#portal_start='null';
#
if [ "$host" == "prd5" ] || [ "$host" == "stg5" ] ||
[ "$host" == "wprd5" ] || [ "$host" == "tst5" ] ||
[ "$host" == "wstg5" ] || [ "$host" == "dev2" ];
then
su -l admin -c /home/admin/scripts_PS/startportal & >> /tmp/weblog/$logfile
echo $dt start command for $server_name on $host Server has been issued. >> /tmp/weblog/$logfile
fi
# check every 3 minutes to see if server has been started. if server does not start in 30 minutes (10 checks), exit the script with
# return code 8
let "count = 0"
while [ $count -lt 11 ]
do
let "count = count +1"
#
dt=$(date +'%D %T')
#
if [ -f $profile_path/logs/$server_name/$server_name.pid ]; then
echo $dt $server_name on $host server has been successfully started. >> /tmp/weblog/$logfile
exit
#
else
echo $dt start process for $server_name on $host server is still running. checked $count times, 3 minutes apart >> /tmp/weblog/$logfile
sleep 180
fi
#
else
if [ "$host" == "prd6" ] || [ "$host" == "stg6" ] ||
[ "$host" == "wprd6" ] || [ "$host" == "tst6" ];
then
su -l admin -c /home/admin/scripts_PS/startportal_2 & >> /tmp/weblog/$logfile
echo $dt start command for $server_name on $host Server has been issued. >> /tmp/weblog/$logfile
fi
# check every 3 minutes to see if server has been started. if server does not start in 30 minutes (10 checks), exit the script with
# return code 8
let "count = 0"
while [ $count -lt 11 ]
do
let "count = count +1"
#
dt=$(date +'%D %T')
#
if [ -f $profile_path/logs/$server_name/$server_name.pid ]; then
echo $dt $server_name on $host server has been successfully started. >> /tmp/weblog/$logfile
exit
#
else
echo $dt start process for $server_name on $host server is still running. checked $count times, 3 minutes apart >> /tmp/weblog/$logfile
sleep 180
fi
done
#
dt=$(date +'%D %T')
echo $dt start process for $server_name on $host server DID NOT complete successfully. checked $count times, 3 minutes apart >> /tmp/weblog/$logfile
exit 8
#
{\}


Thanks
 
Old 12-01-2010, 02:18 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
There is no if before the else at line 56:
Code:
47 if [ -f $profile_path/logs/$server_name/$server_name.pid ]; then
48   echo $dt $server_name on $host server has been successfully started. >> /tmp/weblog/$logfile
49   exit
50   #
51 else
52   echo $dt start process for $server_name on $host server is still running. checked $count times, 3 minutes apart >> /tmp/weblog/$logfile
53   sleep 180
54 fi
55 #
56 else
57 if [ "$host" == "prd6" ] || [ "$host" == "stg6" ] ||
58    [ "$host" == "wprd6" ] || [ "$host" == "tst6" ];
59 then
60   su -l admin -c /home/admin/scripts_PS/startportal_2 & >> /tmp/weblog/$logfile
61   echo $dt start command for $server_name on $host Server has been issued. >> /tmp/weblog/$logfile
62 fi
If you want to put an "else if" condition the correct syntax is elif, as in
Code:
if expression
then
  <commands>
elif expression
  <commands>
fi
 
Old 12-01-2010, 03:27 PM   #3
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
It's amazing what proper indentation and code block markers can do.
 
Old 12-07-2010, 05:17 PM   #4
dnaqvi
Member
 
Registered: Oct 2009
Posts: 117

Original Poster
Rep: Reputation: 15
I repalced else & if to elif

Still woks but now I am getting message as below:

./WebPortalDntest.sh: line 56: syntax error near unexpected token `elif'
./WebPortalDntest.sh: line 56: `elif'

Here is the script:
{/}
elif
[ "$host" == "prd6" ] || [ "$host" == "stg6" ] ||
[ "$host" == "wprd6" ] || [ "$host" == "tst6" ];
then
su -l admin -c /home/admin/scripts_PS/startportal_2 & >> /tmp/weblog/$logfile
{\}

Last edited by dnaqvi; 12-07-2010 at 09:56 PM.
 
Old 12-07-2010, 07:34 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by dnaqvi View Post
I repalced else & if to elif

Still woks but now I am getting message as below:

./WebPortalDntest.sh: line 56: syntax error near unexpected token `elif'
./WebPortalDntest.sh: line 56: `elif'

Here is the script:
{/}
elif
[ "$host" == "prd6" ] || [ "$host" == "stg6" ] ||
[ "$host" == "wprd6" ] || [ "$host" == "tst6" ];
then
su -l admin -c /home/admin/scripts_PS/startportal_2 & >> /tmp/weblog/$logfile
{/}
Hi,

Colucix already gave you the answer. You do not have an if before your else. You cannot just start and else branch nor can you just start with an elif branch. Just remove the else completely in your previous script. This should fix the syntax. Not sure if the logic of your script is correct, though. Your script is unreadable. Please use code tags and reformat it properly.
 
Old 12-08-2010, 03:37 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
As crts said: the else or elif keyword is left alone. Check the sequence of control statements and eventually review the script's logic.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error near unexpected token `then' snakernetb Linux - Server 4 02-16-2010 06:40 PM
Syntax error near unexpected token :( please help mikejosh Programming 4 01-29-2010 06:54 AM
syntax error near unexpected token Aigarzs Linux - Newbie 3 01-12-2008 03:39 PM
syntax error for unexpected token `(' Steve Spurr Linux - Newbie 6 09-22-2006 08:19 AM
syntax error near unexpected token ` mattyspatty Programming 8 05-07-2006 05:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration