[comp.unix.shell] Help with this script

danabu@garnet.berkeley.edu (Daniel N. Abushanab) (04/09/91)

Hi,
I'm trying to write a script to emulate the DOS Norton Utilities "ncd" 
command.  It should scan a directory tree file, find a directory that
matches a pattern inputted by the user, then exit.  The following shell
accomplishes that job.  Unfortunately, when the shell is exited it
returns the user to the directory from which it was called.  Does anyone
have any idea about how to fix this?  Any help is appreciated, please 
respond by e-mail and I will post solution here.

Shell script follows:

#This is a shell script to scan your directory structure and change
#to a directory matching a pattern you input.  To use type "acd
#pattern".  "acd ..." rebuilds the tree structure.

#This changes the directory, but upon exiting the shell the 
#program returns to its starting directory.  I want it to
#stay where it is upon exiting.

if (test $1) then
	if (test $1 = "...") then
	  echo "creating tree file..." 
	  /bin/ls -R $HOME | grep : | awk -F: '{print $1}' > $HOME/tree.abu
	  exit
	fi
	DIRECTORY=`grep "$HOME.*/$1" $HOME/tree.abu`
	FIRST=`echo $DIRECTORY | awk '{print $1}'`
	if (test $FIRST) then
	  echo "Changing to $FIRST"
	  cd $FIRST
	else echo "Could not find $1."
	fi

else echo "You must include a directory."
fi

----------------------------------------------------------------------------
Daniel N. Abushanab		          University of California, Berkeley
E-mail:  danabu@scorpio.berkeley.edu       Mechanical Engineering Department
phone:  (415) 642-5109  
----------------------------------------------------------------------------
Daniel N. Abushanab		          University of California, Berkeley
E-mail:  danabu@garnet.berkeley.edu       Mechanical Engineering Department
phone:  (415) 642-5109  

rgupta@leland.Stanford.EDU (Rajesh Gupta) (04/10/91)

In article <1991Apr9.164257.9128@agate.berkeley.edu> danabu@garnet.berkeley.edu (Daniel N. Abushanab) writes:
>
>Hi,
>I'm trying to write a script to emulate the DOS Norton Utilities "ncd" 
>command.  It should scan a directory tree file, find a directory that
>matches a pattern inputted by the user, then exit.  The following shell
>accomplishes that job.  Unfortunately, when the shell is exited it
>returns the user to the directory from which it was called.  Does anyone
>have any idea about how to fix this?  Any help is appreciated, please 
>respond by e-mail and I will post solution here.
>
...


How about:

	cd `find . -name <argument> -type d -print` 



This should do the job.



Rajesh Gupta

rgupta@sirius.stanford.edu

rouben@math13.math.umbc.edu (Rouben Rostamian) (04/10/91)

In article <1991Apr9.164257.9128@agate.berkeley.edu> danabu@garnet.berkeley.edu (Daniel N. Abushanab) writes:
 >
 >Hi,
 >I'm trying to write a script to emulate the DOS Norton Utilities "ncd" 
 >command.  It should scan a directory tree file, find a directory that
 >matches a pattern inputted by the user, then exit.  The following shell
 >accomplishes that job.  Unfortunately, when the shell is exited it
 >returns the user to the directory from which it was called.  Does anyone
 >have any idea about how to fix this?  Any help is appreciated, please 
 >respond by e-mail and I will post solution here.
 >
 >[Shell script deleted]:
 >

Try

cd `find . -name 'pattern' -type d -print'`

where "pattern" is a directory name, possibly incuding wildcards.

--
Rouben Rostamian                          Telephone: (301) 455-2458
Department of Mathematics and Statistics  e-mail:
University of Maryland Baltimore County   bitnet: rostamian@umbc.bitnet
Baltimore, MD 21228,  U.S.A.              internet: rouben@math9.math.umbc.edu

rhartman@thestepchild.sgi.com (Robert Hartman) (04/10/91)

