The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
changing multiple directory names w/ sed RahJiggah OS X (Apple) 1 11-05-2007 10:55 AM
Changing Directory Using SFU ilak1008 Windows & DOS: Issues & Discussions 1 05-21-2007 11:50 PM
/root directory - date keeps changing??? Alux Linux 1 03-16-2006 03:28 AM
changing directory on server bb00y High Level Programming 1 12-05-2003 09:59 PM
Changing directory on Unix taher_n UNIX for Advanced & Expert Users 3 06-04-2002 09:45 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-10-2005
Registered User
 

Join Date: Feb 2005
Posts: 7
Changing directory through script

Hi Friends !
I want to change my current directory through a script.
I am running the script say, from /home/proj directory.
The script is like this :

#!/usr/bin/sh
cd module/pack/data


once i run the script, i am still in /home/proj directory only.
My problem is, i dont want to type the big directory structure every time i want to change the directory. So, say i will place the above 2 lines in a file named "a" and i will just i run "a" , i will be taken into /home/proj/module/pack/data directory directly.

please suggest me a way out for this !

thanks,
Reply With Quote
Forum Sponsor
  #2  
Old 02-10-2005
pressy's Avatar
solaris cultist
 

Join Date: Aug 2003
Location: Vienna / Austria (Europe) [EARTH]
Posts: 718
.....

you could use a variable for your "main" path like:
MAIN=/home/proj/module/
$MAIN/blabla.sh
$MAIN/2nd-dir/blabla2.sh

gP
Reply With Quote
  #3  
Old 02-10-2005
Registered User
 

Join Date: Feb 2005
Location: Holon, Israel
Posts: 25
mrgubbala,

You can either define alias ( man alias ) or run your script as
. ./myscript.ksh

rather than

./myscript.ksh
Reply With Quote
  #4  
Old 02-10-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
As Phantom indicates, the easiest way would be to define an alias or function within your shells initialisation files that do this.

Have a look at my sample .kshrc file. There is a "cd aliases" section there.... Obviously this is for the Korn shell but most of it will work with bash too.

As pressy indicates, setting up environment variables with commonly navigated directories is also a good idea. Define and export them within your .profile (or .bash_profile, whatever), e.g.

export WEB=/path/to/my/webroot

Then, to edit a file, for example, all you have to type is

vi $WEB/index.html

Cheers
ZB
Reply With Quote
  #5  
Old 02-10-2005
Registered User
 

Join Date: Feb 2005
Posts: 7
Meaning of . ./myscript.ksh

Thanks for your reply.

But I would like to know what does it mean to shell by typing
. ./myscript.ksh

rather than

./myscript.ksh

Can you please explain me.
Thanks,



Quote:
Originally Posted by Phantom
mrgubbala,

You can either define alias ( man alias ) or run your script as
. ./myscript.ksh

rather than

./myscript.ksh
Reply With Quote
  #6  
Old 02-11-2005
Registered User
 

Join Date: Feb 2005
Location: Holon, Israel
Posts: 25
Quote:
Originally Posted by mrgubbala
But I would like to know what does it mean to shell by typing
. ./myscript.ksh

rather than

./myscript.ksh

Can you please explain me.
Thanks,
when you run the script as . ./myscript commands in the script are executed in the same shell. Therefore, if you change directory or any shell variable, the results stay in effect after the script is completed.

In contrast, by running as ./myscript, the script will be executed in subshell and directory (or variable) changes done in it will be effective only until the script exits.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:59 AM.


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 Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0