LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to pass a value back to my calling script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-a-value-back-to-my-calling-script-4175428864/)

ButterSideUp 09-24-2012 07:19 PM

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!

ButterSideUp 09-24-2012 07:29 PM

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

suicidaleggroll 09-24-2012 07:34 PM

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.

ButterSideUp 09-24-2012 07:44 PM

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.

konsolebox 09-24-2012 07:51 PM

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"

suicidaleggroll 09-24-2012 08:00 PM

Quote:

Originally Posted by konsolebox (Post 4788441)
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 $?

konsolebox 09-24-2012 08:02 PM

suicidaleggroll, he said he can't echo the variable from within the calling script.

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

suicidaleggroll 09-24-2012 08:05 PM

Quote:

Originally Posted by konsolebox (Post 4788446)
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.

ButterSideUp 09-24-2012 08:07 PM

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.

konsolebox 09-24-2012 08:08 PM

Quote:

Originally Posted by suicidaleggroll (Post 4788445)
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.

konsolebox 09-24-2012 08:12 PM

Quote:

Originally Posted by ButterSideUp (Post 4788449)
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


konsolebox 09-24-2012 08:14 PM

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


ButterSideUp 09-24-2012 08:17 PM

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.

konsolebox 09-24-2012 08:25 PM

$? 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=$?


suicidaleggroll 09-24-2012 08:26 PM

Quote:

Originally Posted by ButterSideUp (Post 4788455)
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...


All times are GMT -5. The time now is 05:45 PM.