LinuxQuestions.org
Visit Jeremy's Blog.
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 01-10-2013, 02:47 PM   #1
pavan7
LQ Newbie
 
Registered: Jan 2013
Posts: 4

Rep: Reputation: Disabled
if else to run a shell script


Hi,

I am newbie to the forum and to scripting as well. Please help me out on this. I have written a script which checks to see if the server is down if it is down it has to exit the program, if not it should execute the scripts. Below is the sript and error I am seeing. where xyz.sh is the script I run, abc.sh and efg.sh should be excuted from with the xyz.sh script.Please help me with this. Thanks in advance:

#--------------------------------------------------------------
# checking if the server has started
#--------------------------------------------------------------

print "checking to see if server " + serverName + " is running on node " + nodeName
runningServer = AdminControl.completeObjectName("type=Server,node=" + nodeName + ",process=" + serverName + ",*")
# Following if block execute when server is down
# else block executes when server is up and running.
if (len(runningServer) == 0):
print "Error -- Server " + serverName + " not running on node " + nodeName
# return
else
'bash -c /home/abc.sh'
'bash -c /home/efg.sh'
return



Error:

Exception received while running file "/home/xyz.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
(no code object) at line 0
File "<string>", line 52
else
^
SyntaxError: invalid syntax

Last edited by pavan7; 01-10-2013 at 02:49 PM.
 
Old 01-10-2013, 03:20 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Use [CODE][/CODE] tags to wrap your code.

In bash, you don't say

Code:
if (len(runningServer) == 0):
(that looks very like python) you say

Code:
if [ ${#runningServer} -eq 0 ]; then
and you need to close the block with a "fi".

Regards,
 
Old 01-10-2013, 03:30 PM   #3
pavan7
LQ Newbie
 
Registered: Jan 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Unhappy

you are right Sorry actually its a python calling shell

so according to python this should be right aint it?

if (len(runningServer) == 0):
print "Error -- Server " + serverName + " not running on node " + nodeName
# return


So should I change this to

if (len(runningServer) == 0):
print "Error -- Server " + serverName + " not running on node " + nodeName
# return
fi
else
'bash -c /home/abc.sh'
'bash -c /home/efg.sh'
return

Still get
exception from Jython:
Traceback (innermost last):
(no code object) at line 0
File "<string>", line 53
fi
^
SyntaxError: inconsistent dedent

Sorry for my scripting skills
 
Old 01-10-2013, 03:35 PM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Like I said, use [CODE][/CODE] tags around your code in your post, so we can see the formatting better. For any language other than python, this is rather useful. As whitespace is a crucial part of python syntax, we cannot help you unless you put [CODE][/CODE] tags around your code. For example:

[CODE]your code in here[/CODE] produces
Code:
your code in here
If you're writing in python, then you need:

Code:
if (len(runningServer) == 0):
    print "Error -- Server " + serverName + " not running on node " + nodeName
else:
    'bash -c /home/abc.sh'
    'bash -c /home/efg.sh'
return
or something similar - I'm not sure exactly what logic you need to code to follow.
 
Old 01-10-2013, 03:42 PM   #5
pavan7
LQ Newbie
 
Registered: Jan 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
Like I said, use [CODE][/CODE] tags around your code in your post, so we can see the formatting better. For any language other than python, this is rather useful. As whitespace is a crucial part of python syntax, we cannot help you unless you put [CODE][/CODE] tags around your code. For example:

[CODE]your code in here[/CODE] produces
Code:
your code in here
If you're writing in python, then you need:

Code:
if (len(runningServer) == 0):
    print "Error -- Server " + serverName + " not running on node " + nodeName
else:
    'bash -c /home/abc.sh'
    'bash -c /home/efg.sh'
return
or something similar - I'm not sure exactly what logic you need to code to follow.
here it is:

Code:
   #--------------------------------------------------------------
   # checking if the server has started
   #--------------------------------------------------------------

   print "checking to see if server " + serverName + " is  running on node " + nodeName
   runningServer = AdminControl.completeObjectName("type=Server,node=" + nodeName + ",process=" + serverName + ",*")
   # Following if block execute to check if the server is down
   # else block executes when server is up and running.
   if (len(runningServer) == 0):
      print "Error -- Server " + serverName + " not running on node " + nodeName
else:
         'bash -c  /home/abc.sh'
          'bash -c  /home/efg.sh'
      return
 
Old 01-10-2013, 04:22 PM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Right, in Python you need to line your code up correctly:

Code:
   if (len(runningServer) == 0):
      print "Error -- Server " + serverName + " not running on node " + nodeName
   else:            #this 'else' needs to line up with the 'if' line 
                    #(i.e. the same number of spaces at the start of both)
         'bash -c  /home/abc.sh'
         'bash -c  /home/efg.sh' #this line needs to line up with the line before
   return
Ideally, you would also make the 'print' and 'bash' lines line up, but if you don't it won't stop your code from running.
 
Old 01-14-2013, 02:32 PM   #7
pavan7
LQ Newbie
 
Registered: Jan 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for responding the script seems not to throw any error but somehow its skipping this part of the code... I am working on it
Quote:
Originally Posted by Snark1994 View Post
Right, in Python you need to line your code up correctly:

Code:
   if (len(runningServer) == 0):
      print "Error -- Server " + serverName + " not running on node " + nodeName
   else:            #this 'else' needs to line up with the 'if' line 
                    #(i.e. the same number of spaces at the start of both)
         'bash -c  /home/abc.sh'
         'bash -c  /home/efg.sh' #this line needs to line up with the line before
   return
Ideally, you would also make the 'print' and 'bash' lines line up, but if you don't it won't stop your code from running.
 
Old 01-14-2013, 10:05 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by pavan7 View Post
Thanks for responding the script seems not to throw any error but somehow its skipping this part of the code... I am working on it
It's not being skipped, putting the 'bash whatever' just doesn't actually do anything. Take a look at the subprocess module.
 
  


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
Shell script for run an shell script on server using ssh bloodstreetboy Linux - Server 5 01-12-2013 03:23 AM
[SOLVED] run a shelll script from another shell script in another directory raulab Linux - Newbie 12 02-22-2011 05:18 PM
in bash shell how to run shell script during startup rammohan04 Red Hat 2 07-31-2009 02:07 AM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 09:56 AM
Restrict a Shell Script to run from a shell bharaniks Linux - Security 7 08-26-2007 10:57 PM

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

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