Imported from ../bash-2.03.tar.gz.
This commit is contained in:
parent
bc4cd23ce9
commit
b72432fdcc
191 changed files with 10113 additions and 3553 deletions
125
examples/functions/autoload.v3
Normal file
125
examples/functions/autoload.v3
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
#From: Mark Kennedy <mtk@ny.ubs.com>
|
||||
#Message-ID: <35E2B899.63A02DF5@ny.ubs.com>
|
||||
#Date: Tue, 25 Aug 1998 09:14:01 -0400
|
||||
#To: chet@nike.ins.cwru.edu
|
||||
#Subject: a newer version of the ksh-style 'autoload'
|
||||
|
||||
#enclosed you'll find 'autoload.v3', a version of the autoloader
|
||||
#that emulates the ksh semantics of delaying the resolution (and loading) of the function
|
||||
#until its first use. i took the liberty of simplifying the code a bit although it still uses the
|
||||
#same functional breakdown. i recently went through the exercise of converting
|
||||
#my ksh-based environment to bash (a very, very pleasant experience)
|
||||
#and this popped out.
|
||||
|
||||
# the psuedo-ksh autoloader.
|
||||
|
||||
# The first cut of this was by Bill Trost, trost@reed.bitnet.
|
||||
# The second cut came from Chet Ramey, chet@ins.CWRU.Edu
|
||||
# The third cut came from Mark Kennedy, mtk@ny.ubs.com. 1998/08/25
|
||||
|
||||
unset _AUTOLOADS
|
||||
|
||||
_aload()
|
||||
{
|
||||
local func
|
||||
for func; do
|
||||
eval $func '()
|
||||
{
|
||||
local f=$(_autoload_resolve '$func')
|
||||
if [[ $f ]]; then
|
||||
. $f
|
||||
'$func' "$@"
|
||||
return $?
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}'
|
||||
_autoload_addlist $func
|
||||
done
|
||||
}
|
||||
|
||||
_autoload_addlist()
|
||||
{
|
||||
local func
|
||||
|
||||
for func in ${_AUTOLOADS[@]}; do
|
||||
[[ $func = "$1" ]] && return
|
||||
done
|
||||
|
||||
_AUTOLOADS[${#_AUTOLOADS[@]}]=$1
|
||||
}
|
||||
|
||||
_autoload_dump()
|
||||
{
|
||||
local func
|
||||
|
||||
for func in ${_AUTOLOADS[@]}; do
|
||||
[[ $1 ]] && echo -n "autoload "
|
||||
echo $func
|
||||
done
|
||||
}
|
||||
|
||||
_autoload_remove_one()
|
||||
{
|
||||
local func
|
||||
local -a NEW_AUTOLOADS
|
||||
|
||||
for func in ${_AUTOLOADS[@]}; do
|
||||
[[ $func != "$1" ]] && NEW_AUTOLOADS[${#NEW_AUTOLOADS[@]}]=$func
|
||||
done
|
||||
|
||||
_AUTOLOADS=( ${NEW_AUTOLOADS[@]} )
|
||||
}
|
||||
|
||||
_autoload_remove()
|
||||
{
|
||||
local victim func
|
||||
|
||||
for victim; do
|
||||
for func in ${_AUTOLOADS[@]}; do
|
||||
[[ $victim = "$func" ]] && unset -f $func && continue 2
|
||||
done
|
||||
echo "autoload: $func: not an autoloaded function" >&2
|
||||
done
|
||||
|
||||
for func; do
|
||||
_autoload_remove_one $func
|
||||
done
|
||||
}
|
||||
|
||||
_autoload_resolve()
|
||||
{
|
||||
if [[ ! "$FPATH" ]]; then
|
||||
echo "autoload: FPATH not set or null" >&2
|
||||
return
|
||||
fi
|
||||
|
||||
local p
|
||||
|
||||
for p in $( (IFS=':'; set -- ${FPATH}; echo "$@") ); do
|
||||
p=${p:-.}
|
||||
if [ -f $p/$1 ]; then echo $p/$1; return; fi
|
||||
done
|
||||
|
||||
echo "autoload: $1: function source file not found" >&2
|
||||
}
|
||||
|
||||
autoload()
|
||||
{
|
||||
if (( $# == 0 )) ; then _autoload_dump; return; fi
|
||||
|
||||
local opt OPTIND
|
||||
|
||||
while getopts pu opt
|
||||
do
|
||||
case $opt in
|
||||
p) _autoload_dump printable; return;;
|
||||
u) shift $((OPTIND-1)); _autoload_remove "$@"; return;;
|
||||
*) echo "autoload: usage: autoload [-pu] [function ...]" >&2; return;;
|
||||
esac
|
||||
done
|
||||
|
||||
shift $(($OPTIND-1))
|
||||
|
||||
_aload "$@"
|
||||
}
|
||||
|
|
@ -23,6 +23,17 @@ inet2hex ()
|
|||
hex2inet ()
|
||||
{
|
||||
local x1 x2 x3 x4
|
||||
local rev
|
||||
|
||||
OPTIND=1
|
||||
while getopts "r" o
|
||||
do
|
||||
case "$o" in
|
||||
r) rev=true;;
|
||||
*) echo "hex2inet: usage: hex2inet [0x]XXXXXXXX" >&2 ; exit 2;;
|
||||
esac
|
||||
done
|
||||
shift $(( $OPTIND - 1 ))
|
||||
|
||||
case "$1" in
|
||||
0x*) h=${1#??} ;;
|
||||
|
|
@ -40,5 +51,10 @@ hex2inet ()
|
|||
x3=$(( 0x${h:4:2} ))
|
||||
x4=$(( 0x${h:6:2} ))
|
||||
|
||||
printf "%d.%d.%d.%d\n" $x1 $x2 $x3 $x4
|
||||
if [ -z "$rev" ] ; then
|
||||
printf "%d.%d.%d.%d\n" $x1 $x2 $x3 $x4
|
||||
else
|
||||
printf "%d.%d.%d.%d\n" $x4 $x3 $x2 $x1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,19 +8,19 @@
|
|||
|
||||
lowercase()
|
||||
{
|
||||
for file; do
|
||||
filename=${file##*/}
|
||||
case "$filename" in
|
||||
*/*) dirname=${file%/*} ;;
|
||||
*) dirname=.;;
|
||||
esac
|
||||
nf=$(echo $filename | tr A-Z a-z)
|
||||
newname="${dirname}/${nf}"
|
||||
if [ "$nf" != "$filename" ]; then
|
||||
mv "$file" "$newname"
|
||||
echo "$0: $file -> $newname"
|
||||
else
|
||||
echo "$0: $file not changed."
|
||||
fi
|
||||
done
|
||||
for file; do
|
||||
filename=${file##*/}
|
||||
case "$filename" in
|
||||
*/*) dirname=${file%/*} ;;
|
||||
*) dirname=.;;
|
||||
esac
|
||||
nf=$(echo $filename | tr A-Z a-z)
|
||||
newname="${dirname}/${nf}"
|
||||
if [ "$nf" != "$filename" ]; then
|
||||
mv "$file" "$newname"
|
||||
echo "lowercase: $file -> $newname"
|
||||
else
|
||||
echo "lowercase: $file not changed."
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
|
|
|||
12
examples/functions/repeat3
Normal file
12
examples/functions/repeat3
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# From psamuels@jake.niar.twsu.edu (Peter Samuelson)
|
||||
# posted to usenet, Message-ID: <6rtp8j$2a0$1@jake.niar.twsu.edu>
|
||||
|
||||
repeat ()
|
||||
{
|
||||
local i max; # note that you can use \$i in the command string
|
||||
max=$1; shift;
|
||||
|
||||
i=1; while ((i <= max)); do
|
||||
eval "$@"; ((i = i + 1));
|
||||
done;
|
||||
}
|
||||
37
examples/functions/seq2
Normal file
37
examples/functions/seq2
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# Generate a sequence from m to n, m defaults to 1.
|
||||
|
||||
seq ()
|
||||
{
|
||||
declare -i lo hi i # makes local
|
||||
local _SEQ INIT COMPARE STEP
|
||||
|
||||
case "$1" in
|
||||
-r) INIT='i=$hi _SEQ=""' COMPARE='let "i >= $lo"' STEP='let i-=1' ; shift ;;
|
||||
*) INIT='i=$lo _SEQ=""' COMPARE='let "i <= $hi"' STEP='let i+=1' ;;
|
||||
esac
|
||||
|
||||
case $# in
|
||||
1) lo=1 hi="$1" ;;
|
||||
2) lo=$1 hi=$2 ;;
|
||||
*) echo seq: usage: seq [-r] [low] high 1>&2 ; return 2 ;;
|
||||
esac
|
||||
|
||||
# equivalent to the as-yet-unimplemented
|
||||
# for (( "$INIT" ; "$COMPARE" ; "$STEP" )); do _SEQ="${_SEQ}$i "; done
|
||||
eval "$INIT"
|
||||
while eval "$COMPARE"; do
|
||||
_SEQ="${_SEQ}$i "
|
||||
eval "$STEP"
|
||||
done
|
||||
echo "${_SEQ# }"
|
||||
return 0
|
||||
}
|
||||
|
||||
# like the APL `iota' function (or at least how I remember it :-)
|
||||
iota()
|
||||
{
|
||||
case $# in
|
||||
1) seq 1 "$1"; return $?;;
|
||||
*) echo "iota: usage: iota high" 1>&2; return 2;;
|
||||
esac
|
||||
}
|
||||
44
examples/functions/which
Normal file
44
examples/functions/which
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#
|
||||
# which - emulation of `which' as it appears in FreeBSD
|
||||
#
|
||||
# usage: which [-as] command [command...]
|
||||
#
|
||||
|
||||
which()
|
||||
{
|
||||
local aflag sflag ES a
|
||||
|
||||
OPTIND=1
|
||||
while builtin getopts as opt ; do
|
||||
case "$opt" in
|
||||
a) aflag=-a ;;
|
||||
s) sflag=1 ;;
|
||||
?) echo "which: usage: which [-as] command [command ...]" >&2
|
||||
exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
(( $OPTIND > 1 )) && shift $(( $OPTIND - 1 ))
|
||||
|
||||
# without command arguments, exit with status 1
|
||||
ES=1
|
||||
|
||||
# exit status is 0 if all commands are found, 1 if any are not found
|
||||
for command; do
|
||||
# if $command is a function, make sure we add -a so type
|
||||
# will look in $PATH after finding the function
|
||||
a=$aflag
|
||||
case "$(builtin type -t $command)" in
|
||||
"function") a=-a;;
|
||||
esac
|
||||
|
||||
if [ -n "$sflag" ]; then
|
||||
builtin type -p $a $command >/dev/null 2>&1
|
||||
else
|
||||
builtin type -p $a $command
|
||||
fi
|
||||
ES=$?
|
||||
done
|
||||
|
||||
return $ES
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue