LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-10-2011, 12:33 AM   #1
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Rep: Reputation: Disabled
How to find out the output of a script is success or not in another script


Hi All,

I have a script file say myscript.sh
i am calling some other scripts in myscript.sh
i need to find out that if the called script is executed successfully or not. according to that i have to check some conditions in myscrpit.sh

i tried "if [ $? = 0 ]; then" but it is not working properly.
please help.
One more thing iam calling more than one scripts in the myscript.sh file. and for every called script i need to check some conditions according to the output is success or not.
in that case can we use "$?" ?


The calling scripts are for starting some other applications like tomcat etc.
can this be done?
Please let me know if the question is confusing.

Last edited by jhonnappier2007; 11-10-2011 at 12:51 AM.
 
Old 11-10-2011, 12:35 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
You want "$?" not "#?".

Cheers,

Evo2.
 
Old 11-10-2011, 12:40 AM   #3
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
sorry thats "$?"
my mistake

Question edited and corrected

Last edited by jhonnappier2007; 11-10-2011 at 12:51 AM.
 
Old 11-10-2011, 12:53 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Ok, then you need to tell us you mean by "not working properly".

Perhaps you could post the shortest possible script (inside [code] tags) that demonstrates your problem (ie it should not be more than about 5 lines).

Evo2.
 
Old 11-10-2011, 01:16 AM   #5
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
I worked out the script, but it is longer than u mentioned so iam unable to post it.
Any way is it possible to check conditions with $? more than once?
i mean how much time will the data in the exit status variable holds?
 
Old 11-10-2011, 01:23 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
$? simply returns the exit code of the previously-executed program. This has two caveats:

1) You MUST call $? immediately after the program you want to test. Now this doesn't mean you have to throw it into your "if" statement immediately, you can save it in a variable instead (eg: status=$?) and then run your test on the variable later on, but you must save the output of $? the very next line after the routine you want to test. If you run anything else (even an 'ls'), $? will contain the exit code of that program instead of the one you're interested in.

2) Since $? is just returning the exit code of the last-executed program, this will only actually do anything if the program you're testing uses exit codes. If the program doesn't use exit codes to identify a successful completion vs error, the calling routing won't be able to use the exit code to do anything.

Last edited by suicidaleggroll; 11-10-2011 at 01:26 AM. Reason: typo
 
1 members found this post helpful.
Old 11-10-2011, 01:37 AM   #7
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thank you for the answer
That helped a lot
cheers!!
 
  


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
Standard script output to screen & -x option output to file BoBeau236 Programming 6 05-10-2011 08:43 AM
Make a script that uses the output from another script as the variables? Oakems Programming 11 06-18-2009 06:21 PM
[SOLVED] Bash script - how to use output from diff and find in context with cpio Mogget Programming 4 01-23-2009 11:38 AM
Correct output from script appears only when script is run interactively kaplan71 Linux - Software 2 01-15-2009 12:47 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 07:25 PM


All times are GMT -5. The time now is 12:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration