🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
3 messages
3 total messages Started by Ben Wed, 03 Jun 2009 07:44
Get previous command history number from a script
#4008
Author: Ben
Date: Wed, 03 Jun 2009 07:44
12 lines
1758 bytes
I've already figured out a way to do this but I suspect there are
simpler/faster/more elegant solutions that exist.

The objective is to get the command history number of the command that
was just executed, but do it from a script (which seems to prevent
access to the $HISTCMD variable, which would've been nice; any
thoughts on this?). Here's my solution:

history 2| sed -n 1p| sed -e "s/^\s*\([0-9]\+\).*/\1/";

Notice the (command complexity) / (problem complexity) ratio. Can
anyone improve it?
Re: Get previous command history number from a script
#4009
Author: hanzhengxu
Date: Wed, 03 Jun 2009 09:10
16 lines
2013 bytes
On Jun 3, 3:44 pm, Ben <bgol...@gmail.com> wrote:
> I've already figured out a way to do this but I suspect there are
> simpler/faster/more elegant solutions that exist.
>
> The objective is to get the command history number of the command that
> was just executed, but do it from a script (which seems to prevent
> access to the $HISTCMD variable, which would've been nice; any
> thoughts on this?). Here's my solution:
>
> history 2| sed -n 1p| sed -e "s/^\s*\([0-9]\+\).*/\1/";
>
> Notice the (command complexity) / (problem complexity) ratio. Can
> anyone improve it?

Any good usages for history number in scripts?
It seems should use 'set -o history' to enable history in a script.
Re: Get previous command history number from a script
#4011
Author: Scott Lurndal
Date: Wed, 03 Jun 2009 14:57
23 lines
1803 bytes
Ben <bgold12@gmail.com> writes:
>I've already figured out a way to do this but I suspect there are
>simpler/faster/more elegant solutions that exist.
>
>The objective is to get the command history number of the command that
>was just executed, but do it from a script (which seems to prevent
>access to the $HISTCMD variable, which would've been nice; any
>thoughts on this?). Here's my solution:
>
>history 2| sed -n 1p| sed -e "s/^\s*\([0-9]\+\).*/\1/";
>
>Notice the (command complexity) / (problem complexity) ratio. Can
>anyone improve it?

Note that if you have multiple shells open, they'll be sharing the
same history file.    Depending on why you want the last command
executed, this could be a problem.

You can arrange in your .profile/.kshrc/.bashrc file for each shell
to have a different history file by setting the HISTFILE environment
variable to a session-unique name.

scott
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads