LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-21-2008, 11:43 AM   #1
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
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?
 
Old 10-21-2008, 01:08 PM   #2
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix, 98,2000 + various
Posts: 3,171

Rep: Reputation: 52
take out this part.

#! /bin/bash
 
Old 10-21-2008, 01:44 PM   #3
linux-qa
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Rep: Reputation: 0
Quote:
Originally Posted by john test View Post
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.
 
Old 10-21-2008, 02:05 PM   #4
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Original Poster
Rep: Reputation: 35
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
 
Old 10-21-2008, 02:16 PM   #5
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Original Poster
Rep: Reputation: 35
Quote:
Originally Posted by whansard View Post
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???
 
Old 10-21-2008, 02:33 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,043

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Quote:
Originally Posted by john test View Post
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.

Last edited by i92guboj; 10-21-2008 at 02:39 PM.
 
Old 10-21-2008, 04:54 PM   #7
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Original Poster
Rep: Reputation: 35
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.
 
Old 10-21-2008, 05:43 PM   #8
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 14
Posts: 535

Rep: Reputation: 30
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
 
Old 10-21-2008, 06:00 PM   #9
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,043

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Quote:
Originally Posted by john test View Post
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.

Last edited by i92guboj; 10-21-2008 at 06:05 PM.
 
Old 10-21-2008, 06:21 PM   #10
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Original Poster
Rep: Reputation: 35
Thanks for the Help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 03:40 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 06:21 PM
Windows batch script: SET command as variable Dr_Death_UAE Programming 10 09-09-2007 08:18 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 12:23 PM
how do you set an env variable from within a csh script? BrianK Linux - General 3 05-26-2004 03:16 PM


All times are GMT -5. The time now is 09:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration