LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2011, 04:44 PM   #1
nav007_2000
LQ Newbie
 
Registered: Feb 2011
Posts: 3

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


Hello all,
I have an ubuntu 10.10 server and want to run a script on it to check if a process is running. If it is not running, it will start the process and also write into a log file.

When running the script i get the following error message:

syntax error near unexpected token `else'

Here is my script. Hope someone can help.

#!/bin/bash
process=`ps auxwww | grep test.x6 | grep -v grep | awk '{print $1}'`
if [ -z "$process" ]; then
echo "Couldn't find test.x86 running Restarting test Server" >>/var/testlog/test.check
echo && date >>/var/testlog/test.check
/emu/test/test.x86 -d >>/var/testlog/testdbg.log;
else echo "test.x86 is still OK!" >>/var/testlog/test.check
fi

Last edited by nav007_2000; 02-10-2011 at 04:46 PM.
 
Old 02-10-2011, 05:06 PM   #2
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
Greetingz!

I've always seen "else" get it's own line....

Code:
#!/bin/bash
process=`ps auxwww | grep test.x6 | grep -v grep | awk '{print $1}'`
if [ -z "$process" ]; then
   echo "Couldn't find test.x86 running Restarting test Server" >>/var/testlog/test.check
   echo && date >>/var/testlog/test.check
   /emu/test/test.x86 -d >>/var/testlog/testdbg.log;
else
   echo "test.x86 is still OK!" >>/var/testlog/test.check
fi
 
Old 02-10-2011, 05:10 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Yes, and the semicolon before the else is not necessary. A cleaner way to test for the process would be to use pgrep. Is ps and grep all in one and it sets the return code based on a find. Here is its usage:

Code:
EXECUTABLE=/emu/test/test.x86
PROCESS=`basename $EXECUTABLE`
CHECK_FILE=/var/log/$PROCESS.check
LOG_FILE=/var/log/$PROCESS.log

pgrep $PROCESS  > /dev/null
if [ $? -eq 1 ]; then
echo "Couldn't find $PROCESS running Restarting Server" >> $CHECK_FILE
date >> $CHECK_FILE
$EXECUTABLE &
else
   echo "$PROCESS is still OK!" >> $CHECK_FILE
fi
 
Old 02-11-2011, 03:25 AM   #4
nav007_2000
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for your help crabboy

i now get the following errors:

./chk.sh: line 14: syntax error: unexpected end of file

Last edited by nav007_2000; 02-11-2011 at 03:32 AM.
 
Old 02-11-2011, 03:47 AM   #5
appilu
Member
 
Registered: Jan 2011
Distribution: RedHat,Debian-Ubuntu,Fedora
Posts: 73

Rep: Reputation: 8
Hi nav,

your first code itself is working nice without semicolon.
Also crabboy's code is working fine .
 
Old 02-11-2011, 04:46 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You will need to show what your newly adjusted code looks like seeing you have changed it and now have a different error.
 
Old 02-11-2011, 08:07 AM   #7
nav007_2000
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Could it be that I am running the code on an ubuntu desktop version?

Here is my code:

EXECUTABLE=/emu/cccam/cccam.x86
PROCESS=`basename $EXECUTABLE`
CHECK_FILE=/var/log/$PROCESS.check
LOG_FILE=/var/log/$PROCESS.log
pgrep $PROCESS > /dev/null
if [ $? -eq 1 ]; then
echo "Couldn't find $PROCESS running Restarting Server" >> $CHECK_FILE
date >> $CHECK_FILE
$EXECUTABLE &
else
echo "$PROCESS is still OK!" >> $CHECK_FILE
fi
 
Old 02-11-2011, 09:14 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

your syntax looks ok. The semicolon/else issue is unusual but it should not throw an error. I see a windows logo in the left column of your post. Did you by any chance edit/create the file under windows? It might be some CR/LF issue. Execute the following command
Code:
od -c /path/to/your/script
Look out for suspicious character-sequencies like
\r\n

If it turns out that it is a CR/LF issue then you can correct this with
Code:
sed -i 's/\r//' <your file>
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error near unexpected token `else' dnaqvi Linux - General 5 12-08-2010 03:37 AM
Syntax error near unexpected token fi Nortekman Linux - Newbie 8 02-27-2009 03:48 PM
syntax error near unexpected token deskjockey39 Linux - General 2 01-16-2007 08:47 AM
syntax error for unexpected token `(' Steve Spurr Linux - Newbie 6 09-22-2006 08:19 AM
syntax error near unexpected token `else' josedias Programming 3 09-11-2006 07:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:20 PM.

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