[comp.unix.questions] using cd command in a file

ceblair@ux1.cso.uiuc.edu (Charles Blair) (05/20/91)

   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
Thanks in advance.  I'm sure I'm overlooking something well-known.

muquit@garfield.ncat.edu (MUHAMMAD A. MUQUIT) (05/21/91)

In article <1991May20.155136.25162@ux1.cso.uiuc.edu> Charles Blair writes:
>
>   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
>creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
>Thanks in advance.  I'm sure I'm overlooking something well-known.

You can do the job if you put this line in your .login file:
     alias j 'cd /me/A/B/C/D'

I'm also curious why your way didn't work.  I think  there're lots of gurus
out there to answer this.

--
==================================================================
Muhammad A. Muquit
Graduate Student                        Office: 126 Graham Hall
Dept. of Civil Engineering              Phone: (919) 334-7737-53
North Carolina A&T State University
Greensboro, NC 27411

gordon@osiris.cso.uiuc.edu (John Gordon) (05/21/91)

ceblair@ux1.cso.uiuc.edu (Charles Blair) writes:

>   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
>creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
>Thanks in advance.  I'm sure I'm overlooking something well-known.

	This would work better as an alias in your .cshrc or .profile.
Example: alias j 'cd /me/A/B/C/D'

---
John Gordon
Internet: gordon@osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon@cerl.cecer.army.mil       #include <clever_saying.h>

lothar@tmcsys.UUCP (L. Hirschbiegel) (05/21/91)

In article <1991May20.155136.25162@ux1.cso.uiuc.edu> ceblair@ux1.cso.uiuc.edu (Charles Blair) writes:
>
>   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
>creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.

Sure it worked. In the subshell you were starting, which is obviously NOT
what you wanted ... (hint, hint:-)
Folks, isn't this a classic FAQ? Is there a FAQ for this newsgroup? 

>Thanks in advance.  I'm sure I'm overlooking something well-known.

You're right. ;-)

L. Hirschbiegel


-- 
-----------------------------------------------
L. Hirschbiegel, AEG - A84, Frankfurt (Germany) 
email: unido!aega84!lh     tel: -49-69-66414316  
-----------------------------------------------

kaleb@thyme.jpl.nasa.gov (Kaleb Keithley) (05/21/91)

In article muquit@garfield.ncat.edu (MUHAMMAD A. MUQUIT) writes:
>In article Charles Blair writes:
>>
>>   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
>>creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
>>Thanks in advance.  I'm sure I'm overlooking something well-known.
>
>You can do the job if you put this line in your .login file:
>     alias j 'cd /me/A/B/C/D'
>
>I'm also curious why your way didn't work.  I think there are lots of gurus
>out there to answer this.

I'm not a guru, but if you consider that the shell that is running the 
script is a different shell than the one you are typing from.  

The shell that is running the script changes directories, and then exits, 
returning you to your original shell, which, BTW, never changed directories.

-- 
Kaleb Keithley                        kaleb@thyme.jpl.nasa.gov

Meep Meep                             Roadrunner
Veep veep                             Quayle

tchrist@convex.COM (Tom Christiansen) (05/21/91)

From the keyboard of ceblair@ux1.cso.uiuc.edu (Charles Blair):
:
:   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
:creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
:Thanks in advance.  I'm sure I'm overlooking something well-known.

Like your nice value, uid, and umask, your current working directory 
is an attribute associated with a given process.  They are all inherited
by child processes, but changes in children are not propagated back up 
to the calling parent process.  That means a cd in a child is
not reflected in the parent.  You need to coerce the parent into
doing this itself, as in:

    alias j='cd /me/A/B/C/D'

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time." 

gordon@osiris.cso.uiuc.edu (John Gordon) (05/21/91)

muquit@garfield.ncat.edu (MUHAMMAD A. MUQUIT) writes:

>In article <1991May20.155136.25162@ux1.cso.uiuc.edu> Charles Blair writes:
>>
>>   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
>>creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
>>Thanks in advance.  I'm sure I'm overlooking something well-known.

>You can do the job if you put this line in your .login file:
>     alias j 'cd /me/A/B/C/D'

>I'm also curious why your way didn't work.  I think  there're lots of gurus
>out there to answer this.

	Alias is definitely the way to go here.  As to why the script-file
solution did not work: Whenever you invoke a script-file, it creates its
own shell.  Within this shell, it cd's to the right directory, but then
the script-file process terminates and your shell is still sitting right
where it was before.

John

daniel@island.COM (Daniel Smith "innovation, not litigation...") (05/22/91)

In <1991May20.155136.25162@ux1.cso.uiuc.edu> ceblair@ux1.cso.uiuc.edu (Charles Blair) writes:


>    I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
> creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
> Thanks in advance.  I'm sure I'm overlooking something well-known.

	You may get a lot of answers telling you to make an alias so that
when you type 'j', you'll go where you want.  I'll propose another solution
I've been using for several years.

	In your ~/.cshrc file, add the element "~/.dirs" to the cdpath
variable.  You may end up with something that looks like this:

set cdpath=(. ~ .. ../.. /usr ~/.dirs)  # rearrange to suit...

	Now, mkdir ~/.dirs, and cd into it...

	As for your example, you could:

	ln -s /me/A/B/C/D j

	now source your ~/.cshrc, and you should be able to "cd j" and get
to where you want.

	The underlying idea is that you are creating a directory (~/.dirs)
that potentially has a bunch of pointers (symbolic links) to places you like
to go to frequently.  For instance, I "cd ulb" to go to /usr/local/bin,
"cd ubx" to go to /usr/bin/X11, and so on.

					Daniel
-- 
daniel@island.com       Daniel Smith, Island Graphics, (415) 491 0765 x 250(w)
daniel@world.std.com      4000 CivicCenterDrive SanRafael MarinCounty CA 94903
dansmith@well.sf.ca.us      Fax: 491 0402 Disclaimer: Hey, I wrote it, not IG!
falling/yes I'm falling/and she keeps calling/me back again - IJSaF, Beatles

tchrist@convex.COM (Tom Christiansen) (05/22/91)

From the keyboard of daniel@island.COM (Daniel Smith "innovation, not litigation..."):
:	In your ~/.cshrc file, add the element "~/.dirs" to the cdpath
:variable.  You may end up with something that looks like this:
:
:set cdpath=(. ~ .. ../.. /usr ~/.dirs)  # rearrange to suit...
:
:	Now, mkdir ~/.dirs, and cd into it...
:
:	As for your example, you could:
:
:	ln -s /me/A/B/C/D j
:
:	now source your ~/.cshrc, and you should be able to "cd j" and get
:to where you want.
:
:	The underlying idea is that you are creating a directory (~/.dirs)
:that potentially has a bunch of pointers (symbolic links) to places you like
:to go to frequently.  For instance, I "cd ulb" to go to /usr/local/bin,
:"cd ubx" to go to /usr/bin/X11, and so on.

Interesting.  Another baroque (baroquen? :-) idea is to just set a
variable to the name of directory.  On most csh derivitives, it suffices
to incant:

    set j=/me/A/B/C/D
    cd j

This is nice as it also allows you to use $j for things.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time." 

chapmns@motcid.UUCP (Simon ( Grouchy Babes ) Chapman) (05/23/91)

In article <1991May20.155136.25162@ux1.cso.uiuc.edu> ceblair@ux1.cso.uiuc.edu (Charles Blair) writes:
>
>   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
>creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
>Thanks in advance.  I'm sure I'm overlooking something well-known.

Surely executing this shell script will spawn a new process, and for the
process itself, the directory will alter, but the effects of the change
will not be exported to the parent process; the shell you run the script
from. I don't think that it is possible to do this using a script in the
way you mean, but two alternatives exist that I know of :

For c-shell: use an alias :

	alias	j	"cd /me/A/B/C/D"

For Bourne Shell: use a function :

	j() { cd /me/A/B/C/D ; }

Both of these do NOT spawn another process to do their stuff.

If anyone else can show us both how to do an "export cwd > parent.proc."
I'd be interested.

Cheers,
	thimon

---------------------------------------------------+---------------------------
Just 'cos you're not paranoid, doesn't mean        |       Simon Chapman 
    they're not out to get you.                    |     U.K. Motorola Cid.
                                                   |  ...uunet!motcid!chapmns
---------------------------------------------------+---------------------------

rmk@rmkhome.UUCP (Rick Kelly) (05/24/91)

