LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-24-2012, 08:19 PM   #1
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Rep: Reputation: Disabled
How to pass a value back to my calling script


Hi there,

My head has finally melted, so I've thrown in the towel again to ask you intelligent folk.

In my second script i set a variable to send back to the calling script, bit i can't echo the variable from within the calling script.

I've searched a whole pile of options, but none seem to work.

Help please!
 
Old 09-24-2012, 08:29 PM   #2
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Original Poster
Rep: Reputation: Disabled
One of the suggestions i came across was this, but no joy:


echo -e "\n%%%%%%%% The /etc/fstab file has been successfully updated !!! %%%%%%%%%%%\n"

if [ $FILE_SYSTEM -eq 2 ] ; then OUTPUT=$(FILE_SYSTEM)

echo $OUTPUT

fi

exit 0
 
Old 09-24-2012, 08:34 PM   #3
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
I really don't know what you're trying to do in that example. What is FILE_SYSTEM? Where is this variable being set? Your "OUTPUT=$(FILE_SYSTEM)" isn't going to work because that syntax is trying to execute the command FILE_SYSTEM and store the output in the variable OUTPUT. It's as if you were sitting on the terminal and just wrote "FILE_SYSTEM" and pressed enter...nothing would happen other than the shell telling you FILE_SYSTEM is not a valid command. What are you trying to do here?

Also, all caps is typically reserved for environment variables. You should use lower or mixed upper/lower case for local variables inside a script to avoid confusion.

Last edited by suicidaleggroll; 09-24-2012 at 08:36 PM.
 
Old 09-24-2012, 08:44 PM   #4
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thanks,

Earlier in my second script i set the FILE_SYSTEM variable:

elif [ "$response" == "2" ] ; then FILE_SYSTEM="2"

I just need to get this value: 2 back to my calling script.

Last edited by ButterSideUp; 09-24-2012 at 08:47 PM.
 
Old 09-24-2012, 08:51 PM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
If the called script is separate from the calling script's environment there's no way you could pass the variable directly. That is something like:
Code:
bash script.sh # returns a value to a global variable like __
RETURNED_VALUE=$__
But if not, another way would be to use a temporary data bank somewhere like a database, a file, key-value store, etc.
The most apparent is using a temporary file, and next would be using a named pipe.

Using a temporary file
Code:
TEMPFILE=$(mktemp)
: > "$TEMPFILE" # optionally truncate the file
call script.sh # script.sh inherits value of "$TEMPFILE" and uses it to store the result
read RETURNED_VALUE < "$TEMPFILE" || RETURNED_VALUE=$(<"$TEMPFILE") # just pick one of the two methods
Another way, is to use a named pipe.
Code:
NAMED_PIPE=$(mktemp -u)
mkfifo "$NAMED_PIPE"
exec 4< "$NAMED_PIPE" & # open named pipe with file descriptor 4 for input
read -u 4 RETURNED_VALUE
exec 4<&- # close the pipe
Called script:
Code:
echo "I was called." > "$NAMED_PIPE"

Last edited by konsolebox; 09-24-2012 at 09:32 PM. Reason: I'm not sure if a subprocess is used for opening the pipe, so I removed the comment.
 
Old 09-24-2012, 09:00 PM   #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
Quote:
Originally Posted by konsolebox View Post
If the called script is separate from the calling script's environment there's no way you could pass the variable directly. That is something like:
Code:
bash script.sh # returns a value to a global variable like __
RETURNED_VALUE=$__
But if not, another way would be to use a temporary data bank somewhere like a database, a file, key-value store, etc.
The most apparent is using a temporary file, and next would be using a named pipe.

Using a temporary file
Code:
TEMPFILE=$(mktemp)
: > "$TEMPFILE" # optionally truncate the file
call script.sh # script.sh inherits value of "$TEMPFILE" and uses it to store the result
read RETURNED_VALUE < "$TEMPFILE" || RETURNED_VALUE=$(<"$TEMPFILE") # just pick one of the two methods
Another way, is to use a named pipe.
Code:
NAMED_PIPE=$(mktemp -u)
mkfifo "$NAMED_PIPE"
exec 4 < "$NAMED_PIPE" & # open named pipe with file descriptor 4 for input with a sub-process in the background
call script.sh # call script.sh
read -u 4 RETURNED_VALUE
exec 4<&- # close the pipe
Called script:
Code:
echo "I was called." > "$NAMED_PIPE"
Or he could echo the output and capture it from the caller as:
Code:
val=$(script2)
Or, depending on what kind of values will be returned, he could pass it back using the exit code and capture it in the caller with $?

Last edited by suicidaleggroll; 09-24-2012 at 09:01 PM.
 
Old 09-24-2012, 09:02 PM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
suicidaleggroll, he said he can't echo the variable from within the calling script.

Also, you don't have to quote my whole post

Last edited by konsolebox; 09-24-2012 at 09:03 PM.
 
Old 09-24-2012, 09:05 PM   #8
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
Quote:
Originally Posted by konsolebox View Post
suicidaleggroll, he said he can't echo the variable from within the calling script.
I interpreted that comment as that he hasn't figured out how to echo the variable, not that he isn't allowed to echo the variable. In fact he's even trying to echo it in his example, it just isn't working because it's not being set properly. But maybe I misread.

Last edited by suicidaleggroll; 09-24-2012 at 09:06 PM.
 
Old 09-24-2012, 09:07 PM   #9
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Original Poster
Rep: Reputation: Disabled
OK i'll give that a try - thanks!

Do i need to set a variable in my called script for the tmp_file or named_pipe options.
 
Old 09-24-2012, 09:08 PM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by suicidaleggroll View Post
Or, depending on what kind of values will be returned, he could pass it back using the exit code and capture it in the caller with $?
Yeah, but only if the expected values are only from 0 to 255.
 
Old 09-24-2012, 09:12 PM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by ButterSideUp View Post
Do i need to set a variable in my called script for the tmp_file or named_pipe options.
I'm not sure what options you refer to but, those variables are inherited by its child processes. Actually, when calling it, the best way would be like this:
Code:
( source script.sh )
But sometimes you could just export the needed variables
Code:
export TEMPFILE
bash script.sh
or
Code:
export NAMED_PIPE
bash script.sh
 
Old 09-24-2012, 09:14 PM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Come to think of it, you did consider that you could "source" your subscript right? And that you just decided to run it as a separate process?

--- Add ---

Because if you source or ., your sub-script would share same environment with your calling script, so no need to use other mediums, just use a variable.

The only thing is that if the script crashes, the caller crashes as well, and unless the called script handles the variables well, the caller would mess-up its own variables too.

For more info about it you could run:
Code:
help .
help source

Last edited by konsolebox; 09-24-2012 at 09:17 PM.
 
Old 09-24-2012, 09:17 PM   #13
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Original Poster
Rep: Reputation: Disabled
I kinda know what's going on here, but have only been scripting for a couple of weeks.

I can use $? when i'm in one script, how does this work with two??

Do you set $? in the second script then reference it from the first file.

If possible a basic explanation would be helpful.
 
Old 09-24-2012, 09:25 PM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
$? is actually used to get the exit code of the called process or child process. But some could use it to return an integer value to the caller. The value could only be from 0 to 255

The basic way of using it is:
Code:
# within script.sh
exit "$N" # where $N is a number from 0 to 255
Code:
# calling script
bash script.sh # or ( . script.sh ), or ( source script.sh ), or </path/to|.>/script.sh (if executable)
RETURNED_VALUE=$?
 
Old 09-24-2012, 09:26 PM   #15
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
Quote:
Originally Posted by ButterSideUp View Post
I kinda know what's going on here, but have only been scripting for a couple of weeks.

I can use $? when i'm in one script, how does this work with two??

Do you set $? in the second script then reference it from the first file.

If possible a basic explanation would be helpful.
$? grabs the exit code of the previously-executed process

You can set the exit code in a script with "exit x", as in "exit 0", "exit 2", etc.


edit: too slow...
 
  


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
[SOLVED] How do I get expect to send a response back that my calling program can use? cgeekwannabe Linux - General 5 08-21-2012 03:32 AM
how to pass MySQL user/pass securely in shell script? digity Linux - Newbie 5 01-07-2010 06:48 AM
g++ calling the deconstructor, and not the constructor in pass by value functions pilotmm Programming 1 07-21-2009 08:00 PM
how to pass a variable from a called script back to the calling script steven.c.banks Linux - General 2 05-05-2008 03:00 PM
wine doesn't pass arguments correctly when calling programs jonchapman Linux - Software 0 10-08-2003 02:56 PM


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