TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Linux & Opensource > Unix

Notices

Why Cd isn't a process???

Unix


Reply
 
Thread Tools Display Modes
Old 15-09-2007, 11:50 AM   #1
sulekha
Guest
 
Posts: n/a
Why Cd isn't a process???

Hi all,

I was read ing the book unix for dummies by john levine in it it is
written as follows.

If you make a shell script that contain a cd command ,the cd affects
only subsequent commands in that script, after your shell script
finishes running,you find yourself back in the orginal directory as
though the cd never occured.

Although you can write a script that does change the directory,doing
so is so complicated task that even wizards shrink from the task.

now my question is to what extent the later statement is true????

  Reply With Quote
Old 15-09-2007, 11:50 AM   #2
Lew Pitcher
Guest
 
Posts: n/a
Re: Why Cd isn't a process???

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

sulekha wrote:
> Hi all,
>
> I was read ing the book unix for dummies by john levine in it it is
> written as follows.
>
> If you make a shell script that contain a cd command ,the cd affects
> only subsequent commands in that script, after your shell script
> finishes running,you find yourself back in the orginal directory as
> though the cd never occured.
>
> Although you can write a script that does change the directory,doing
> so is so complicated task that even wizards shrink from the task.
>
> now my question is to what extent the later statement is true????


The part that says that it "is so complicated task that even wizards shrink
from the task" is a bit of an overstatement. Basically, if you want to write
such a script, you only prove that you really don't understand how processes work.

Here's how to make a real simple script to change directory, and I'm by no
means a wizard:

# first, create the script
echo "cd /" >change_directory


# now, execute the script
. change_directory

That's it. Simple enough for you? And totally useless.



- --
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
- ---------- Slackware - Because I know what I'm doing. ------


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Armoured with GnuPG

iD8DBQFGp+u8agVFX4UWr64RAu9ZAKCNC91cn8g4/u9cm/s967p/JcrFPQCg14Ye
mDA1ixJ3ex1C/ehEbxLGSPk=
=TKvH
-----END PGP SIGNATURE-----
  Reply With Quote
Old 15-09-2007, 11:50 AM   #3
Bill Marcum
Guest
 
Posts: n/a
Re: Why Cd isn't a process???

On Wed, 25 Jul 2007 13:30:44 -0700, sulekha
<aarklon@gmail.com> wrote:
>
>
> Hi all,
>
> I was read ing the book unix for dummies by john levine in it it is
> written as follows.
>
> If you make a shell script that contain a cd command ,the cd affects
> only subsequent commands in that script, after your shell script
> finishes running,you find yourself back in the orginal directory as
> though the cd never occured.
>
> Although you can write a script that does change the directory,doing
> so is so complicated task that even wizards shrink from the task.
>
> now my question is to what extent the later statement is true????
>


The current working directory is an environment variable in the current
process, and a child process cannot change its parent's environment.


--
party, n.:
A gathering where you meet people who drink
so much you can't even remember their names.
  Reply With Quote
Old 15-09-2007, 11:50 AM   #4
Chris McDonald
Guest
 
Posts: n/a
Re: Why Cd isn't a process???

Bill Marcum <marcumbill@bellsouth.net> writes:

>The current working directory is an environment variable in the current
>process, and a child process cannot change its parent's environment.



I'd suggest that it's an attribute of the process, not an environment variable.
If "just" an environment variable, we wouldn't need a system call to change it.

--
Chris.
  Reply With Quote
Old 15-09-2007, 11:52 AM   #5
Michael Paoli
Guest
 
Posts: n/a
Re: Why Cd isn't a process???

On Jul 25, 10:04 pm, Bill Marcum <marcumb...@bellsouth.net> wrote:
> On Wed, 25 Jul 2007 13:30:44 -0700, sulekha
> <aark...@gmail.com> wrote:
> > I was read ing the book unix for dummies by john levine in it it is
> > written as follows.


0) Beware of books that contain "Dummies" or "Idiot's" in the title.

> > If you make a shell script that contain a cd command ,the cd affects
> > only subsequent commands in that script, after your shell script
> > finishes running,you find yourself back in the orginal directory as
> > though the cd never occured.


1) Not strictly true. At least one counterexample was already given
among the responses to the original post. I'll show 2
counterexamples, one very much like the one already shown:
a)
$ ls
change_directory change_directory2
$ pwd
/home/m/michael/tmp/CD
$ . ./change_directory
$ pwd
/home/m/michael
$ cd tmp/CD
$ cat change_directory
#!/bin/sh
cd "${1-$HOME}"
b)
$ pwd
/home/m/michael/tmp/CD
$ ./change_directory2
$ pwd
/home/m/michael
$ cd tmp/CD
$ cat change_directory2
#!/bin/sh
cd "${1-$HOME}"
exec sh
Note that in b) an additional process remains and the directory
wasn't changed by the command in the original invoking PID. That
can be avoided by doing:
$ exec ./change_directory2
Note also that in b), "after your shell script finishes running",
that technically the original shell script has finished running
(that happened when the the script used exec), but the PID
continues as that which initially started the actual execution of
the shell script, so the parent continues to consider the child
PID as running and having not completed - though the parent might
not take particular note that the PID is no longer running that
script.

> > Although you can write a script that does change the directory,doing
> > so is so complicated task that even wizards shrink from the task.


Not that I'm a wizard, but I've not even shrunk from the task.
Depending what one wants to do, those might not be highly practical
counterexamples, but they're counterexamples to the book's statements,
nonetheless.

> > now my question is to what extent the later statement is true????

> The current working directory is an environment variable in the current
> process, and a child process cannot change its parent's environment.


It's a false statement. It's the logical AND of two parts:
"a child process cannot change its parent's environment"
That's generally (in most ordinary circumstances), but not quite
always, true. (non-trivial) counterexample: if child process mucks
about with inprocess memory of and related to the parent process,
then that part of the statement could be false.
"The current working directory is an environment variable in the
current process"
That's not generally true. Though some shells may reflect the
current working directory in a shell variable (parameter) or
environment variable (e.g. PWD)), not all such (e.g. the original
Bourne shell) shells do so. And being the logical AND of two
statements, at least one of which is false, the resultant compound
statement is then false.

references:
sh(1)
chdir(2)
execve(2)
exec(3)
http://dictionary.reference.com/browse/wizard
http://www.eps.mcgill.ca/jargon/jargon.html#wizard
books for non-dummies/non-idiots: http://www.oreilly.com/
http://www.eps.mcgill.ca/jargon/jargon.html#RTFM

  Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why Cd isn't a process??? sulekha Unix 3 15-09-2007 11:45 AM
Invoking System.Diagnostics.Process Start and changing the parent process Dax C#(C Sharp) 2 05-09-2007 02:35 PM
RE: settings.ini is being used by another process.... tracnmud Windows Vista All 0 18-08-2007 03:42 AM
Is there a process log? Bill W Windows XP 2 16-08-2007 04:08 PM
Rundll process uses 100% of CPU Keith Windows XP 2 16-08-2007 11:45 AM


< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 02:05 PM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional