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! |
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 |
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. |
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. |
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 __ The most apparent is using a temporary file, and next would be using a named pipe. Using a temporary file Code:
TEMPFILE=$(mktemp) Code:
NAMED_PIPE=$(mktemp -u) Code:
echo "I was called." > "$NAMED_PIPE" |
Quote:
Code:
val=$(script2) |
suicidaleggroll, he said he can't echo the variable from within the calling script.
Also, you don't have to quote my whole post :) |
Quote:
|
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. |
Quote:
|
Quote:
Code:
( source script.sh ) Code:
export TEMPFILE Code:
export NAMED_PIPE |
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 . |
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. |
$? 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 Code:
# calling script |
Quote:
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. |