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
To call/execute a shell script from a shell script konark UNIX for Dummies Questions & Answers 1 10-26-2007 02:16 PM
How to pass a parameter from one Shell-script to another Shell-script subodhbansal Shell Programming and Scripting 2 09-22-2007 02:19 AM
How to Run a shell script from Perl script in Parent shell? hifake Shell Programming and Scripting 16 08-28-2007 05:42 PM
Accessing variables of one shell script in another shell script rsendhilmani Shell Programming and Scripting 1 04-30-2007 05:43 AM
Have a shell script call another shell script and exit heprox Shell Programming and Scripting 2 11-20-2006 04:17 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-14-2004
Registered User
 

Join Date: Jul 2002
Posts: 34
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
cd in shell script

I have a shell script (bash shell) that just has a cd command, but when it runs, the directory does not change. How can I get this to work?
__________________
[url=http://www.spetnik.com/images/storybanner.php][img]http://www.spetnik.com/images/storybanner.php?s=i[/img][/url]
Forum Sponsor
  #2 (permalink)  
Old 01-14-2004
Kelam_Magnus's Avatar
Unix does a body good.
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
It would help greatly if you can post all or part of it... hard to say till we can see it.

You may not be having the problem you think... Is the cd /dir saved as a variable?

Im guessing this is Linux since its bash...
__________________
My brain is your brain
  #3 (permalink)  
Old 01-14-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
A shell script is executed in a sub process. If you use a cd command, that sub-process will change directories. But when the script finishes, the parent process resumes. A sub process cannot affect its parent's current directory.
  #4 (permalink)  
Old 01-14-2004
Kelam_Magnus's Avatar
Unix does a body good.
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
i should have remembered that from watching my ignite command kick off from cron... It executes a sh to run the make_tape_recovery from my mkignite.sh script.

It is best to use the full path in shell scripts imho...
__________________
My brain is your brain
  #5 (permalink)  
Old 01-14-2004
Registered User
 

Join Date: Jul 2002
Posts: 34
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
the shell script just has the following:

cd /home/spetnik


I remember back in school on sh shell with SCO Unix V that I was able to make a similar script. Surely, there must be a way, no?

-Aaron
__________________
[url=http://www.spetnik.com/images/storybanner.php][img]http://www.spetnik.com/images/storybanner.php?s=i[/img][/url]
  #6 (permalink)  
Old 01-14-2004
Kelam_Magnus's Avatar
Unix does a body good.
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
I think you can save the path to a variable and call it...

OR maybe add the PATH to $PATH for the dir in your script and call it that way
__________________
My brain is your brain
  #7 (permalink)  
Old 01-14-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,208
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
You would have to run your cd script in your current shell, not a sub-shell, so...

. /path/to/script

...i.e dot space scriptname
  #8 (permalink)  
Old 01-14-2004
kduffin's Avatar
UN1X
 

Join Date: Nov 2003
Location: Virginia
Posts: 441
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Ygor is correct. You use . to prevent a "fork" from occuring when you execute the script. Keep in mind, if you just want the script to cd to the directory and then perform som functions, that will work within the script just fine.

Consider the following script:

cd /tmp
pwd

If you run the script, it echos out /tmp as the path - thus any operations that follow in the script will take place in /tmp. When the shell terminaties, you will not be in /tmp, but in your starting directory. If you want the script to leave the running shell in that directory, use:

. script

Cheers,

Keith
Google UNIX.COM
Closed Thread

Tags
linux

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
"inappropriate ioctl for device" 421 service not available, remote server has closed connection autosys awk trim bash eval bash exec bash for loop boot: cannot open kernel/sparcv9/unix close_wait command copy/move folder in unix curses.h cut command in unix dead.letter find grep find null character in a unix file grep multiple lines grep or grep recursive inaddr_any inappropriate ioctl for device logrotate.conf lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp batch sftp script snoop unix stale nfs file handle syn_sent tar exclude unix unix .profile unix com unix date command unix forum unix forums unix interview questions unix mtime unix simulator unix.com vi select all vi tab size vi+substitute+end+of+line+character while loop within while loop shell script


All times are GMT -7. The time now is 11:31 AM.


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

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101