LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 08-04-2011, 04:58 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Rep: Reputation: 33
syntax error near unexpected token `fi'


I wrote a small shell script as follow:
Code:
#!/bin/bash

echo "this is my script"

RESULT=${RESULT:-"ll /var/log/jboss/mtext/"}

if [ -z $RESULT ] then
   ls /var/log/jboss/mtext/
fi
;;
But I got error "syntax error near unexpected token `fi'" when I start this script.


Thomas
 
Old 08-04-2011, 05:08 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
yep, you will. "then" is a bash builtin command, not mere syntax. Here it's passed as an option to the if, which it's not. just put the "then" on a new line.

Your code has plenty of other issues too btw... what's the ;; for? Did you copy that from a "case" example? delete it. Also you should not use "[", use "[[" instead for various boring reasons. Basically [ is the /bin/test command whilst [[ is a genuine builtin bash operation, so handles strings more logically. in your example, if $RESULT doesn't contain anything then it will cause an error, using [[ it will know that the variable is empty.
 
Old 08-04-2011, 05:39 AM   #3
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Hi,

Many thanks. It works now.

Now I extend the script as follow:
Code:
#!/bin/bash

RESULT=${RESULT:-"grep Started /var/log/jboss/mtext/server.log"}
LOG=${LOG:-"/var/log/jboss/mtext/server.log"}

if [[ -e $LOG ]]
then
  echo "server.log exist"
  if [[ -z $RESULT ]]
  then
    echo "Jboss started"
  else
    echo "JBoss not started"
  fi
else
   echo "server.log doesn't exist"
fi
I think the syntactic should be correct.

The server.log exist and the $RESULT is not empty but it shows even though
Code:
server.log exist  (this is correct)
JBoss not started (this is not correct)
Anything wrong?

Thomas
 
Old 08-04-2011, 05:48 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
Anything wrong?
Well I would have to say yes if you are not getting the results you expect!

Maybe you should echo the variable RESULT and see exactly what is in it??
 
Old 08-04-2011, 06:26 AM   #5
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
I add the echo and the script ölooks as follow now:
Code:
#!/bin/bash

RESULT=${RESULT:-"grep Started /var/log/jboss/mtext/server.log"}
LOG=${LOG:-"/var/log/jboss/mtext/server.log"}

if [[ -e $LOG ]]
then
  echo "server.log exist"
  echo "RESULT = " $RESULT
  if [[ -z $RESULT ]]
  then
    echo "Jboss started"
  else
    echo "JBoss not started"
  fi
else
   echo "server.log doesn't exist"
fi
As I run this script I got:
Code:
server.log exist
RESULT =  grep Started /var/log/jboss/mtext/server.log
JBoss not started
1.
One can see the RESULT is not empty.

2.
I wonder why the "grep Started /var/log/jboss/mtext/server.log" doesn't work.
 
Old 08-04-2011, 06:45 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
is does "work" you're just setting that as a string.

just use this:

RESULT=$(grep whatever)

much simpler. No idea where you've picked up all this odd syntax from.

In terms of what that command is trying to do, I would suggest a different approach though:

RESULT=$(grep -c whatever)
if [[ $RESULT -gt 0 ]]
then
...
fi

i.e. count the number of times that grep finds a hit. more elegant that way. another common alternative would be to use the exit code of the grep, not the output:

grep -q whatever
if [[ $? -eq 0 ]]
then
... exit code was zero, so grep "worked"
fi

Last edited by acid_kewpie; 08-04-2011 at 06:48 AM.
 
1 members found this post helpful.
Old 08-04-2011, 06:51 AM   #7
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Now I change the script as follow:
Code:
#!/bin/bash

function grepLog(){
  grep "Started in" /var/log/jboss/mtext/server.log
}

LOG=${LOG:-"/var/log/jboss/mtext/server.log"}

if [[ -e $LOG ]]
then
  echo "server.log exist"
#  echo $(grepLog)
  result=$(grepLog)
  echo $result
  if [[ -z  "$result" ]]
  then
    echo "JBoss started"
  else
    echo "JBoss not started"
  fi
else
   echo "server.log doesn't exist"
fi
The ran result is:
Code:
server.log exist
2011-08-04 12:32:54,098 INFO [org.jboss.bootstrap.microcontainer.ServerImpl] (main) JBoss (Microcontainer) [5.0.1 (build: SVNTag=JBPAPP_5_0_1 date=201003301050)] Started in 2m:19s:741ms
JBoss not started
One can see the result is not empty. But it show always "JBoss not started".

Thomas

Last edited by thomas2004ch; 08-04-2011 at 06:55 AM.
 
Old 08-04-2011, 06:54 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
no no, you're now trying to execute a program called grepLog, that makes no sense. You don't need a function for it.
 
Old 08-04-2011, 06:58 AM   #9
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Sorry, I make mistake. The -z means empty. I have to change the script as follow:
Code:
#!/bin/bash

function grepLog(){
  grep "Started in" /var/log/jboss/mtext/server.log
}

LOG=${LOG:-"/var/log/jboss/mtext/server.log"}

if [[ -e $LOG ]]
then
  echo "server.log exist"
#  echo $(grepLog)
  result=$(grepLog)
  echo $result
  if [[ -z  "$result" ]]
  then
    echo "JBoss not started"
  else
    echo "JBoss started"
  fi
else
   echo "server.log doesn't exist"
fi
 
Old 08-04-2011, 07:00 AM   #10
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by acid_kewpie View Post
no no, you're now trying to execute a program called grepLog, that makes no sense. You don't need a function for it.
Ok, I change the script as you recommended as follow. It print correctly.
Code:
#!/bin/bash

function grepLog(){
  grep "Started in" /var/log/jboss/mtext/server.log
}

LOG=${LOG:-"/var/log/jboss/mtext/server.log"}

if [[ -e $LOG ]]
then
  echo "server.log exist"
#  echo $(grepLog)
#  result=$(grepLog)
  result=$(grep "Started in" /var/log/jboss/mtext/server.log)
  echo $result
  if [[ -z  "$result" ]]
  then
    echo "JBoss not started"
  else
    echo "JBoss started"
  fi
else
   echo "server.log doesn't exist"
fi
Many thanks.

Thoams
 
Old 08-04-2011, 07:14 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Your other option is to do away with the variable altogether:
Code:
if grep -q "Started in" $LOG
then
    echo "JBoss started"
else
    echo "JBoss not started"
fi
 
Old 08-04-2011, 07:53 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by thomas2004ch View Post
Now I extend the script as follow:
Code:
if [[ -e $LOG ]]
then
   <stuff>
fi
If you like your if-fi to line up vertically you can use
Code:
if [[ -e $LOG ]]; then
   <stuff>
fi
 
  


Reply



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 jchmski Programming 5 06-18-2010 10:28 AM
sh: syntax error near unexpected token `(' venkatesh_b Linux - Newbie 1 05-16-2009 05:44 AM
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 > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:39 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