Implementing ! functionality for Korn shell

This is a discussion on Implementing ! functionality for Korn shell within the Tech Board forums, part of the Community Boards category; Hello, I'm trying to implement the ! operator from the Bourne shell for Korn shell. The ! operator allows you ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    Implementing ! functionality for Korn shell

    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

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    This is driving me nuts. Here is what I changed in the last while loop:
    Code:
     while (($i < ${#hist[*]})) do
      `perl -e 'exit "${hist[$i]}" =~ /^$1/'`;
    
      if (($?==1)) then
       print ${hist[$i]};
       ${hist[$i]};
       return $?;
      fi
    
      ((i=$i+1));
     done
    This always executes the last history item. Also, I would prefer not to use Perl.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 01:26 PM
  2. What shell is more powerful?
    By xddxogm3 in forum Tech Board
    Replies: 10
    Last Post: 07-24-2004, 11:47 PM
  3. Implementing ! functionality for Korn shell
    By Yasir_Malik in forum Linux Programming
    Replies: 1
    Last Post: 06-14-2004, 09:24 PM
  4. System.ini Shell Problems
    By (TNT) in forum Windows Programming
    Replies: 2
    Last Post: 08-26-2001, 02:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21