The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
My script is not running manna UNIX for Dummies Questions & Answers 8 01-05-2008 12:11 PM
Running KSH script In SFU ilak1008 Windows & DOS: Issues & Discussions 2 05-23-2007 11:17 PM
when running my script below vbslim Shell Programming and Scripting 5 01-03-2006 08:11 PM
running a script? jtredway UNIX for Dummies Questions & Answers 2 09-11-2001 08:08 PM
Running a script from CDE... kristy UNIX for Dummies Questions & Answers 4 09-05-2001 07:33 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-07-2007
Registered User
 

Join Date: Dec 2006
Posts: 18
running cd from a script

is there anyway to change directories in a script and have it actually work?

when u put something like cd /var/tmp in a script named runner.

when u run that runner script, the current directory is still what it is. it doesn't change to /var/tmp.

what can be done about this?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-07-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
depends on HOW you execute your 'runner' script.

try prefixing 'runner' with '. ' (dot space):
Code:
. runner
Reply With Quote
  #3 (permalink)  
Old 03-07-2007
Registered User
 

Join Date: Aug 2005
Location: Bangalore
Posts: 196
you can use cd command itself to change the directory before running the script something like this:

Code:
cd /folder/sub folder
. ./script
Reply With Quote
  #4 (permalink)  
Old 03-07-2007
Registered User
 

Join Date: Jan 2007
Posts: 8
what is the mean by placing a dot before the script name, when I am learning the shell, I found only if you put dot before the script name, or those command such as ' export variable ' in the shell file would not take effect?
Thanks
Reply With Quote
  #5 (permalink)  
Old 03-08-2007
Registered User
 

Join Date: Aug 2005
Location: Bangalore
Posts: 196
. is used run the script in the same process. export works when you use the . before the script. here is an example:

Code:
$$ cat try
. ./test
echo $TEST
$$ cat test
export TEST=1
$$ ./try
1
Reply With Quote
  #6 (permalink)  
Old 03-08-2007
cfajohnson's Avatar
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 682
Quote:
Originally Posted by SkySmart
is there anyway to change directories in a script and have it actually work?

when u put something like cd /var/tmp in a script named runner.

when u run that runner script, the current directory is still what it is. it doesn't change to /var/tmp.

what can be done about this?
When you execute a script, it is run in a child process, and a child process cannot affect the environment of its parent.

To change directories, you need to execute cd in the current shell; cd is a shell builtin command, and thus runs in the current shell environment.

To get a script to run in the current shell, you can source it, using the dot command:

Code:
. name-of-script
In bash, you can also use the word, source:

Code:
source name-of-script
You can also put it into a function.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0