[opensuse] VEEERY elementary script question
Ok, I've written very few scripts in my time but I've generally gotten them to work. However, I've run into something that completely has me stumped. I have the following 2 line script (it was more than that, but I stripped it down just to see if I could get something extremely basic to work) -- MYVAR=xyz export MYVAR After the script runs, I do an echo MYVAR and it comes up empty. If I simply enter those same statements outside of a script it works fine! Could someone enlighten me as to what the heck is wrong with having that in a script? Thanks, Greg Wallace -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 05 December 2006 8:01 pm, Greg Wallace wrote:
After the script runs, I do an echo MYVAR and it comes up empty. If I simply enter those same statements outside of a script it works fine! Could someone enlighten me as to what the heck is wrong with having that in a script?
Ok, you need to "source" the script if you want all variables inside it within your current shell. Try this (assuming your script is in current directory) source ./yourScript.sh and try now. You'll see that all variables you defined within script are now part of your session. Another shortcut , instead of typing source, try this: . ./yourScript.sh (that's period space & then your script) The probem you are having is that, when you run your script a new shell is created (the one that is going to actually run your script) and then after the script ends, that shell dies and control resumes back to your shell. Check this out: http://mdobson.home.cern.ch/mdobson/onlinesw/unix-scripting.html#sec4sourcee... HTH, Jorge -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday, December 05, 2006 @ 6:11 PM, Jorge Fabregas wrote:
On Tuesday 05 December 2006 8:01 pm, Greg Wallace wrote:
After the script runs, I do an echo MYVAR and it comes up empty. If I simply enter those same statements outside of a script it works fine! Could someone enlighten me as to what the heck is wrong with having that in a script?
Ok, you need to "source" the script if you want all variables inside it within your current shell. Try this (assuming your script is in current directory)
source ./yourScript.sh
and try now. You'll see that all variables you defined within script are now part of your session. Another shortcut , instead of typing source, try this:
. ./yourScript.sh (that's period space & then your script)
The probem you are having is that, when you run your script a new shell is created (the one that is going to actually run your script) and then after the script ends, that shell dies and control resumes back to your shell.
Check this out:
http://mdobson.home.cern.ch/mdobson/onlinesw/unix-scripting.html#sec4source execute
HTH, Jorge --
Yep, that worked like a charm. Thanks, Greg Wallace -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
MYVAR=xyz export MYVAR
After the script runs, I do an echo MYVAR and it comes up empty. hi Greg, Please allow me to add a couple of tips to what Jorge has already told you, just for clarification. The export keyword works *forward*--- in other words, if you want MYVAR to be in scope for subsequent scripts then the variable needs to be exported. The export does not work in reverse. The dot command is subtle and has some side effects that you might want to keep in mind. First, as Jorge pointed out, the dot command says use the existing interactive shell instead of a new shell. This may not be what you really want, since it *ties up* the interactive shell until the script or executeable returns. Second, the dot command can be used to run (execute) a script that does not technically have execute permissions... because the dot command does not take into account whether the source file is executeable or not... it just feeds
On Tuesday 05 December 2006 18:01, Greg Wallace wrote: the lines of the source file line by line to the interactive shell to interpret. -- Kind regards, M Harris <>< -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (3)
-
Greg Wallace
-
Jorge Fábregas
-
M Harris