In article <1991Apr9.175008.12044@leland.Stanford.EDU> rgupta@leland.Stanford.EDU (Rajesh Gupta) writes:
>
>How about:
>
>	cd `find . -name <argument> -type d -print` 

Ooooh!  I like this!  Just cooked a version for csh:

alias jd  'set arg="\!:1"; cd `(find . -name "*${arg}*" -type d -print) \
    | head -1`' # jump to a directory

You have to use a variable to capture the alias argument, because the
history (argument) substitution syntax collides with the trailing *
in the argument to -name.  I want to go to the first match, so the head
command is necessary to avoid "Ambiguous" errors.  I have "," aliased to
pop me back to the previous directory if find guesses wrong.

-r

hendrik@cca.vu.nl (Hendrik te Winkel) (04/10/91)

rgupta@leland.Stanford.EDU (Rajesh Gupta) writes:

>In article <1991Apr9.164257.9128@agate.berkeley.edu> danabu@garnet.berkeley.edu (Daniel N. Abushanab) writes:
>>
>>Hi,
>>I'm trying to write a script to emulate the DOS Norton Utilities "ncd" 
>>command.  It should scan a directory tree file, find a directory that
>>matches a pattern inputted by the user, then exit.  The following shell
>>accomplishes that job.  Unfortunately, when the shell is exited it
>>returns the user to the directory from which it was called.  Does anyone
>>have any idea about how to fix this?  Any help is appreciated, please 
>>respond by e-mail and I will post solution here.


>How about:

>	cd `find . -name <argument> -type d -print` 
>This should do the job.

Not really, again when you put this into a file it will change your
directory but after the filescript finishes you'll discover
that you are again in the original directory.
Of course you could alias it in csh.
But now some real answer from a guru please! Is it really impossible
to change your working dir with a shell script _and_ to remain there
after it is finished? I don't know how to do it. Please inform.

Hendrik

-- 
handtekeningetje

eravin@panix.uucp (Ed Ravin) (04/10/91)

If you've got ksh to play with, check out the CDPATH variable.  This
will let you select a list of directories to search when you say "cd foo".

For example:

$ CDPATH=".:/usr/mom:/usr/dad:/home"

and then 

$ cd foo

Will search, in order, for ./foo, /usr/mom/foo, /usr/dad/foo, /home/foo
and whichever one it finds first it will cd to (and echo the directory
name it chose).

ksh also gives you built in pattern substitution on the cd command.  So
if your current directory is

/usr/mom/foo/bar/rastafarian/lefthanded/mugglethworp

And you say:

$ cd rastafarian unitarian

ksh will attempt to cd to:

/usr/mom/foo/bar/unitarian/lefthanded/mugglethworp

With all those features, if you can switch to ksh (or are already
using it), you might not want to even bother with "ncd" emulation.
-- 
Ed Ravin            | This random number tells the computer that you are 
cmcl2!panix!eravin  | a member in good standing.  It is not related to your
philabs!trintex!elr | membership number.   --- Sierra Club

ken@racerx.UUCP (Ken Hardy) (04/11/91)

>How about:
>
>	cd `find . -name <argument> -type d -print`

If 'find' finds more than one directory that matches the pattern, you're in trouble.
How about:
	cd `find . -name <argument> -type d -print | head -1`
  or
	cd `find . -name <argument> -type d -print | tail -1`
	

-- 
Ken Hardy		uunet!racerx!ken		ken@racerx.UUCP

asg@sage.cc.purdue.edu (Bruce Varney) (04/11/91)

In article <1991Apr10.140153.480@cca.vu.nl> hendrik@cca.vu.nl (Hendrik te Winkel) writes:
}rgupta@leland.Stanford.EDU (Rajesh Gupta) writes:
}
}>>
}>>Hi,
}>>I'm trying to write a script to emulate the DOS Norton Utilities "ncd" 
}>>accomplishes that job.  Unfortunately, when the shell is exited it
}>>returns the user to the directory from which it was called.  Does anyone
}>How about:
}
}>	cd `find . -name <argument> -type d -print` 
}>This should do the job.
}
}Not really, again when you put this into a file it will change your
}directory but after the filescript finishes you'll discover
}that you are again in the original directory.
}Of course you could alias it in csh.
}But now some real answer from a guru please! Is it really impossible
}to change your working dir with a shell script _and_ to remain there
}after it is finished? I don't know how to do it. Please inform.
}Hendrik
Why do you wnant to do it in a shell script?????????
Try using the CDPATH (or cdpath for csh junkies) variable.
---------
                                   ###             ##
Courtesy of Bruce Varney           ###               #
aka -> The Grand Master                               #
asg@sage.cc.purdue.edu             ###    #####       #
PUCC                               ###                #
;-)                                 #                #
;'>                                #               ##

asg@sage.cc.purdue.edu (Bruce Varney) (04/11/91)

In article <1991Apr10.154927.23360@panix.uucp> eravin@panix.uucp (Ed Ravin) writes:
}If you've got ksh to play with, check out the CDPATH variable.  This
}will let you select a list of directories to search when you say "cd foo".
EVERY DAMN SHELL HAS CDPATH!!! although for csh and tcsh the syntax is
set cdpath (. /usr/mom /usr/dad /homw)
}
}$ CDPATH=".:/usr/mom:/usr/dad:/home"
}
}and then 
}
}$ cd foo
}
}Will search, in order, for ./foo, /usr/mom/foo, /usr/dad/foo, /home/foo
}and whichever one it finds first it will cd to (and echo the directory
}name it chose).
}
Course there is also th cdable_vars variable in bash. When it is set you 
can do:
cdable_vars=on
src=$HOME/etc/src
then when you do
$ cd src
it will look for src in your CDPATH. i.e. ./src, /usr/mom/src. /usr/dad/src
and /home/src. If none of these exist, it will then cd to $src 
This is useful IMHO

			The  Grand Master
---------
                                   ###             ##
Courtesy of Bruce Varney           ###               #
aka -> The Grand Master                               #
asg@sage.cc.purdue.edu             ###    #####       #
PUCC                               ###                #
;-)                                 #                #
;'>                                #               ##

tchrist@convex.COM (Tom Christiansen) (04/11/91)

From the keyboard of hendrik@cca.vu.nl (Hendrik te Winkel):
:Is it really impossible
:to change your working dir with a shell script _and_ to remain there
:after it is finished? I don't know how to do it. Please inform.

This could be rephrased "Is it really impossible to change another
process's environment without that process's cooperation?".  This answer
is generally, yes, this is really impossible.  

There are a few exceptions.  If it's your own process, you may be able to
change its nice value or send it a signal, but you're not going to be able
to change its environment variables, its umask, or its working directory.
Well, modulo a kernel dive:  with kmem write privs, you could munge that
process's u_cdir or u_cmask while it's not looking, but don't tell anyone
I said that. :-)

Much better solutions involve using shell aliases or functions, since they
operate in the same process as the calling shell.  A less good solution
would be to use TIOCSTIs if you have them to jam the right command into
your input buffer so that it's the next thing that your shell reads.

--tom

rhartman@thestepchild.sgi.com (Robert Hartman) (04/11/91)

In article <1991Apr10.140153.480@cca.vu.nl> hendrik@cca.vu.nl (Hendrik te Winkel) writes:
>
>>	cd `find . -name <argument> -type d -print` 
>
>Not really, again when you put this into a file it will change your
>directory but after the filescript finishes you'll discover
>that you are again in the original directory.
>Of course you could alias it in csh.
>But now some real answer from a guru please! Is it really impossible
>to change your working dir with a shell script _and_ to remain there
>after it is finished? I don't know how to do it. Please inform.
>
>Hendrik

I'm not a guru, but I already posted answers to this.  I'll summarize
one last time.  If you want the current shell to execute a script, you
have to tell it to specifically.  If you simply invoke a script on the
command line, that script runs in a child process--same as any other
command.  To get the current shell to execute commands from a file (other
than the tty):

