On 4/25/2012 3:27 AM, lynn wrote:
Hi What am I doing wrong here?
file setstuff: #!/bin/bash DB="hello" export DB
then I chmod + x it and run ./setstuff then echo $DB
gives a blank line. From anywhere. A new terminal, a new login, root...
How do I get DB to the outside world? L x
There is no outside world. In this case what you want is to source the file instead of execute it. What that means is: In this case you want to remove the #!/bin/bash from the top of the script and add a comment at the top like: # setstuff - This is sourced by <some other script or scripts>, not executed on it's own. And just for complete correctness, remove the execute bits chmod 644 setstuff Those aren't strictly necessary but they make the file properly self-documenting. Then in your other script that wants to load these variables, or at the command line if you want to load the values in your current interactive session, you _source_ the file instead of _executing_ it. (Notice I called it a file instead of a script. Thinking the right way helps you keep track of what to do and not to do where & when, and what different files are meant for. This file IS made of executable shell syntax exactly like any other script, but the difference lies in the context it's used within. By calling it a file instead of a script, and by removing the #! line and the execute bits, you reinforce the mental cues that this is not a stand alone program of it's own, do not expect to run it, it is only a library of routines or a config file that is used by some other program.) What happens when you source a file is the current process reads the file and executes the commands as if you had typed them in. That's NOT what happens when you run a script. When you run a script, or any other program, an entirely new child process is created, and THAT process runs the commands in it's own wholly separate child environment, and then at the end, THAT process exits and is destroyed along with it's environment, and your tty is connected back to your interactive shell process which was put on hold in the background during that time. The syntax to source a file is just "dot space filename" myserver:~ $ . setstuff The comment I said to put in the file says it's only used by some other script but actually you can use this at the command line manually instead of in some other script all you want. The point was just to document clearly that this file is not a stand-alone script that does anything useful by itself. It's just a config file or a function library (or both) that is loaded and used by something else. That something else can be some other script or your interactive shell. Also just in case this wasn't obvious, sourcing a file requires that the sourced file be written in the same language as the process that will source it. If you are sitting at an interactive shell, which is /bin/bash, and want to source a file like this, then that file much be made up of valid bourne shell syntax. It can't be a python file for instance. Conversely if a python script wanted to source another file, that other file would have to contain only valid python, not bourne shell syntax. -- bkw -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org