[comp.unix.questions] Changing directories in ksh...

pepper@dasys1.UUCP (Angeli "Ms. Pepper" Wahlstedt) (07/04/88)

Normally, I'd have an alias in "csh" that looks like this:

		alias down 'cd \!^; ls'

What it does is move to a subdirectory and prints it out. (I like to "see"
where I'm going, so to speak. :-)

But, here's the fun part...how do I do the same thing in ksh?? Shell
scripts ain't the answer, because once I get to a new directory, I want
to STAY there. Also, ksh's alias isn't as...ummm...err..."intelligent"
as csh's alias.


-- 
Angeli "Ms. Pepper" Wahlstedt

Big Electric Cat: ...!{sun!hoptoad,cmcl2!phri}!dasys1!pepper
Portal: ms-pepper@cup.portal.com

lvc@tut.cis.ohio-state.edu (Lawrence V. Cipriani) (07/04/88)

In article <5318@dasys1.UUCP> pepper@dasys1.UUCP (Angeli "Ms. Pepper" Wahlstedt) writes:
>
>Normally, I'd have an alias in "csh" that looks like this:
>
>		alias down 'cd \!^; ls'
>
>But, here's the fun part...how do I do the same thing in ksh?? Shell
>scripts ain't the answer, because once I get to a new directory, I want
>to STAY there. Also, ksh's alias isn't as...ummm...err..."intelligent"
>as csh's alias.

This is a weakness in ksh, but there is a work around.  Use a ksh
function:

	function down
	{
		cd "${@}" ; ls
	}

and put it in your .profile or ENV file.  You could put quote marks
around the cd and ls if you don't want alias processing on them.


-- 
Larry Cipriani, AT&T Network Systems and Ohio State University
Domain: lvc@tut.cis.ohio-state.edu
Path: ...!cbosgd!osu-cis!tut.cis.ohio-state.edu!lvc (strange but true)

wcs@skep2.ATT.COM (Bill.Stewart.[ho95c]) (07/05/88)

In article <5318@dasys1.UUCP> pepper@dasys1.UUCP (Angeli "Ms. Pepper" Wahlstedt) writes:
> Normally, I'd have an alias in "csh" that looks like this:
> 		alias down 'cd \!^; ls'
> But, here's the fun part...how do I do the same thing in ksh?? ...
> Also, ksh's alias isn't as...ummm...err..."intelligent" :as csh's alias.

Either you convince alias to give you access to arguments (i.e. read
the manual which I don't have here), or use shell functions
	down(){
		cd $* ; ls -FC
	}
Shell functions are an SVR2 shell feature which ksh also supports.
A side benefit is that if you find yourself on a system that doesn't
have ksh,  shell functions can let you create many of your favorite aliases.

The really fun part is if you want your command name to be "cd".
It's possible, but you have to do just the right things with
order-of-evaluation differences between built-ins, aliases, and
functions, and go three or so layers deep.
-- 
#				Thanks;
# Bill Stewart, AT&T Bell Labs 2G218, Holmdel NJ 1-201-949-0705 ihnp4!ho95c!wcs
Rnmail: .signature too boring - deleted

gwyn@brl-smoke.ARPA (Doug Gwyn ) (07/05/88)

In article <5318@dasys1.UUCP> pepper@dasys1.UUCP (Angeli "Ms. Pepper" Wahlstedt) writes:
>		alias down 'cd \!^; ls'
>But, here's the fun part...how do I do the same thing in ksh??

Since ksh is advertised as being compatible with the Bourne shell,
I would simply define a shell function:
	down(){ cd $1; ls; }

jewett@hpl-opus.HP.COM (Bob Jewett) (07/06/88)

> I would simply define a shell function:
> 	down(){ cd $1; ls; }

Almost right.  If the cd fails, you don't want to do the ls.  The (k)shell
function I use for this is:

   down() { cd $1 && /bin/ls -sF ; }

Bob Jewett