LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 05:58 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 502

Rep: Reputation: 31
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, 06:08 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
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, 06:39 AM   #3
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 502

Original Poster
Rep: Reputation: 31
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, 06:48 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
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, 07:26 AM   #5
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 502

Original Poster
Rep: Reputation: 31
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, 07:45 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
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 07:48 AM.
 
1 members found this post helpful.
Old 08-04-2011, 07:51 AM   #7
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 502

Original Poster
Rep: Reputation: 31
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 07:55 AM.
 
Old 08-04-2011, 07:54 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
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, 07:58 AM   #9
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 502

Original Poster
Rep: Reputation: 31
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, 08:00 AM   #10
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 502

Original Poster
Rep: Reputation: 31
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, 08:14 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
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, 08:53 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
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


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 04:37 AM
Syntax error near unexpected token jchmski Programming 5 06-18-2010 11:28 AM
sh: syntax error near unexpected token `(' venkatesh_b Linux - Newbie 1 05-16-2009 06:44 AM
syntax error for unexpected token `(' Steve Spurr Linux - Newbie 6 09-22-2006 09:19 AM
syntax error near unexpected token ` mattyspatty Programming 8 05-07-2006 06:19 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration