- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to change the working dir for parent shell?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2002 11:00 PM
тАО11-07-2002 11:00 PM
I just hope to change the current working dir after a script has been executed.
That is,
ls
chdir mygrep mymake mytest temp
My expection is as below:
chdir(NOT . chdir)
However I wrote the script "chdir" with:
. cd temp
or exec cd temp
It doesn't work well,the working dir still keep the initial one.
Maybe the trap could be helpful?
Thanks
Jack
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2002 11:53 PM
тАО11-07-2002 11:53 PM
SolutionFollow this link and goto 2.8 chapter.
http://groups.google.com/groups?dq=&hl=fr&lr=&ie=UTF-8&threadm=unix-faq/faq/part2_1036576409%40rtfm.mit.edu&prev=/groups%3Fgroup%3Dcomp.unix.questions
You can found :
2.8) How do I {set an environment variable, change directory} inside
a program or shell script and have that change affect my
current shell?
In general, you can't, at least not without making special
arrangements. When a child process is created, it inherits a
copy of its parent's variables (and current directory). The
child can change these values all it wants but the changes won't
affect the parent shell, since the child is changing a copy of
the original data.
Some special arrangements are possible. Your child process could
write out the changed variables, if the parent was prepared to
read the output and interpret it as commands to set its own
variables.
Also, shells can arrange to run other shell scripts in the
context of the current shell, rather than in a child process, so
that changes will affect the original shell.
For instance, if you have a C shell script named "myscript":
cd /very/long/path
setenv PATH /something:/something-else
or the equivalent Bourne or Korn shell script
cd /very/long/path
PATH=/something:/something-else export PATH
and try to run "myscript" from your shell, your shell will fork
and run the shell script in a subprocess. The subprocess is also
running the shell; when it sees the "cd" command it changes *its*
current directory, and when it sees the "setenv" command it
changes *its* environment, but neither has any effect on the
current directory of the shell at which you're typing (your login
shell, let's say).
In order to get your login shell to execute the script (without
forking) you have to use the "." command (for the Bourne or Korn
shells) or the "source" command (for the C shell). I.e. you type
. myscript
to the Bourne or Korn shells, or
source myscript
to the C shell.
If all you are trying to do is change directory or set an
environment variable, it will probably be simpler to use a C
shell alias or Bourne/Korn shell function. See the "how do I get
the current directory into my prompt" section of this article for
some examples.
A much more detailed answer prepared by
xtm@telelogic.se (Thomas Michanek) can be found at
ftp.wg.omron.co.jp in /pub/unix-faq/docs/script-vs-env.
Regards,
Jerome
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2002 11:58 PM
тАО11-07-2002 11:58 PM
Re: How to change the working dir for parent shell?
Start the script as:
. chdir
or
. ./chdir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-08-2002 12:26 AM
тАО11-08-2002 12:26 AM
Re: How to change the working dir for parent shell?
As I put such script in the bin directory,I don't want other users have to use the command ". /users/tester/bin/chdir",and wish the only "chdir" command could set the current dir successfully.
That's the reason why I mention "change dir for PARENT shell".
Cheers
Jack
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-08-2002 02:24 AM
тАО11-08-2002 02:24 AM
Re: How to change the working dir for parent shell?
You could do something like:
cd $(chdir)
The chdir script runs and prints the name of the directory on standard output and the parent shell cd's to it.
If you give some more information about what you are trying to achieve, I'm sure that a solution can be found.
Regards,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-08-2002 02:44 AM
тАО11-08-2002 02:44 AM
Re: How to change the working dir for parent shell?
Script commands are execute in the shell specified in the script, once the script has finished it exits the shell it was using and returns to the current environment.
If want to keep the environment of the script you have to execute the scipt using a . before.
e.g.
[/]# . /tmp/bin/yourscript
or
[/tmp/bin] # . ./yourscript
Chuck J
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-08-2002 10:33 PM
тАО11-08-2002 10:33 PM
Re: How to change the working dir for parent shell?
Yes,I know that the child shell is running under another separate process.
My initial intention with which the script was developed:
1.Check the current environment variable.(OK)
2.Create/or not a new directory with the infor above.(OK)
3.Change the current directory to the new one which was just made.
(Notice: the child shell has finished now,so the dir changing was not effectiv.The script user has to set his dir by hand.I just want to remove such unnecessary step for convenience.)
Any comments please let me know.
Cheers
Jack
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-08-2002 10:43 PM
тАО11-08-2002 10:43 PM
Re: How to change the working dir for parent shell?
Yes,I know that the child shell is running in another separate process.
My initial intention with which the script was developed:
1.Check the current environment variables.(OK)
2.Create/or not a new directory with the infor above.(OK)
3.Change the current dir to the new one that was made by step 2.
(Notice: Since the child shell has finished,the dir changing was not effective.
Thus the script user has to do such action by hand now,and I just want to remove these unnecessary steps for the user's convenience.)
Any comments please let me know.
Cheers
Jack
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-11-2002 01:11 AM
тАО11-11-2002 01:11 AM
Re: How to change the working dir for parent shell?
--------------------
#!/usr/bin/sh
DIR1=/tmp
cd $DIR1
--------------------
Then at the command line do this:
[server:/home/chuckj]# . ./script
It will change the directory to /tmp upon exit:
[server:/tmp]#
but if in your script you use the absolute path to the cd command i.e.
/usr/bin/cd $DIR1
then execute from your commnand line, it won't change the directory.
If you want to do this, have users start a new shell before running the script:
[server:/home/user]# sh
[server:/home/user]# . /tmp/scripts/script