2 # Chet Ramey <chet.ramey@case.edu>
4 # Copyright 2002 Chester Ramey
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
11 # TThis program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # substr -- a function to emulate the ancient ksh builtin
23 # -l == remove shortest from left
24 # -L == remove longest from left
25 # -r == remove shortest from right (the default)
26 # -R == remove longest from right
31 local usage="usage: substr -lLrR pat string or substr string pat"
32 local options="l:L:r:R:"
35 while getopts "$options" c
49 if [ "$OPTIND" -gt 1 ] ; then
50 shift $(( $OPTIND -1 ))
53 if [ "$#" -eq 0 ] || [ "$#" -gt 2 ] ; then
54 echo "substr: bad argument count"
61 # We don't want -f, but we don't want to turn it back on if
62 # we didn't have it already
75 str="${str#$pat}" # substr -l pat string
78 str="${str##$pat}" # substr -L pat string
81 str="${str%$pat}" # substr -r pat string
84 str="${str%%$pat}" # substr -R pat string
87 str="${str%$2}" # substr string pat
94 # If we had file name generation when we started, re-enable it
96 if [ "$fng" = "1" ] ; then