Hello,
No one answered this on the Tech Board, so I thought I'd post it here.
I'm trying to implement the ! operator from the Bourne shell for Korn shell. The ! operator allows you to execute the last command entered beginning with the word after !. Here's what I have so far. Note: I know that fc -s would allow me to implement the functionality of !, but I did not know about fc before writing this. I figured I might as well go through with this.
This works for things like "_ ls", but it won't work for something like "_ l", which would find the first command beginning with "l" and execute it. I tried the following, but it always executes the last command:Code:#!/bin/ksh _() { i=0; tmp=`history`; if (($?==0)) then list=`history | cut -f2`; #sequence to store each line in an array while [[ -n $list ]] do temp2=$temp; temp=${list% *}; #last line does not have \n if [[ $temp2 == $temp ]] then hist[$i]=$temp; list=""; else hist[$i]=${list#$temp }; list=$temp; ((i=$i+1)); fi done else print "ksh: _: no history (yet)"; fi ((i=0)); while (($i < ${#hist[*]})) do if [[ "${hist[$i]}" == "$1" ]] then print "here: ${hist[$i]}"; ${hist[$i]}; return $?; fi ((i=$i+1)); done print "ksh: _: $1: not in history"; return 1; }
I would prefer not to use perl, just the shells simple globbing rules. I am using pdksh that comes with NetBSD.Code:while (($i < ${#hist[*]})) do tmp="\"${hist[$i]}\""; `perl -e 'exit $tmp =~ /^$1/'`; if (($?==1)) then # if [[ "${hist[$i]}" == "$1" ]] then print ${hist[$i]}; ${hist[$i]}; return $?; fi ((i=$i+1)); done
Thanks,
Yasir



LinkBack URL
About LinkBacks