In article <1991May20.201923.27920@garfield.ncat.edu> muquit@garfield.ncat.edu (MUHAMMAD A. MUQUIT) writes:
>In article <1991May20.155136.25162@ux1.cso.uiuc.edu> Charles Blair writes:
>>
>>   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
>>creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
>>Thanks in advance.  I'm sure I'm overlooking something well-known.
>
>You can do the job if you put this line in your .login file:
>     alias j 'cd /me/A/B/C/D'
>
>I'm also curious why your way didn't work.  I think  there're lots of gurus
>out there to answer this.

A two line script would do it.

#!/bin/sh
cd /me/A/B/C/D

Then chmod +x scriptname.

Rick Kelly	rmk@rmkhome.UUCP	frog!rmkhome!rmk	rmk@frog.UUCP

SCHDAVZ@YaleVM.YCC.Yale.Edu (Dave Schweisguth) (05/25/91)

>muquit@garfield.ncat.edu (MUHAMMAD A. MUQUIT) writes:
>>In article <1991May20.155136.25162@ux1.cso.uiuc.edu> Charles Blair writes:
>>>   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
>>>creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
>>>Thanks in advance.  I'm sure I'm overlooking something well-known.
>>
>>You can do the job if you put this line in your .login file:
>>     alias j 'cd /me/A/B/C/D'
>>I'm also curious why your way didn't work.  I think  there're lots of gurus
>>out there to answer this.
>
>        Alias is definitely the way to go here.  As to why the script-file
>solution did not work: Whenever you invoke a script-file, it creates its
>own shell.  Within this shell, it cd's to the right directory, but then
>the script-file process terminates and your shell is still sitting right
>where it was before.
 
But 'source' in csh stays in the same shell and needn't be 'chmod'ed, yes?
 _____________________________________________________________________________
/                                                                             \
|   Dave Schweisguth               5386 Yale Station           203-436-2694   |
|   schdavz@yalevm.ycc.yale.edu    New Haven, CT 06502-5386                   |
\_____________________________________________________________________________/

pfalstad@phoenix.princeton.edu (Paul Falstad) (05/27/91)

rmk@rmkhome.UUCP (Rick Kelly) wrote:
>A two line script would do it.
>
>#!/bin/sh
>cd /me/A/B/C/D
>
>Then chmod +x scriptname.

This is a joke, right?

A C program will do it, using everyone's favorite ioctl (since tty
security is a hot topic these days):

#include <sys/ioctl.h>
main()
{
char *s="cd /me/A/B/C/D\n";

   while(*s)
      ioctl(0,TIOCSTI,s++);
}
--
Paul Falstad                     | 10 PRINT "PRINCETON CS"
pfalstad@phoenix.princeton.edu   | 20 GOTO 10

subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (05/27/91)

In article <59921@rtfm.Princeton.EDU> pfalstad@phoenix.princeton.edu (Paul Falstad) writes:
>rmk@rmkhome.UUCP (Rick Kelly) wrote:
>>A two line script would do it.
>>
>>#!/bin/sh
>>cd /me/A/B/C/D
>>
>>Then chmod +x scriptname.
You wonder about these people that don't read threads, then followup with
the stupidest suggestions.

>This is a joke, right? 
Let's hope it is.

>A C program will do it, using everyone's favorite ioctl (since tty
>security is a hot topic these days):
>
>#include <sys/ioctl.h>
>main()
>{
>char *s="cd /me/A/B/C/D\n";
>
>   while(*s)
>      ioctl(0,TIOCSTI,s++);
>}

Oh, come come. A C program? tsk tsk, since we're talking about tty
security, we have to use a real hacker's tool -- perl:


#! /usr/princeton/bin/perl

require "ioctl.pl";

