2 # substr -- a function to emulate the ancient ksh builtin
5 # -l == remove shortest from left
6 # -L == remove longest from left
7 # -r == remove shortest from right (the default)
8 # -R == remove longest from right
13 local usage="usage: substr -lLrR pat string or substr string pat"
14 local options="l:L:r:R:"
17 while getopts "$options" c
31 if [ "$OPTIND" -gt 1 ] ; then
35 if [ "$#" -eq 0 ] || [ "$#" -gt 2 ] ; then
36 echo "substr: bad argument count"
43 # We don't want -f, but we don't want to turn it back on if
44 # we didn't have it already
57 str="${str#$pat}" # substr -l pat string
60 str="${str##$pat}" # substr -L pat string
63 str="${str%$pat}" # substr -r pat string
66 str="${str%%$pat}" # substr -R pat string
69 str="${str%$2}" # substr string pat
76 # If we had file name generation when we started, re-enable it
78 if [ "$fng" = "1" ] ; then