Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I want to source a file eg name.sh from another script and check the return code and decide on it's error code. If I use

source name.sh

then if name.sh return non-zero return code my main script stop to run but I need to decide on exit code weather to continue or stop.

If I use

ret_code="`source name.sh`"
echo $ret_code

then ret_code is null and no error code is printed. I can't use these:

sh name.sh
./name.sh
bash name.sh

because name.sh is not executable and I don't want it to be executable

share|improve this question
    
Why not enclosing the executable code of "name.sh" in a function ? – michaelmeyer May 21 '13 at 11:49
    
@doukremt because my script should run a serial of other scripts in a directory – amin May 21 '13 at 11:55
1  
The backticks (``) spawn an subordinate shell which then executes source name.sh; this is equivalent to bash name.sh (assuming $SHELL is bash). – msw May 21 '13 at 11:56
up vote 6 down vote accepted

File does not need to be executable to run sh name.sh. Than use $?.

sh name.sh
ret_code=$?
share|improve this answer

The return code should be in the variable $?. You can do the following:

source name.sh         # Execute shell script
ret_code=$?            # Capture return code
echo $ret_code
share|improve this answer
    
if i use source name.sh if name.sh contain exit 1 the code stop to continue – amin May 21 '13 at 11:57
1  
The purpose of using source is to run the script within the current shell. Hence, the exit will stop the current shell as well. Your original question had an explicit requirement to use source and I assumed that you know the consequences. – unxnut May 21 '13 at 12:14
    
I have mentioned that if i use source what problem happen but how ever thank for your help – amin May 21 '13 at 12:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.