@s = split(//, "cd /me/A/B/C/D\n");

for (0..$#s) { ioctl(STDOUT, $TIOCSTI, $s[$_]); }


		-Kartik


--
internet% ypwhich

subbarao@phoenix.Princeton.EDU -| Internet
kartik@silvertone.Princeton.EDU (NeXT mail)  
SUBBARAO@PUCC.BITNET			          - Bitnet

michael@xzaphod.uucp (Michael R. Miller) (05/28/91)

In article <9105230900.22@rmkhome.UUCP> rmk@rmkhome.UUCP (Rick Kelly) writes:
>In article <1991May20.201923.27920@garfield.ncat.edu> muquit@garfield.ncat.edu (MUHAMMAD A. MUQUIT) writes:
>>In article <1991May20.155136.25162@ux1.cso.uiuc.edu> Charles Blair writes:
>>>
>>>   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
>>>creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
>>>Thanks in advance.  I'm sure I'm overlooking something well-known.
>>
>>You can do the job if you put this line in your .login file:
>>     alias j 'cd /me/A/B/C/D'
>>
>>I'm also curious why your way didn't work.  I think  there're lots of gurus
>>out there to answer this.
>
>A two line script would do it.
>
>#!/bin/sh
>cd /me/A/B/C/D
>
>Then chmod +x scriptname.
>
>Rick Kelly	rmk@rmkhome.UUCP	frog!rmkhome!rmk	rmk@frog.UUCP

A script won't work because "cd" is a "built in" command in every shell
I've heard of.

What happens when you run the script is your shell forks a copy of itself
which then runs the commands in the script.  The CHILD shell does the "cd"
command.  Then the CHILD shell exits.  The shell that "knew" about the
directory change has disappeared along with the directory change itself.
The parent process (shell) still has its working directory set as it was
prior to your script running.  Nothing constructive has happened except
another process has run and some files were opened and closed causing
updates to buffers to possibly occur inside the kernel.

A modification to the script command could be:

#!/bin/sh
exec $SHELL -c "cd /me/A/B/C/D"

assuming the SHELL environment variable is set to the path to the default
shell you want to use (eg. /bin/sh).  This is not the most efficient way
of doing this.  If your shell supports aliases (/bin/ksh, /bin/csh,
/bin/zsh), use it.

Michael R. Miller
uunet!xzaphod!michael

dts@quad.sialis.com (David Sandberg) (05/28/91)

In article <1991May27.170005.12870@xzaphod.uucp> michael@xzaphod.UUCP (Michael R. Miller) writes:
>A script won't work because "cd" is a "built in" command in every shell
>I've heard of.

Whether cd is builtin or not has nothing to do with the reason a
"cd script" won't work.  Rather, it's because each process has
it's own idea of what is the current working directory.  New
processes inherit the current working directory from their parent
process, but not visa versa.

I also have to wonder why you made the above statement when, in
the very next paragraph, you described why a "cd script" doesn't
work in an essentially correct fashion.  But then you got screwed
up again and closed your article with the following:

>A modification to the script command could be:
>
>#!/bin/sh
>exec $SHELL -c "cd /me/A/B/C/D"

Huh?  I hate to tell you this, but your modified script won't
work any better than the original, since you're still running the
cd command in a subshell.  In fact, your suggested change makes
it worse if anything... it accomplishes the same thing as the
other (nothing, that is) and takes more processes to do it.

>If your shell supports aliases (/bin/ksh, /bin/csh, /bin/zsh), use it.

Agreed.  I said as much a week ago via email to the original
person who posed the question, thinking that it was too obvious
of a question to post a public reply to.  Heh... I should've
known better, I guess.

-- 
 \*=-      David Sandberg, dts@quad.sialis.com    ,=,       ,=,        -=*\
  \*=-   "like words whispered by waking ghosts   | |uadric `=,ystems   -=*\
   \*=-   that in my ears muttered" - Torhthelm   `=\       `='          -=*\

rmk@rmkhome.UUCP (Rick Kelly) (05/29/91)

In article <1991May27.170005.12870@xzaphod.uucp> michael@xzaphod.UUCP (Michael R. Miller) writes:
>In article <9105230900.22@rmkhome.UUCP> rmk@rmkhome.UUCP (Rick Kelly) writes:
>>In article <1991May20.201923.27920@garfield.ncat.edu> muquit@garfield.ncat.edu (MUHAMMAD A. MUQUIT) writes:
>>>In article <1991May20.155136.25162@ux1.cso.uiuc.edu> Charles Blair writes:
>>>>
>>>>   I would like to get to a directory /me/A/B/C/D by just typing j.  I tried
>>>>creating a file j with cd /me/etc in it, then chmod +x j.  It didn't work.
>>>>Thanks in advance.  I'm sure I'm overlooking something well-known.
>>>
>>>You can do the job if you put this line in your .login file:
>>>     alias j 'cd /me/A/B/C/D'
>>>
>>>I'm also curious why your way didn't work.  I think  there're lots of gurus
>>>out there to answer this.
>>
>>A two line script would do it.
>>
>>#!/bin/sh
>>cd /me/A/B/C/D
>>
>>Then chmod +x scriptname.
>>
>>Rick Kelly	rmk@rmkhome.UUCP	frog!rmkhome!rmk	rmk@frog.UUCP
>
>A script won't work because "cd" is a "built in" command in every shell
>I've heard of.
>
>What happens when you run the script is your shell forks a copy of itself
>which then runs the commands in the script.  The CHILD shell does the "cd"
>command.  Then the CHILD shell exits.  The shell that "knew" about the
>directory change has disappeared along with the directory change itself.
>The parent process (shell) still has its working directory set as it was
>prior to your script running.  Nothing constructive has happened except
>another process has run and some files were opened and closed causing
>updates to buffers to possibly occur inside the kernel.
>
>A modification to the script command could be:
>
>#!/bin/sh
>exec $SHELL -c "cd /me/A/B/C/D"
>
>assuming the SHELL environment variable is set to the path to the default
>shell you want to use (eg. /bin/sh).  This is not the most efficient way
>of doing this.  If your shell supports aliases (/bin/ksh, /bin/csh,
>/bin/zsh), use it.

As I hit 's', I realised this.  I canceled from within rn, but it didn't
seem to work.  So I guess I will have to debug canceling articles. :-)

Rick Kelly	rmk@rmkhome.UUCP	frog!rmkhome!rmk	rmk@frog.UUCP

vlb@apple.com (06/04/91)

In [several articles] [several people] write
>>>>>   I would like to get to a directory /me/A/B/C/D by just typing j.

If you are using csh (or tcsh) look up cdpath.  For example, I need to get to
the directory
        /d4/oreo/install/3.0
fairly regularly.  My cdpath contains "/d4/oreo/install" and I just use the
command "cd 3.0" to get to where I want to go.

Since you can also use (no wildcards) subdirectories, this can be quite useful:
       cd 3.0/src
        cd 3.0/tst
all with only 1 definition

You may find this to be useful...
   Vicki Brown   A/UX Development Group		Apple Computer, Inc.
   Internet: vlb@apple.com			MS 50UX, 10300 Bubb Rd.	
   UUCP: {sun,amdahl,decwrl}!apple!vlb		Cupertino, CA  95014 USA
              Ooit'n Normaal Mens Ontmoet?  En..., Beviel't?
        (Have you ever met a normal person?  So..., did you like it?)

jrj@Spies.COM (Richard Jones) (06/04/91)

In article <13862@goofy.Apple.COM> vlb@apple.COM (Vicki Brown) writes:
>In [several articles] [several people] write
>>>>>>   I would like to get to a directory /me/A/B/C/D by just typing j.
>
>If you are using csh (or tcsh) look up cdpath.  For example, I need to get to
>the directory
>        /d4/oreo/install/3.0
>fairly regularly.  My cdpath contains "/d4/oreo/install" and I just use the
>command "cd 3.0" to get to where I want to go.
>
>Since you can also use (no wildcards) subdirectories, this can be quite useful:
>       cd 3.0/src
>        cd 3.0/tst
>all with only 1 definition
>
>You may find this to be useful...
>   Vicki Brown   A/UX Development Group		Apple Computer, Inc.
>   Internet: vlb@apple.com			MS 50UX, 10300 Bubb Rd.	
>   UUCP: {sun,amdahl,decwrl}!apple!vlb		Cupertino, CA  95014 USA
>              Ooit'n Normaal Mens Ontmoet?  En..., Beviel't?
>        (Have you ever met a normal person?  So..., did you like it?)

The first thing that comes to mind is the use of ALIAS.  

Why not use "alias j 'cd /me/A/B/C/D'" ?

Then, by typing j<CR> from anywhere, you are cd'd into D.

Just a thought.

guy@auspex.auspex.com (Guy Harris) (06/05/91)

>If you are using csh (or tcsh) look up cdpath.

And if you're using a sufficiently modern Bourne shell, or a Korn shell,
or probably the Bourne-again shell, and probably most other
publicly-available rewrites of Bourne-flavored shells, look up CDPATH;
it works similarly to the C shell's "cdpath".