Technology Questions

Go Back   Technology Questions > Software Questions > Operating System Questions > Linux

Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old 09-30-2007, 08:40 PM
asdf
Newsgroup Contributor
 
Posts: n/a
calling one script from another

lets say i have one script which initializes system variables.
So it looks something like this:

#!/bin/bash
VAR1="etc/dir1"
export VAR1
VAR2="etc/dir2"
export VAR2


I want to call this script from another script and initialize variables this
way.
Right now I'm trying this.

#!/bin/bash
cd to the directory of the first script
source ./script1


After I check variables by running ENV command I don't see them
so something is obviously not working.

Variables do get set if i simply run ./script one from the command line
though.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

 
Old 09-30-2007, 08:40 PM
  #2 (permalink)  
Old 09-30-2007, 11:10 PM
Unruh
Newsgroup Contributor
 
Posts: n/a
Re: calling one script from another

"asdf" <asdf@asdf.com> writes:

>lets say i have one script which initializes system variables.
>So it looks something like this:


>#!/bin/bash
>VAR1="etc/dir1"
>export VAR1
>VAR2="etc/dir2"
>export VAR2



>I want to call this script from another script and initialize variables this
>way.


You cannot do so. variables are not inherited by parents.
You can source a file in which the variables are defined however.

source firstfile
in the second.

(See examples in /etc/init.d)


>Right now I'm trying this.


>#!/bin/bash
>cd to the directory of the first script
>source ./script1



>After I check variables by running ENV command I don't see them
>so something is obviously not working.


Sure they are. Try
echo $VAR1
in that second script. Note that these variable will NOT be defined in the
parent (eg console)


>Variables do get set if i simply run ./script one from the command line
>though.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #3 (permalink)  
Old 10-01-2007, 05:10 PM
asdf
Newsgroup Contributor
 
Posts: n/a
Re: calling one script from another


"Unruh" <unruh-spam@physics.ubc.ca> wrote in message
news:dX%Li.35280$nO3.9507@edtnps90...
> "asdf" <asdf@asdf.com> writes:
>
>>lets say i have one script which initializes system variables.
>>So it looks something like this:

>
>>#!/bin/bash
>>VAR1="etc/dir1"
>>export VAR1
>>VAR2="etc/dir2"
>>export VAR2

>
>
>>I want to call this script from another script and initialize variables
>>this
>>way.

>
> You cannot do so. variables are not inherited by parents.
> You can source a file in which the variables are defined however.
>
> source firstfile
> in the second.
>
> (See examples in /etc/init.d)
>
>
>>Right now I'm trying this.

>
>>#!/bin/bash
>>cd to the directory of the first script
>>source ./script1

>
>
>>After I check variables by running ENV command I don't see them
>>so something is obviously not working.

>
> Sure they are. Try
> echo $VAR1
> in that second script. Note that these variable will NOT be defined in the
> parent (eg console)


so how do I define them within the script so that they are retained in the
console?

thank you
>
>
>>Variables do get set if i simply run ./script one from the command line
>>though.

>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #4 (permalink)  
Old 10-01-2007, 10:00 PM
Unruh
Newsgroup Contributor
 
Posts: n/a
Re: calling one script from another

"asdf" <asdf@asdf.com> writes:


>"Unruh" <unruh-spam@physics.ubc.ca> wrote in message
>news:dX%Li.35280$nO3.9507@edtnps90...
>> "asdf" <asdf@asdf.com> writes:
>>
>>>lets say i have one script which initializes system variables.
>>>So it looks something like this:

>>
>>>#!/bin/bash
>>>VAR1="etc/dir1"
>>>export VAR1
>>>VAR2="etc/dir2"
>>>export VAR2

>>
>>
>>>I want to call this script from another script and initialize variables
>>>this
>>>way.

>>
>> You cannot do so. variables are not inherited by parents.
>> You can source a file in which the variables are defined however.
>>
>> source firstfile
>> in the second.
>>
>> (See examples in /etc/init.d)
>>
>>
>>>Right now I'm trying this.

>>
>>>#!/bin/bash
>>>cd to the directory of the first script
>>>source ./script1

>>
>>
>>>After I check variables by running ENV command I don't see them
>>>so something is obviously not working.

>>
>> Sure they are. Try
>> echo $VAR1
>> in that second script. Note that these variable will NOT be defined in the
>> parent (eg console)


>so how do I define them within the script so that they are retained in the
>console?


Teh only way is to source the script.


>thank you
>>
>>
>>>Variables do get set if i simply run ./script one from the command line
>>>though.

>>
>>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #5 (permalink)  
Old 10-02-2007, 07:40 PM
s. keeling
Newsgroup Contributor
 
Posts: n/a
Re: calling one script from another

asdf <asdf@asdf.com>:
> lets say i have one script which initializes system variables.
> So it looks something like this:
>
> #!/bin/bash
> VAR1="etc/dir1"
> export VAR1
> VAR2="etc/dir2"
> export VAR2
>
> I want to call this script from another script and initialize
> variables this way. Right now I'm trying this.
>
> #!/bin/bash
> cd to the directory of the first script
> source ./script1


That should be fine, and script1 should find them set.

> After I check variables by running ENV command I don't see them
> so something is obviously not working.


You won't see them in your current environment. Their parent-->child
relationship has nothing to do with your environment. Feature! :-)

> Variables do get set if i simply run ./script one from the command
> line though.


