Hello,
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.
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;
}
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.
Thanks,
Yasir