[comp.unix.shell] printing ksh history a la csh

wohler@sapwdf.UUCP (Bill Wohler) (02/12/91)

folks,

  in ksh, i'd like to say "history 30" to print the last 30 commands as
  in csh.  if you know how to do this, i would be obliged.

  the problem is that the "fc -l" command optionally asks for the first 
  and last command of your list.  there aren't any variables that i
  know of that contain the current history number which one could subtract
  30 from and pass to fc -l.

  another "feature" of our ksh is that if you specify a starting
  number that is too low, instead of printing just what it knows, it
  prints the following useful message (are you listening, ibm?):

    /bin/ksh: fc: 0403-009 The specified number is not valid for this command.

  thus, such a history alias would have to ensure that it doesn't try
  to print commands $HISTSIZE commands ago.

						--bw
						wohler@sap-ag.de

mullins@convex.COM (Don Mullins) (02/13/91)

In article <2582@sapwdf.UUCP> Bill Wohler <wohler@sap-ag.de> writes:
>folks,
>
>  in ksh, i'd like to say "history 30" to print the last 30 commands as
>  in csh.  if you know how to do this, i would be obliged.
>
>  the problem is that the "fc -l" command optionally asks for the first 
>  and last command of your list.  there aren't any variables that i
>  know of that contain the current history number which one could subtract
>  30 from and pass to fc -l.
>
>						--bw
>						wohler@sap-ag.de

    I think the -<num> option is what you want (transcript follows..)
------------------------ PROMPT IS '$' -------------------------------
$ alias history
history=fc -l
$ history -30
15146   cls
15147   lf
.....   [deleted for brevity -dwm]
15172   lf
15173   trn
15174   history -20
15175   alias history
15176   history -30
$ what /bin/ksh
/bin/ksh
	Version 11/16/88
	sleep.c 4.6 (Berkeley) 9/11/83
$

----------------------------------------------------------------------

Don

--
Don Mullins          Convex Computer Corporation          Richardson, Texas USA
INTERNET: mullins@convex.COM -- UUCP: {uiucuxc, uunet, sun, ...}!convex!mullins
              Mullins' 1st Law of Statistical Presentation: 
"Never present numbers as an integer value, people won't believe they're valid."

lvc@cbnews.att.com (Lawrence V. Cipriani) (02/13/91)

In article <2582@sapwdf.UUCP> Bill Wohler <wohler@sap-ag.de> writes:
>folks,
>
>  in ksh, i'd like to say "history 30" to print the last 30 commands as
>  in csh.  if you know how to do this, i would be obliged.

	$ history -30

>  another "feature" of our ksh is that if you specify a starting
>  number that is too low, instead of printing just what it knows,

This has been a sore point with me for a long time as well.  I have
asked Korn to change ksh so that it would do this but it was not done.

You could do something like this:

	ncmd="`history -1 | sed -e 's/[ 	].*//' -e 1d`"
	if [ "$ncmd" -gt "$HISTSIZE" ]
	then
		ncmd=$HISTSIZE
	fi
	history -$ncmd

That's the basic idea at least.
-- 
Larry Cipriani, att!cbvox!lvc or lvc@cbvox.att.com
"Fight fire with fire, I always say" -- Bugs Bunny