Free Downloads, Community Forum,
FAQs and Developer Resources


Make /Tools Your Home | Link to us

Today's posts | Posts since last visit | Most Active Topics

All Forums Register Login Search Subscriptions My Profile Inbox
Tool Warehouse FAQs Resources Help Member List Address Book Logout

cd in script is temporary

 
Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [SFU / Interix / SUA Technology] >> SFU / Interix - Getting Started >> cd in script is temporary Page: [1]
Login
Message << Older Topic   Newer Topic >>
cd in script is temporary - Sep. 8, '06, 7:59:57 AM   
lforster

 

Posts: 11
Joined: Dec. 12, '05,
Status: offline
When I use cd within a Korn shell script, it appears to take effect only for the life of the script:
----
>pwd
/dev/fs/C/Documents and Settings/lforster/Tools
>cat go
d1="/dev/fs/C/Documents and Settings"
if [ ! -d "$d1" ]; then
echo d1 does not exist
fi
ls -d "$d1"
cd "$d1"
echo I am here: `pwd`
pwd
>go
/dev/fs/C/Documents and Settings
I am here: /dev/fs/C/Documents and Settings
/dev/fs/C/Documents and Settings
>pwd
/dev/fs/C/Documents and Settings/lforster/Tools
type cd
cd is a shell builtin
----

This is totally unexpected behavior.
Post #: 1
RE: cd in script is temporary - Sep. 8, '06, 10:06:14 AM   
eperea

 

Posts: 70
Joined: Apr. 6, '05,
Status: offline
Why did you expect it to be different? AIX, SCO and BSD (at least) work the same way. It would be absolutely insane for a script to change the current directory the way you seem to want it to.

< Message edited by eperea -- Sep. 8, '06, 10:07:54 AM >

(in reply to lforster)
Post #: 2
RE: cd in script is temporary - Sep. 8, '06, 1:02:34 PM   
Rodney

 

Posts: 3714
Joined: Jul. 9, '02,
From: /Tools lab
Status: offline
Eperea is correct. All shell scripts work this way.
It allows scripts to do something independent which affecting, say, an
interactive shell's current context. Besides you can have a script run
by one shell (ksh for example) while your interactive shell is
another (tcsh for example).

If you want your current shell to be affected by the actions of the script
then the interactive and script shell need to be the same, and you "source"
the script. With csh/tcsh the command is "source"; with sh/ksh the
command is ".".

(in reply to eperea)
Post #: 3
RE: cd in script is temporary - Sep. 8, '06, 4:08:11 PM   
lforster

 

Posts: 11
Joined: Dec. 12, '05,
Status: offline
I totally agree and apologize for the post. I’ve used a ‘go’ command on a UnixWare server for some time now, but it’s actually a function within my .profile, not a separate shell script. It is therefore ‘sourced’ as you suggested. Now that I’ve rediscovered this, here’s an example of how it works. I add this to the top of my .profile (there will be additional choices):

go()
{
d1="/dev/fs/C/Documents and Settings/lforster/My Documents/Visual Studio Projects/Back Office Merger Libraries"
echo "1) $d1"
read KEY
if [ ! "$KEY." = "." ]; then
if [ "$KEY" = "1" ]; then
cd "$d1"
fi
fi
}

Then when I log in and (at some point) type ‘go’, it looks like this (the current directory is in parentheses).

(/dev/fs/C/Documents and Settings/lforster)
>go
1) /dev/fs/C/Documents and Settings/lforster/My Documents/Visual Studio Projects/Back Office Merger Libraries
1
(/dev/fs/C/Documents and Settings/lforster/My Documents/Visual Studio Projects/Back Office Merger Libraries)
>

This is just what I wanted. I’m sure you can see why I don’t want to key the entire path too often.
Again, thanks for setting me straight.

(in reply to lforster)
Post #: 4
Page:   [1]
All Forums >> [SFU / Interix / SUA Technology] >> SFU / Interix - Getting Started >> cd in script is temporary Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Search All Forums -

Advanced search


SPONSORS



Forum Software © ASPPlayground.NET Advanced Edition 2.5 ANSI

0.063