2 # .kshenv -- functions and aliases to provide the beginnings of a ksh
3 # environment for bash.
9 # These are definitions for the ksh compiled-in `exported aliases'. There
10 # are others, but we already have substitutes for them: "history", "type",
14 alias functions="typeset -f"
15 alias integer="typeset -i"
17 alias command="command "
18 alias stop="kill -s STOP"
19 alias redirect="command exec"
23 # An almost-ksh compatible `whence' command. This is as hairy as it is
24 # because of the desire to exactly mimic ksh (whose behavior was determined
27 # This depends somewhat on knowing the format of the output of the bash
28 # `builtin type' command.
33 local vflag pflag fflag defarg c
36 vflag= aflag= pflag= fflag=
38 if [ "$#" = "0" ] ; then
39 echo "whence: usage: whence [-afpv] name..." >&2
44 while getopts "avfp" c
51 ?) echo "whence: $1: unknown option" >&2
52 echo "whence: usage: whence [-afpv] name..." >&2
57 shift $(( $OPTIND - 1 ))
59 if [ "$#" = "0" ] ; then
60 echo "whence: usage: whence [-afpv] name..." >&2
66 if [ "$vflag" ] ; then
67 if [ -z "$defarg" ]; then
68 builtin type $cmd | sed 1q
70 if builtin type $defarg -t $cmd | grep 'function$' >/dev/null 2>&1; then
71 # HAIRY awk script to suppress
72 # printing of function body -- could
73 # do it with sed, but I don't have
75 builtin type $defarg $cmd | awk '
77 $1 == "'$cmd'" && $2 == "()" {printit=0; next; }
78 /^}$/ { if (printit == 0) printit=1 ; else print $0; next ; }
79 /.*/ { if (printit) print $0; }'
81 builtin type $defarg $cmd
85 path=$(builtin type $defarg -p $cmd)
91 *) case "$(builtin type -t $cmd)" in
104 # For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
115 0) builtin cd "$HOME" ;;
116 1) builtin cd "$@" ;;
119 # dir=$(echo "$PWD" | sed "s:$old:$new:g")
120 dir=${PWD//$old/$new}
122 "$PWD") case "$PWD" in
124 *) echo "$FUNCNAME: bad substitution" >&2 ; return 1 ;;
131 *) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
137 # ksh print emulation
139 # print [-Rnprsu[n]] [-f format] [arg ...]
142 # -R BSD-style -- only accept -n, no escapes
143 # -n do not add trailing newline
144 # -p no-op (no coprocesses)
146 # -s print to the history file
147 # -u n redirect output to fd n
148 # -f format printf "$format" "$@"
154 local nflag= fflag= c
158 while getopts "fRnprsu:" c
170 shift $(( $OPTIND - 1 ))
172 if [ -n "$fflag" ]; then
173 builtin printf "$@" >&$fd
178 y) builtin history -s "$*" ;;
179 *) builtin echo $eflag $nflag "$@" >&$fd
184 # this function should be equivalent to the substring built-in which was
185 # eliminated after the 06/29/84 version
188 local lpat flag str #local variables
197 # test for too few or too many arguments
198 if [ x"$1" = x -o $# -gt 2 ]; then
199 print -u2 'substring: bad argument count'
203 if [ x"$flag" = x-l ]; then #substring -l lpat
205 elif [ x"$flag" = x-L ]; then
206 str=${str##$lpat} #substring -L lpat
209 if [ x"$2" != x ]; then