LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
LinkBack Search this Thread
Old 01-22-2003, 10:26 PM   #1
raypen
Member
 
Registered: Jun 2002
Location: Midwest
Distribution: Slackware
Posts: 355

Rep: Reputation: 30
Bash script turmoil


A small script which sets an environment variable will not work.

#!/bin/sh
NPX_PLUGIN_PATH=/usr/local/Acrobat5/Browsers/intellinux
export NPX_PLUGIN_PATH

I can surround the [/usr/local/Acrobat5/Browsers/intellinux] with
single quotes or double quotes and still nothing.

I can enter the commands individually from the command line
and the variable appears in the environment but, despite what
I do, the script will not work.

I'm rather new to bash scripting but the above seems simple
enough and should work. Before you ask, there is a symlink
to bash in /bin (i.e. sh -> bash). Running as root.

Is there something different in Slackware that could be causing
the problem or is it something very simple that I am missing or
just unaware of?
 
Old 01-23-2003, 05:30 AM   #2
Excalibur
Senior Member
 
Registered: Jun 2002
Location: Northern VA, USA
Distribution: Ubuntu
Posts: 1,180

Rep: Reputation: 45
I would think the script is executing and performing as desired. However, after the script terminates the setting will be lost because the script terminated. The value will be stored in the environment for the script file and all child processes, but not the environment for the parent process I think as you desire.

Consider adding your setting in the /etc/profile script or create a .bashrc script in the users home directory. If the setting is for a particular program, edit or create a shell wrapper script to execute the setting and then call the program from within the script.

Hope it helps.
 
Old 01-23-2003, 09:41 PM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,822

Rep: Reputation: 120Reputation: 120
Try source(ing) the script to cause it to exeute in the current shell.
Code:
. ./script.sh
You must have a . [space] then command. The env variable will then show up in the current shell.
 
Old 01-25-2003, 09:43 AM   #4
raypen
Member
 
Registered: Jun 2002
Location: Midwest
Distribution: Slackware
Posts: 355

Original Poster
Rep: Reputation: 30
I suspected that running the script might cause the variable to go 'out of scope'
after the script finished, but that's what the 'export' function is for isn't it? To set
the environment variable permanently (at least for the current session).

Using . ./script.sh results in an 'env' printout in which the exported variable appears
but upon entering 'env' again, the variable is missing.

Some of the startup script files like profile and the various rc.x's contain execution
instructions that use the space convention like:

. /somedirectory/somefile

but using something like this while you are in the current shell gives the result
described above, i.e. the env variable is not really set.

Where do you want to go from here?
 
Old 01-25-2003, 10:24 AM   #5
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,822

Rep: Reputation: 120Reputation: 120
I'm not sure what your problem is. You may have to post an example. These are the results I see:

dude.sh
Code:
#!/bin/sh

export DUDE=Hello
bash shell
Code:
# set | grep DUDE
# . ./dude.sh
# set | grep DUDE
DUDE=Hello
#
Works as I would expect. Now if you exited out of this shell, you would loose the variable.
 
Old 01-26-2003, 10:45 PM   #6
raypen
Member
 
Registered: Jun 2002
Location: Midwest
Distribution: Slackware
Posts: 355

Original Poster
Rep: Reputation: 30
I suppose the upshot of all this is why can't I create
a script which will export an environmental in the
same fashion as if I had typed the instructions at
the terminal.

For instance, if I type at the terminal:

$ NPX_PLUGIN_PATH=/usr/local/Acrobat5/Browsers/intellinux[enter]

$ export NPX_PLUGIN_PATH[enter]

and then enter 'env' [enter]

I see the variable in the 'env' printout, but if I use the script

#!/bin/sh
NPX_PLUGIN_PATH=/usr/local/Acrobat5/Browsers/intellinux
export NPX_PLUGIN_PATH

call it DUDE.sh and enter on the command line . ./DUDE.sh

the variable is not set.

That's as clear as I can describe it!
 
Old 01-26-2003, 10:52 PM   #7
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,822

Rep: Reputation: 120Reputation: 120
post the output of:
Code:
set | grep SHELL
 
Old 01-27-2003, 10:23 PM   #8
raypen
Member
 
Registered: Jun 2002
Location: Midwest
Distribution: Slackware
Posts: 355

Original Poster
Rep: Reputation: 30
The output of set | grep SHELL is:

SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:histexpand:monitor:ignoreeof:interactive-comments:emacs
 
Old 01-27-2003, 11:46 PM   #9
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 46
Quote:
Originally posted by raypen
I suppose the upshot of all this is why can't I create
a script which will export an environmental in the
same fashion as if I had typed the instructions at
the terminal.

For instance, if I type at the terminal:

$ NPX_PLUGIN_PATH=/usr/local/Acrobat5/Browsers/intellinux[enter]

$ export NPX_PLUGIN_PATH[enter]

and then enter 'env' [enter]

I see the variable in the 'env' printout, but if I use the script

#!/bin/sh
NPX_PLUGIN_PATH=/usr/local/Acrobat5/Browsers/intellinux
export NPX_PLUGIN_PATH

call it DUDE.sh and enter on the command line . ./DUDE.sh

the variable is not set.

That's as clear as I can describe it!
The reason is because when you place the "#!/bin/sh" at the top
of your script, you are explicitly saying you would like to start up
a NEW shell, one that will run until the end of this file. It's just
as if you had started a new xterm (or whatever you are using).
The variables you've defined in your old xterm will not be carried
over to the new one. They know nothing about each other. If
you want something to be kept across different shells, you need
to set it up in your shell startup (.cshrc, .bashrc, etc.).
To get what you want, you need to "source" the file. That is,
you need to tell your current shell that you want to run the
commands in the file as if you had just typed them all in at the
prompt.
example (in a file):
Code:
#!/bin/bash
export FOO=bar
Code:
bash-2.05a$ ./foobar 
bash-2.05a$ printenv FOO
bash-2.05a$ . foobar 
bash-2.05a$ printenv FOO
bar
bash-2.05a$ unset FOO
bash-2.05a$ printenv FOO
bash-2.05a$ source foobar 
bash-2.05a$ printenv FOO
bar
bash-2.05a$
The "./foobar" notation means to run this as a shell executable,
and the first line indicates that it is, in fact a shell executable, so
it starts up a new shell (/bin/bash), and then runs the rest of the
file.
The ". foobar" and "source foobar" notation tell the current shell
to just run the commands as if you had typed them in. The
reason the #!/bin/bash doesn't start up a new shell is because it
is recognized as a comment by the shell. In the "./foobar"
notation, it's not a comment, it's a special sequence of
characters that indicate that this file can be treated as a shell
executable, IF it's called accordingly.
 
Old 01-28-2003, 11:54 PM   #10
raypen
Member
 
Registered: Jun 2002
Location: Midwest
Distribution: Slackware
Posts: 355

Original Poster
Rep: Reputation: 30
Thank you! I knew there must be some way to accomplish this
with a script. As it turns out the notation

. ./Script.sh does not work as advertised but using either

. Script.sh or source Script.sh work as you describe. I had actually
tried the . Script.sh before but I guess I did something wrong because
it did not work correctly.

I have been through several bash scripting tutorials and have
never seen these methods described. Maybe this is not all
that obscure but I was certainly unaware of the distinction.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script Linh Programming 4 04-21-2004 05:19 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script brian0918 Programming 7 06-12-2003 06:06 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM


All times are GMT -5. The time now is 10:36 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