[comp.unix.questions] changing dir inside shell

bianco@uxe.cso.uiuc.edu (07/26/88)

I have the following in a file called 'mover':
 cp *.me /usr/tmp
 cd /usr/tmp 
Upon entering 'sh mover' the files are copied, and the directory is
changed, however the shell is then exited and the current directory
is changed back to my home directory. How can I cause the shell to exit
in the directory I changed to ? 

-nick
----------------------------------------------------------
bianco@uiuc.uxe.cso.edu  or  bianco@uiucvmd.cso.uiuc.edu
----------------------------------------------------------
"Nothing is given to man on earth except a potential and the material on which
 to actualize it."           
   Ayn Rand

pdb@sei.cmu.edu (Patrick Barron) (07/27/88)

In article <47800013@uxe.cso.uiuc.edu> bianco@uxe.cso.uiuc.edu writes:
>How can I cause the shell to exit
>in the directory I changed to ? 

Your shell script runs in a child process, and can't affect the state
of it's parent (like changing it's working directory).  To make this
work, you need to execute the shell commands within your top-level shell.
Use ". <filename>" to do this, or "source <filename>" with the C-shell.

--Pat.

ron@topaz.rutgers.edu (Ron Natalie) (07/27/88)

You can't.  Directory changing is a per process thing.
A cd in a shell file will only change the directory for
the duration of that shell file.  You need to get the
cd interpreted by the interactive shell.  Assuming you
have a 5R2 or later Bourne Shell, try something like
this:

mover() {
 cp *.me /usr/tmp
 cd /usr/tmp 
}

kluft@hpcupt1.HP.COM (Ian Kluft) (07/27/88)

bianco@uxe.cso.uiuc.edu writes:
> 
> I have the following in a file called 'mover':
>  cp *.me /usr/tmp
>  cd /usr/tmp 
> Upon entering 'sh mover' the files are copied, and the directory is
> changed, however the shell is then exited and the current directory
> is changed back to my home directory. How can I cause the shell to exit
> in the directory I changed to ? 

That depends on which version of the Bourne Shell (assumed from the "sh"
in your example) you're using.  If you're on System V Release 2 or later
(or using the Korn Shell), you're in luck.

The solution comes down to one thing: you must do the 'cd' in your
interactive shell.  You can have it do it for you using shell functions.
Put the following example in your .profile:

mover()
{
	cp *.me /usr/tmp
	cd /usr/tmp
}

If you don't have shell functions, there's still a solution but it's not
quite as nice.  You can type ". mover" instead of "sh mover" to force it
to do the script in your interactive shell.

------------------------------------------------------------------
    Ian Kluft			RAS Lab
    UUCP: hplabs!hprasor!kluft	HP Systems Technology Division
    ARPA: kluft@hpda.hp.com	Cupertino, CA
------------------------------------------------------------------

rbj@nav.icst.nbs.gov (Root Boy Jim) (07/28/88)

? From: bianco@uxe.cso.uiuc.edu

? I have the following in a file called 'mover':
?  cp *.me /usr/tmp
?  cd /usr/tmp 
? Upon entering 'sh mover' the files are copied, and the directory is
? changed, however the shell is then exited and the current directory
? is changed back to my home directory. How can I cause the shell to exit
? in the directory I changed to ? 

The bad news is that you can't, at least not the way you are doing it.
The good news is that you can, if you do it in other ways.

The problem is that `mover' is being executed by a subshell, and when it
goes away, the parent's working directory is unchanged. Solutions:

1) If `sh' or `ksh' is your parent shell, type ". mover".
   You may also use shell functions, or even aliases if you prefer.

