LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to set a variable in a script and have the variable still set after script termin (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-set-a-variable-in-a-script-and-have-the-variable-still-set-after-script-termin-678071/)

john test 10-21-2008 10:43 AM

how to set a variable in a script and have the variable still set after script termin
 
The Script:
Code:

#! /bin/bash
echo
export DISPLAY=192.168.1.100:0.0
xhost
echo $DISPLAY

Works well and displays the proper xhost output and echos the exported $DISPLAY value
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?

whansard 10-21-2008 12:08 PM

take out this part.

#! /bin/bash

linux-qa 10-21-2008 12:44 PM

Quote:

Originally Posted by john test (Post 3317683)
The Script:
Code:

#! /bin/bash
echo
export DISPLAY=192.168.1.100:0.0
xhost
echo $DISPLAY

Works well and displays the proper xhost output and echos the exported $DISPLAY value
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?

These values are kept in memory and they get erased as soon as the script completes or you exit from shell. In order to make these values permanent, you need to put them under the shell configuration files like .profile, .bashrc or .bash_profile.

john test 10-21-2008 01:05 PM

Thanks for the response.
Did a lot of searching and finally came up with:
Code:

$ source ./xwinstart.sh
which makes the exported DISPLAY variable persistent for the duration of the SSH/PuTTY session.
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

john test 10-21-2008 01:16 PM

Quote:

Originally Posted by whansard (Post 3317777)
take out this part.

#! /bin/bash

Went back and gave that a shot -- Didn't do the trick.
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???

i92guboj 10-21-2008 01:33 PM

Quote:

Originally Posted by john test (Post 3317845)
Went back and gave that a shot -- Didn't do the trick.
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

There's no reason why that would fix your problem.

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:

Don't understand what the #! /bin/bash line is supposed to accomplish in a shell script???
http://en.wikipedia.org/wiki/Shebang_(Unix)

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.

john test 10-21-2008 03:54 PM

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.

urka58 10-21-2008 04:43 PM

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

i92guboj 10-21-2008 05:00 PM

Quote:

Originally Posted by john test (Post 3317998)
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?

If bash is not installed, yes. It will fail. And it is the way is MUST be. After all, you can't run a bash script if you don't have bash installed, just like you can't run java programs if you haven't a java virtual machine installed (I hope that the comparison makes any sense for you).

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:

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 the needed shell doesn't exist, the script will abort with an error, which is a good thing because it will prevent your script from doing weird things. You can test yourself. Just create a file with just one line, containing "#!/bin/non_existant_shell", save it, chmod u+x it, and try to run it. It will abort telling you that the required interpreter has not been found. The same would happen for bash if it was not found.

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.

john test 10-21-2008 05:21 PM

Thanks for the Help.


All times are GMT -5. The time now is 01:27 PM.