Again, correct behaviour. Now they're defined in your own shell's
environment. "set" shows you. Write a shell script that says:

#!/bin/bash
#
# yada.
#

env > /tmp/env.txt

exit 0

Now run ./script, then the above.

That'll show that child process inherited its parent's process
environment, without having to run ./script itself. Env. vars are not
global until you export them, and exporting them only affects child
processes. ... of the shell which exports them.

It gets a bit fuzzier when you're at the command line affecting your
existing environment, but there are no children involved there, so
inheritance rules don't apply.


--
Any technology distinguishable from magic is insufficiently advanced.
(*) http://blinkynet.net/comp/uip5.html Linux Counter #80292
- - http://www.faqs.org/rfcs/rfc1855.html Please, don't Cc: me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #6 (permalink)  
Old 10-02-2007, 10:10 PM
Joseph H. Rosevear
Newsgroup Contributor
 
Posts: n/a
Re: calling one script from another

asdf <asdf@asdf.com> wrote:
> lets say i have one script which initializes system variables.
> So it looks something like this:


This is script1:

> #!/bin/bash
> VAR1="etc/dir1"
> export VAR1
> VAR2="etc/dir2"
> export VAR2



> I want to call this script from another script and initialize variables this
> way.
> Right now I'm trying this.


This is script2:

> #!/bin/bash
> cd to the directory of the first script
> source ./script1


Do this at the command line:

source ./script2

and it will work. You will get a changed environment at the console.
The other poster was confused.

The source command causes a script to run in the current environment
instead of in its own shell. Because you use two scripts, you need two
instances of source.

-Joe

[The remainder of asdf's posting has been omitted.]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #7 (permalink)  
Old 10-03-2007, 05:10 PM
asdf
Newsgroup Contributor
 
Posts: n/a
Re: Ok I think I almost figured it out--almost

so I configured chkconfig to start sybstart server on levels 345

and it shows correctly in rc5.d and also starts up correctly

when i do 'service sybstart start'



As a way to error check I also modified script to output to a file
'sybstart'

the status of the server while the script is being executed during startup.

the 'sybstart' file shows that the script does run on startup and that the
server

is functional. However when i do 'showserver' command after startup it's not

running. I'm still not sure what's wrong but I feel it's something minor.



Thanks a bunch; the script is below.



#!/bin/bash
#chkconfig: 345 99 50
#description: sybase startup script



#source function library.
.. /etc/rc.d/init.d/functions
.. /opt/sybase/SYBASE.sh



start() {
/opt/sybase/ASE-15_0/install/startserver -f
/opt/sybase/ASE-15_0/install/RUN_sybase
/opt/sybase/ASE-15_0/install/showserver >> /home/homedir/sybstart



}



case "$1" in
start)
start
;;



stop)
stop

#function to be completed
;;



status)
status

#to be completed
;;



restart)

#to be completed
stop
start
;;
*)
esac





"Joseph H. Rosevear" <joe@airlink9.hopto.org> wrote in message
news:47032059$0$90270$14726298@news.sunsite.dk...
> asdf <asdf@asdf.com> wrote:
>> lets say i have one script which initializes system variables.
>> So it looks something like this:

>
> This is script1:
>
>> #!/bin/bash
>> VAR1="etc/dir1"
>> export VAR1
>> VAR2="etc/dir2"
>> export VAR2

>
>
>> I want to call this script from another script and initialize variables
>> this
>> way.
>> Right now I'm trying this.

>
> This is script2:
>
>> #!/bin/bash
>> cd to the directory of the first script
>> source ./script1

>
> Do this at the command line:
>
> source ./script2
>
> and it will work. You will get a changed environment at the console.
> The other poster was confused.
>
> The source command causes a script to run in the current environment
> instead of in its own shell. Because you use two scripts, you need two
> instances of source.
>
> -Joe
>
> [The remainder of asdf's posting has been omitted.]



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #8 (permalink)  
Old 10-03-2007, 07:10 PM
Joseph H. Rosevear
Newsgroup Contributor
 
Posts: n/a
Re: Ok I think I almost figured it out--almost

asdf <asdf@asdf.com> wrote:
> so I configured chkconfig to start sybstart server on levels 345


> and it shows correctly in rc5.d and also starts up correctly


> when i do 'service sybstart start'




> As a way to error check I also modified script to output to a file
> 'sybstart'


> the status of the server while the script is being executed during startup.


> the 'sybstart' file shows that the script does run on startup and that the
> server


> is functional. However when i do 'showserver' command after startup it's not


> running. I'm still not sure what's wrong but I feel it's something minor.




> Thanks a bunch; the script is below.


[snip]

Sounds great. Keep up the good work.

-Joe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sick of the name-calling Ron Windows Vista 21 09-09-2007 05:10 AM
PC to PC calling Nataneh Internet Explorer 0 08-06-2007 07:50 PM
Calling an MVP janet-m Windows XP 17 06-20-2007 12:40 PM
Calling XP Gurus Trahsub Windows XP 0 04-20-2007 01:45 PM
Calling all Techies!!! ChrisB Windows XP 2 02-08-2007 01:02 PM


New To Technology Questions? Do You Need Help with Your Computer or Device? Do You Need Help with this site?

All times are GMT -8. The time now is 02:35 PM.


2003 - 2009 All Rights Reserved. Technology Questions

Search Engine Friendly URLs by vBSEO 3.3.0