how to set a variable in a script and have the variable still set after script termin
The Script:
Code:
#! /bin/bash Gut: Subsequent execution of xhost fails and echo $DISPLAY outputs the empty set. So how do I make the variable persistent so that it retains its value after the script terminates? |
take out this part.
#! /bin/bash |
Quote:
|
Thanks for the response.
Did a lot of searching and finally came up with: Code:
$ source ./xwinstart.sh I don't see how to automate the execution of the export DISPLAY to discriminate against local logins and only kick off when I am logging as self across the LAN. Any help would be appreciated |
Quote:
after starting a new PuTTY session so the DISPLAY=NIL I executed the script with #! /bin/bash deleted and when the script completed, tried to do an xhost which failed because the DISPLAY variable reverted to the empty set on completion of the script. That said: source //yourversionof the script worked just as well as the version with the #! /bin/bash line Don't understand what the #! /bin/bash line is supposed to accomplish in a shell script??? |
Quote:
The problem is that each shell script is run into it's own sub-shell, and not the current one. When you call a script, a new shell is opened, and the script runs into that new shell. Once the script is over, that shell is closed as well, and the environment inside that shell (variables included) is vaporized. There's nothing at all that you could do inside the script to prevent this behavior, because the new shell is opened before your script begins to run. The only thing that will work is "sourcing" the script, either using "source <myscript>" or the equivalent form ". <myscript>", with a single dot and a blank space in front of the name of the script. Source will dump the contents of the script in the current shell, instead of spawning a new one and running the shell inside that new shell. Quote:
In short, it tells linux which shell (or interpreter) must be used to run the script. You could use awk, sed, python, perl or whatever here instead of a shell interpreter if that's what you need, depending on the language used to write your script. If no shebang is specified, then the current shell spawns a new copy of itself (whatever it is) and tries to run the script into it. That might or might not work, specially if the shell is not written using the standard bourne shell syntax (sh), which is probably the most widespread standard. |
Thanks for your treponse. Far more illuminating than what I picked up in the search.
I will use the . form as it seems to work as well as the source form above. Moving on to the script issues that you have raised, it seems that you are saying that if I am on ubuntu 7.10 which has bash then the shell scripts with #! /bin/bash iin the first line will fail if I move to another distro where there is a different shell??? Is that true? Also you seen to say that deleting that line altogether will cause the script to fail under some conditions. Is that true?? Any info on how the #! /bin/Xash functions to start the script. |
If you want to make your script portable on different systems use #!/bin/sh instead.
/bin/sh is a symlink to /bin/your_default_shell on most systems, so it will work even if you use the script on a different system.(within the Linux range) Just consider different shells (ie FreeBsd uses a C shell, if my memory helps me) may behave differently, so you may find some surprises... Ciao |
Quote:
If you don't include the shebang for bash, and bash is not the default shell, then the script can fail, or have unwanted effects. The active shell will try to run it, and if the syntax is compatible then it will run without problems. This can be handy when you are using simple scripts to do simple tasks that do no rely on any specific shell feature. *Most* of the shells are supersets of sh (the bourne shell), so, if you can write what you want using sh-only code, it will ensure that your script can run on most shells. But some of them (csh comes to my mind) are very different, and that can cause lots of troubles if you try to run a script designed for another shell under them (unless the correct shebang is present, in which case the right shell will be invoked). For now, and as a beginner, I think that it's safe enough to assume that bash will be available. Even if it's not the shell you are using, it will be installed on 99% of the linux distros by default, because many times even the init scripts depend on bash. When needed, and as instructed by the shebang, it will be called to run your script. So, usually I consider the inclussion of a correct header a good habbit that can prevent problems in the future. If a script designed for bash fails then you just know you need to install it. It's a small package in any case. In some distros, even /bin/sh is not really sh, but a symlink to /bin/bash, you can check it with ls -l /usr/bin/sh or ls -l /bin/sh. Quote:
Just to clear your doubt: as long as bash is installed, an script headed with #!/bin/bash will run without problems even if bash is not the shell you use interactively as your shell of choice. As said above, sh would be the most portable solution. As long as you are sure you stick to the bourne shell syntax and do not use any bash (or other shell) specific stuff. |
Thanks for the Help.
|
All times are GMT -5. The time now is 01:27 PM. |