LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2003, 06:26 PM   #1
joseamuniz
Member
 
Registered: May 2003
Distribution: Mandrake Linux (9.1) , Red Hat Linux (8)
Posts: 32

Rep: Reputation: 15
UNIX processes


Quote:
Understanding that a new process is created when you run a shell script helps to explain a very common misconception under UNIX. When you run a shell script and that script changes directories, your original shell knows nothing about the change. This confuses a lot of people who are new to UNIX as they come from the DOS world, where changing the directory from within a batch file does change the original shell. This is because DOS does not have the same concept of a process as UNIX does.
Extracted from here

I read this in a Linux tutorial. And this lead me to think that if I created a function that changed my directory, it would execute the function and still return me to the same place before I ran the script, so I created:

changeDir()
{
cd /
}

and executed it from my home directory. And the result was me getting redirected to /. Am I doing something wrong? What are they referring to if not that? I don't understand. I tried searching in Google but found nothing. Thank you very much in advance.
 
Old 06-29-2003, 06:45 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Ok, how did you use this function you created? The tutorial mentions running in a shell script. I just tried putting this into a script called test.bash:

Code:
#!/bin/bash

cd /
echo "Changed directory to /"
echo `pwd`

exit 0
If you execute that script from a directory other than root, it should tell you the working directory is /, but when it quits, you'll be in the same directory you started the script from.

This is why: The '#!/bin/bash' at the top says to create a new bash shell and execute the commands within that new shell. This new shell is another process, and not at all related to the shell you started the script from. It's similar to having two terminals open at the same time.

make sense?
 
Old 06-29-2003, 07:07 PM   #3
joseamuniz
Member
 
Registered: May 2003
Distribution: Mandrake Linux (9.1) , Red Hat Linux (8)
Posts: 32

Original Poster
Rep: Reputation: 15
Oh... I got it to work. I can't figure out what I had one wrong but I created my script again and ran it and everything ran smoothly. Thanks for your help.

Last edited by joseamuniz; 06-29-2003 at 07:12 PM.
 
Old 06-29-2003, 07:16 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Hehehe... typing this:
Code:
. example
isn't doing what you think it's doing... At least, that's the impression I get from the beginning of your reply. That command (the period by itself) is an alias for the source command. That WOULD cause you to change directories in the current shell.

As for the PATH environment variable, it is entirely up to you whether to include the current directory or not. I prefer not to, because then I'm guaranteed not to run something accidentally. Only the programs "officially" installed will run, forcing me to be absolutely certain I want to run what's in the current dir.
 
Old 06-29-2003, 09:55 PM   #5
joseamuniz
Member
 
Registered: May 2003
Distribution: Mandrake Linux (9.1) , Red Hat Linux (8)
Posts: 32

Original Poster
Rep: Reputation: 15
Yeah I got aware of my stupid reply and edited it :P I didn't know about the alias, though. That cleared yet more doubts out. Thanks a bunch.
 
Old 06-29-2003, 10:16 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Stupid? Nah. It's those little things that can cause the biggest stumbling blocks. I chuckled only because I had very similar experiences.

Glad I could help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unix Book for Beginners (Not Linux... Unix) suse2166 General 6 11-25-2004 11:46 AM
monitoring active processes and identifying the required processes. gajaykrishnan Programming 2 08-13-2004 01:58 AM
Why did you experienced users of Unix change to unix over Windows? Laptop2250 Linux - General 11 10-28-2003 11:51 AM
How to schedule unix script periodically from unix os level??? gopi_20us Programming 2 03-11-2002 06:45 AM
UNIX processes gluon Programming 5 01-26-2002 05:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration