Returning a value to a calling script


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Returning a value to a calling script
# 1  
Old 06-16-2008
Returning a value to a calling script

Hi. I'm trying to call a script named checkForFile.sh from within my main script named master.sh. In checkForFile.sh I want to set a value to the variable fileExist, but then I want to reference the value in the master.sh script. Whenever I attempt this the echo statement is just blank.

#master.sh
./checkForFile.sh
echo $fileExist

#checkForFile.sh (called from within master.sh)
fileExist=1

Is there a good way to accomplish this? What I'm trying obviously isn't working. Any help would be greatly appreciated!
# 2  
Old 06-16-2008
You can do something like:

Code:
#master.sh
fileExist=$(./checkForFile.sh)
echo $fileExist

Code:
#checkForFile.sh (called from within master.sh)
fileExist=1
echo $fileExist

Regards
# 3  
Old 06-16-2008
Hammer & Screwdriver From child back to parent

Well, you can EXPORT data to go from parent to child, but this does not help you in going from child back to parent (process).
A couple of thougts:
(a) You could utilize the return code from the child process. Instead of simply using an "exit", you could use "exit 1" (or whatever) to pass something back to the parent. Caution should be used in this application since returning any value is normally significant to there being an error in the child process.
(b) Write a temporary file, perhaps even with information in it. The child can create a work file that the parent program will read for next steps.

Personally, I like the 2nd approach as it is cleaner. However, with careful consideration, the 1st approach might also work for you.
# 4  
Old 06-16-2008
hmm...so there is no way to make the called script a sort of Function that will return a value I designate?
# 5  
Old 06-16-2008
Another way, run the script in the current shell:

Code:
#master.sh
. ./checkForFile.sh
echo $fileExist

Code:
#checkForFile.sh (called from within master.sh)
fileExist=1
export $fileExist

Regards
# 6  
Old 06-16-2008
This worked perfectly! I really appreciate the help.

Quote:
Originally Posted by Franklin52
Another way, run the script in the current shell:

Code:
#master.sh
. ./checkForFile.sh
echo $fileExist

Code:
#checkForFile.sh (called from within master.sh)
fileExist=1
export $fileExist

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Control not returning from Sqlplus to calling UNIX shell script.

Hello All, I have exactly same issue @vikas_trl had in following link: https://www.unix.com/shell-programming-and-scripting/259854-control-not-returning-sqlplus-calling-unix-shell-script.html I wonder if he or somebody else could find the issue's cause or the solution. Any help would... (4 Replies)
Discussion started by: RicardoQ
4 Replies

2. UNIX for Beginners Questions & Answers

Script not returning what I am expecting

Hi there I am trying to create a script where I am checking the process is being run by the correct user, when I go to run the script it is not returning what I am expecting and I am not 100% sure why!!! First off I have determined what the process user should be at the top of the script ... (3 Replies)
Discussion started by: simpsa27
3 Replies

3. Shell Programming and Scripting

Control not returning from Sqlplus to calling UNIX shell script.

Hello All, I have a UNIX script which will prepare anonymous oracle pl/sql block in a temporary file in run time and passes this file to sqlplus as given below. cat > $v_Input_File 2>>$v_Log << EOF BEGIN EXECUTE IMMEDIATE 'ALTER SESSION FORCE PARALLEL DML PARALLEL 16'; EXECUTE... (1 Reply)
Discussion started by: vikas_trl
1 Replies

4. UNIX for Dummies Questions & Answers

Not returning from a sub script?

My problem in brief is that I execute a script from another script and I can not pick up the return code from that script, or otherwise I am not returning from that script. I have an echo in the executed script and we get a response code of 0 and exit that script with the return code. I then try to... (1 Reply)
Discussion started by: Charles Swart
1 Replies

5. Shell Programming and Scripting

Script returning 0

hello i write a script which calculate free space but he always is 0 thats wrong with my script? getFileSystemPerformanceData() { if ; then if grep -q "Ubuntu" /etc/issue ; then CMD="df -lP | grep -v "\/home" | grep -v "\/dev/mapper/VolGroup-lv_root"" elif grep... (5 Replies)
Discussion started by: donatas1234
5 Replies

6. Shell Programming and Scripting

Returning to shell from previous script

Found myself stuck on this seemingly trivial issue. I have this script which call other shell files to do their deeds. The shell files in turn call some other programs as well. My problem is that in two of these shell files, the control doesnt return to next command in script unless the Enter key... (2 Replies)
Discussion started by: DoesntMatter
2 Replies

7. Shell Programming and Scripting

Calling script from RM cobol and returning value to cobol

Is there a way you can return a value from a script that is called from a rm cobol program... 01 WS-COMD-LINE-PGM X(39) value sh ./getUserId.sh 12345" 01 WS-RETURN-SYS-CODE PIC 9(8). CALL "SYSTEM" USING WS-COMD-LINE-PGM GIVING WS-RETURN-SYS-CODE. ... (1 Reply)
Discussion started by: pavanmp
1 Replies

8. Shell Programming and Scripting

returning to the parent shell after invoking a script within a script

Hi everybody, I have a script in which I'm invoking another script which runs in a subshell. after the script is executed I want to return to the parent shell as some variables are set. However i'm unable to return to my original shell as the script which i'm calling inside is invoked in... (5 Replies)
Discussion started by: gurukottur
5 Replies

9. Shell Programming and Scripting

Returning to begining of a script

Dear all, When i ask a question in my script if the answer is not correct i need to goback to the begning of the script and ask the question again. ex - What is your name ? saman Name is not correct ...try again What is the name ? Nimal Thank you.... (2 Replies)
Discussion started by: Nayanajith
2 Replies

10. Shell Programming and Scripting

Returning Values (shell Script)

I have an application on Informix 4GL, and I am invoking the shell script from the program, but I need to know if the shell script work fine or not, in order to continue the process. I know that we can use $? to get the final status but this is on unix command. How can I return a value from the... (3 Replies)
Discussion started by: jennifer01
3 Replies
Login or Register to Ask a Question