1.  Use the "." command to tell the shell to execute commands from the file
    given as its argument.

2.  Make the script into a shell function.

3.  Define a shell function that uses the "." command to interpolate the text
    of an existing script:

	ncd() {
	    . $HOME/bin/ncd
	}

4.  Or, if you can do the job with a one-liner, just do it:

	ncd() {
	    cd `find . -name "*${1}*" -type d -print` ; pwd
	}

Try it!

-r

rgupta@leland.Stanford.EDU (Rajesh Gupta) (04/11/91)

In article <1991Apr10.140153.480@cca.vu.nl> hendrik@cca.vu.nl (Hendrik te Winkel) writes:
>rgupta@leland.Stanford.EDU (Rajesh Gupta) writes:
>
>>In article <1991Apr9.164257.9128@agate.berkeley.edu> danabu@garnet.berkeley.edu (Daniel N. Abushanab) writes:
>>>
>>>Hi,
>>>I'm trying to write a script to emulate the DOS Norton Utilities "ncd" 
...
>
>
>>How about:
>
>>	cd `find . -name <argument> -type d -print` 
>>This should do the job.
>
>Not really, again when you put this into a file it will change your
>directory but after the filescript finishes you'll discover
>that you are again in the original directory.
>Of course you could alias it in csh.
>But now some real answer from a guru please! Is it really impossible
>to change your working dir with a shell script _and_ to remain there
>after it is finished? I don't know how to do it. Please inform.


no it is not possible. When running the shell as a child process, 
the parent environment can not be changed by the child with
pre-meditation  on part of the parent.



Rajesh Gupta
rgupta@sirius.stanford.edu

jik@athena.mit.edu (Jonathan I. Kamens) (04/11/91)

In article <1991Apr10.140153.480@cca.vu.nl>, hendrik@cca.vu.nl (Hendrik te Winkel) writes:
|> But now some real answer from a guru please! Is it really impossible
|> to change your working dir with a shell script _and_ to remain there
|> after it is finished? I don't know how to do it. Please inform.

  It doesn't take "a guru" to answer this question, since it is answered in
the monthly comp.unix.questions FAQ posting, in question number 14.  People
should read that article before posting to the comp.unix.* newsgroups.  If the
article has expired at your site, you can retrieve it using the instructions
at the end of this posting.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710
-- 
        Subject: Frequently Asked Questions about Unix - with Answers [Monthly posting]
        Newsgroups: comp.unix.questions

        Available via anonymous ftp from pit-manager.mit.edu (18.72.1.58)
        in the file

        /pub/usenet/comp.unix.questions/Frequently_Asked_Questions_about_Unix_-_with_Answers_[Monthly_posting]

        Available from mail-server@pit-manager.mit.edu by sending a message
        containing

        send usenet/comp.unix.questions/Frequently_Asked_Questions_about_Unix_-_with_Answers_[Monthly_posting]

        Send a message containing "help" to get general information about the
        mail server.

tif@doorstop.austin.ibm.com (Paul Chamberlain) (04/11/91)

In article <560@racerx.UUCP> ken@racerx.UUCP (Ken Hardy) writes:
>>	cd `find . -name <argument> -type d -print`
>If 'find' finds more than one directory that matches the pattern,
>you're in trouble.

If you do it this way, you'll either change directories and
print the directory you went to, or get an error changing
directories and display the list of directories found.
The only time this is awkward is if one directory was
found but the cd failed (i.e. mkdir x; chmod 0 x; fcd x).

function fcd {
	typeset dirs
	dirs=`find . -name "*${1}*" -type d -print`
	cd $dirs
	echo $dirs | tr ' ' '\012'
	}

Paul Chamberlain | I do NOT speak for IBM.          IBM VNET: PAULCC AT AUSTIN
512/838-9748     | ...!cs.utexas.edu!ibmchs!auschs!doorstop.austin.ibm.com!tif