2) If `csh' is your parent shell, type "source mover".
   You may also use aliases here.

Note that sourcing a file is less flexible, as you cannot pass any
parameters ($1, $2, etc) to it. With shell functions and aliases you can.

? -nick		bianco@uiuc.uxe.cso.edu  or  bianco@uiucvmd.cso.uiuc.edu

	(Root Boy) Jim Cottrell	<rbj@icst-cmr.arpa>
	National Bureau of Standards
	Flamer's Hotline: (301) 975-5688
	The opinions expressed are solely my own
	and do not reflect NBS policy or agreement
	Careful with that VAX Eugene!

bianco@uxe.cso.uiuc.edu (07/28/88)

Many thanks to those that responded to my question. The two answers I
have been told are:

1)   Instead of 'sh mover', try '. mover' (if you're using sh or ksh) or
     'source mover' (if you're using csh).

2)   If you are running a shell which supports aliasing, you can:

     alias mover="cp *.me /usr/tmp;cd /usr/tmp"

-----------
ucbvax!garnet!weemba	Matthew P Wiener/Brahms Gang/Berkeley CA 94720
-----------
Tracey Baker  {att, rutgers!moss}!mhuxu!tab or tab@mhuxu.att.com  (201)582-5357
-----------
newcomb@cory.Berkeley.EDU Tom Newcomb
-----------
 jfh@rpp386.uucp  -john	(The Beach Bum at The Big "D" Home for Wayward Hackers)
-----------
internet: mkhaw@teknowledge.arpa  Mike Khaw
uucp:	  {uunet|sun|ucbvax|decwrl|uw-beaver}!mkhaw%teknowledge.arpa
----------
DOMAIN: dsix2!daveh@masa.com                                /dsix2!daveh
  UUCP: {uunet | rutgers | spl1 | ...}!{masa.com | hombre}!<
Dave Hammond                                                \marob!daveh
-----------------------------------------------------------------------------
Breck Beatie	    				(415)856-8649
{uunet,ames!coherent}!aimt!breck

daniel@island.uu.net (Dan Smith) (07/28/88)

In article <Jul.26.15.39.08.1988.28499@topaz.rutgers.edu> ron@topaz.rutgers.edu (Ron Natalie) writes:
>You can't.  Directory changing is a per process thing.

	Well...you can, at least on BSD systems. I use a program that
will "type" its arguments to standard input (like a "cd foo" command).
I guess it would be analogous to "keyfake" or something of that type for
MsLoss. Ron's right technically - you just have to work around the
per-process limit! :-) Below is the source to "typein", written
by my friend David Vezie many moons ago. Posted with permission.
Try "typein date", and "typein date^M" (type control V control M)

				dan

-----------------------snip crumble chomp-----------------------------
/*
 * typein.c -- push it's arguments onto the standard input.
 * uses the TIOCSTI ioctl, and LPENDIN (turning off echo), so
 * it appears after the prompt.
 *
 * Copyright 1984, David Vezie.
 * Permission to copy unlimited granted, providing this message
 * stays with the source
 * All other rights reserved (unless all lawyers are shot :-).
 */
#include <stdio.h>
#include <sgtty.h>

main(argc, argv)
	int argc;
	char **argv;
{
	register char *cp;
	struct sgttyb stb, stb2;
	int pendin = LPENDIN;

	ioctl(2, TIOCGETP, &stb);
	stb2 = stb;
	stb.sg_flags &= ~ECHO;
	ioctl(2, TIOCSETN, &stb);
	for (argc--, argv++; argc > 0; argc--, argv++) {
		for (cp = *argv; cp && *cp; cp++)
			ioctl(2, TIOCSTI, cp);
		if (argc > 1)
			ioctl(2, TIOCSTI, " ");
	}
	ioctl(2, TIOCSETN, &stb2);
	ioctl(2, TIOCLBIS, &pendin);
	exit(0);
}
----------------------------------------------------------------------
-- 
dan smith, island graphics, marin co, ca| +1 (415) 491 1000(W), 332 FAST(H)
4000 civic center dr, san rafael 94903  | dis: they're solely my opinions
daniel@island.uu.net {ucbvax!ucbcad,sun}!island!daniel pacbell!unicom!daniel
I'd rather have Roosevelt in a wheelchair, than Reagan & Bush on a horse -Jesse

ok@quintus.uucp (Richard A. O'Keefe) (07/28/88)

In article <16654@brl-adm.ARPA> rbj@nav.icst.nbs.gov (Root Boy Jim) writes:
...
>1) If `sh' or `ksh' is your parent shell, type ". mover".
>   You may also use shell functions, or even aliases if you prefer.
...
>Note that sourcing a file is less flexible, as you cannot pass any
>parameters ($1, $2, etc) to it. With shell functions and aliases you can.

You can pass parameters to a sourced file using named variables.
E.g. suppose file "demo" contains
	echo $arg1
and you do
	arg1=example . demo

torsten@pcsbst.UUCP (Torsten Homeyer) (07/28/88)

In article <47800013@uxe.cso.uiuc.edu< bianco@uxe.cso.uiuc.edu writes:
<
<I have the following in a file called 'mover':
< cp *.me /usr/tmp
< cd /usr/tmp 
<Upon entering 'sh mover' the files are copied, and the directory is
<changed, however the shell is then exited and the current directory
<is changed back to my home directory. How can I cause the shell to exit
<in the directory I changed to ? 

	Try the shells dot-command:

	$ . mover

	Then all commands in your script will be interpreted by your current
	shell, without forking another shell to interpret your commnd file.

		Torsten.

steve@geac.UUCP (Steve Lane) (07/28/88)

In article <47800013@uxe.cso.uiuc.edu> bianco@uxe.cso.uiuc.edu writes:
>
>I have the following in a file called 'mover':
> cp *.me /usr/tmp
> cd /usr/tmp 
>Upon entering 'sh mover' the files are copied, and the directory is
>changed, however the shell is then exited and the current directory
>is changed back to my home directory. How can I cause the shell to exit
>in the directory I changed to ? 
>

Try using either of the following:

	. mover            -- instead of 'sh mover'
or	source mover       -- instead of 'csh mover'

Each of these statements executes the commands in 'mover' in the current
shell rather than firing up a sub-shell. You can also change shell
variables in a similar manner.

Steve.

-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Steve Lane,                        UUCP: ...[mnetor, yunexus, utgpu]!geac!steve
Software Solutions                 (416) 221-2830
108-755 Steeles Ave W, Willowdale, Ontario, M2R 2S6.

orchid@deneb.ucdavis.edu (0048;0000007760;130;574;56;) (07/29/88)

This is power-hungry, but consider

	[$ | %] exec mover

at the command line, where mover contains

	mv files* dir
	cd dir
	exec $SHELL

Of course, arguments can be added as appropriate.
The only problem I've found so far is that you can't
run login in csh as '% login <user>' after running 
mover.		Tom. (orchid@deneb.ucdavis.edu.UUCP)