[net.unix] Can anyone explain?

ram@wgivax.UUCP (Rick Messinger #4975) (07/26/85)

  In this sh scipt is a sequence of two events.

> : Copy a file to a directory then chdir to the given dir
> case $# in
> 	2) cp $1 $2/$1 ; cd $2 ;;
> 	*) echo 'Usage: ccarry file dir/path' ;;
> esac

 Due to the design of the shell, it does not work (not in actuality anyway).
Does anyone out there know of a way this can be done.  I got word it could
be done in C?  I'll be interested to find out.
Thanks for any info.

 (ram)

mjs@eagle.UUCP (M.J.Shannon) (07/31/85)

> 
>   In this sh scipt is a sequence of two events.
> 
> > : Copy a file to a directory then chdir to the given dir
> > case $# in
> > 	2) cp $1 $2/$1 ; cd $2 ;;
> > 	*) echo 'Usage: ccarry file dir/path' ;;
> > esac
> 
>  Due to the design of the shell, it does not work (not in actuality anyway).
> Does anyone out there know of a way this can be done.  I got word it could
> be done in C?  I'll be interested to find out.
> Thanks for any info.
> 
>  (ram)

It *does* work, but it doesn't do what you want it to.  The only way to get it
to do what you want is to have your shell (not a sub-shell) execute it.  How,
you ask?  Assuming you've called it "ccarry" and it lives in one of the
directories in your PATH, use it thusly:

	. ccarry foo otherdir

The "." tells your shell to execute the script as though you had typed it at
the terminal.  You must use this method for any shell script that does things
that intend to change the environment of the current shell (such as setting
variables).  This is how your .profile is executed when you login.

It can also be done in C, but showing how is more than my not quite awake brain
is up to at this hour of the day.  (MORE CAFFIENE!)
-- 
	Marty Shannon
UUCP:	ihnp4!eagle!mjs
Phone:	+1 201 522 6063

don@hcrvx1.UUCP (Don McKillican) (07/31/85)

>> : Copy a file to a directory then chdir to the given dir
>> case $# in
>> 	2) cp $1 $2/$1 ; cd $2 ;;
>> 	*) echo 'Usage: ccarry file dir/path' ;;
>> esac
>
> Due to the design of the shell, it does not work (not in actuality anyway).
>Does anyone out there know of a way this can be done.

If your version of unix has shell functions, you could try something along
these lines:

	cpd() { cp $1 $2/$1 ; cd $2 ; }

This defines cpd as a shell function which executes the commands listed *within
the current shell*, rather than in a subshell.  This is documented in SH(1)
(page 2 in the System 5 documentation).

			Don McKillican @ Human Computing Resources Ltd.
			{ihnp4,decvax,utzoo,watmath}!hcr!hcrvx1!don

twb@hoqam.UUCP (BEATTIE) (08/01/85)

I attempted to answer the original question via E-mail but there is
some problem at akgua!mcnc as shown below. (I don't really
understand all the uucp stuff)

I have also seen a sufficient answer posted so my actual response
is mute.
BUT, these sorts of problems need to be persued to clean up the
netnews system. There is really no reason why the answer to a
simple question needs to be posted, but if E-mail can't get thru...

Could I have sent a message to someone at akgua notifing them of
this problem without bothering the net?
Does uucp do this automatically?

Happy netting!
Tom.
~!{ihnp4|hou2g|allegra|mhuxh}!hoqam!twb

Blithwapping - v. Using anything BUT a hammer to hammer a nail into the
wall, such as shoes, lamp bases, doorstops, etc.

============================================================
From uucp Thu Aug  1 12:56 EDT 1985
>From daemon  Thu Aug  1 12:56:14 1985 remote from clyde
Date: Thu, 1 Aug 85 11:50:35 edt
From: clyde!daemon
Full-Name: Mail Delivery Subsystem
Subject: Returned mail: unknown mailer error 101
Message-Id: <8508011656.AA18438@clyde.UUCP>
Received: by clyde.UUCP; id AA18438; 1 Aug 85 12:56:14 EDT (Thu)
To: burl!ulysses!hoqam!twb

   ----- Transcript of session follows -----
bad system name: mcnc
uux failed. code 101
554 bonnie!akgua!mcnc!unccvax!wgivax!ram... unknown mailer error 101
554 bonnie!akgua!mcnc!unccvax!wgivax!ram... unknown mailer error 101

   ----- Unsent message follows -----
Received: by clyde.UUCP; id AA18427; 1 Aug 85 12:56:14 EDT (Thu)
Received: by ulysses.UUCP; Thu, 1 Aug 85 11:50:35 edt
Date: Thu, 1 Aug 85 11:50:35 edt
From: ulysses!hoqam!twb
Message-Id: <8508011550.AA28647@ulysses.UUCP>
To: ulysses!burl!clyde!bonnie!akgua!mcnc!unccvax!wgivax!ram
Subject: Re: Can anyone explain?
In-Reply-To: your article <118@wgivax.UUCP>
============================================================

friesen@psivax.UUCP (Stanley Friesen) (08/03/85)

In article <118@wgivax.UUCP> ram@wgivax.UUCP (Rick Messinger #4975) writes:
>
>  In this sh scipt is a sequence of two events.
>
>> : Copy a file to a directory then chdir to the given dir
>> case $# in
>> 	2) cp $1 $2/$1 ; cd $2 ;;
>> 	*) echo 'Usage: ccarry file dir/path' ;;
>> esac
>
> Due to the design of the shell, it does not work (not in actuality anyway).
>Does anyone out there know of a way this can be done.  I got word it could
>be done in C?  I'll be interested to find out.

	Well, the problem is not the design of the shell, it is the
design of the kernel! The current directory is an attribute of a
*process*, not a user or login session, and as such it may only be
passed *downwards* to children, not upwards to the parent. Thus even
a C program would fail, since it also would run as a child process.
The solution is to get the command sequence interpreted directly by
the interactive shell rather than in a sub-shell. Csh has two ways
of doing this, "alias" and "source". For something like this I
usually combine both. That is I set up an alias to "source" the script.
Unfortunately, the sh equivalent of "source", the "." command, does
not pass arguments to the script being interpreted, so it will not
work, and shell variable expansion does not provide for arguments
either, so you have no general equivalent of the alias feature!
-- 

				Sarima (Stanley Friesen)

{trwrb|allegra|cbosgd|hplabs|ihnp4|aero!uscvax!akgua}!sdcrdcf!psivax!friesen
or {ttdica|quad1|bellcore|scgvaxd}!psivax!friesen