mirror of
https://https.git.savannah.gnu.org/git/bash.git
synced 2026-07-03 18:30:49 +02:00
bash-20130308 remove leftover and stray files
This commit is contained in:
@@ -1,103 +0,0 @@
|
||||
# usage: reverse arrayname
|
||||
reverse()
|
||||
{
|
||||
local -a R
|
||||
local -i i
|
||||
local rlen temp
|
||||
|
||||
# make r a copy of the array whose name is passed as an arg
|
||||
eval R=\( \"\$\{$1\[@\]\}\" \)
|
||||
|
||||
# reverse R
|
||||
rlen=${#R[@]}
|
||||
|
||||
for ((i=0; i < rlen/2; i++ ))
|
||||
do
|
||||
temp=${R[i]}
|
||||
R[i]=${R[rlen-i-1]}
|
||||
R[rlen-i-1]=$temp
|
||||
done
|
||||
|
||||
# and assign R back to array whose name is passed as an arg
|
||||
eval $1=\( \"\$\{R\[@\]\}\" \)
|
||||
}
|
||||
|
||||
A=(1 2 3 4 5 6 7)
|
||||
echo "${A[@]}"
|
||||
reverse A
|
||||
echo "${A[@]}"
|
||||
reverse A
|
||||
echo "${A[@]}"
|
||||
|
||||
# unset last element of A
|
||||
alen=${#A[@]}
|
||||
unset A[$alen-1]
|
||||
echo "${A[@]}"
|
||||
|
||||
# ashift -- like shift, but for arrays
|
||||
|
||||
ashift()
|
||||
{
|
||||
local -a R
|
||||
local n
|
||||
|
||||
case $# in
|
||||
1) n=1 ;;
|
||||
2) n=$2 ;;
|
||||
*) echo "$FUNCNAME: usage: $FUNCNAME array [count]" >&2
|
||||
exit 2;;
|
||||
esac
|
||||
|
||||
# make r a copy of the array whose name is passed as an arg
|
||||
eval R=\( \"\$\{$1\[@\]\}\" \)
|
||||
|
||||
# shift R
|
||||
R=( "${R[@]:$n}" )
|
||||
|
||||
# and assign R back to array whose name is passed as an arg
|
||||
eval $1=\( \"\$\{R\[@\]\}\" \)
|
||||
}
|
||||
|
||||
ashift A 2
|
||||
echo "${A[@]}"
|
||||
|
||||
ashift A
|
||||
echo "${A[@]}"
|
||||
|
||||
ashift A 7
|
||||
echo "${A[@]}"
|
||||
|
||||
# Sort the members of the array whose name is passed as the first non-option
|
||||
# arg. If -u is the first arg, remove duplicate array members.
|
||||
array_sort()
|
||||
{
|
||||
local -a R
|
||||
local u
|
||||
|
||||
case "$1" in
|
||||
-u) u=-u ; shift ;;
|
||||
esac
|
||||
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "array_sort: argument expected" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
# make r a copy of the array whose name is passed as an arg
|
||||
eval R=\( \"\$\{$1\[@\]\}\" \)
|
||||
|
||||
# sort R
|
||||
R=( $( printf "%s\n" "${A[@]}" | sort $u) )
|
||||
|
||||
# and assign R back to array whose name is passed as an arg
|
||||
eval $1=\( \"\$\{R\[@\]\}\" \)
|
||||
return 0
|
||||
}
|
||||
|
||||
A=(3 1 4 1 5 9 2 6 5 3 2)
|
||||
array_sort A
|
||||
echo "${A[@]}"
|
||||
|
||||
A=(3 1 4 1 5 9 2 6 5 3 2)
|
||||
array_sort -u A
|
||||
echo "${A[@]}"
|
||||
@@ -1,59 +0,0 @@
|
||||
#
|
||||
# A function that works as a front end for both stty and the `bind'
|
||||
# builtin, so the tty driver and readline see the same changes
|
||||
#
|
||||
|
||||
#
|
||||
# Convert between the stty ^H control character form and the readline \C-H
|
||||
# form
|
||||
#
|
||||
cvt()
|
||||
{
|
||||
echo "$@" | cat -v | sed 's/\^/\\C-/'
|
||||
}
|
||||
|
||||
#
|
||||
# stty front-end. Parses the argument list and creates two command strings,
|
||||
# one for stty, another for bind.
|
||||
#
|
||||
fstty()
|
||||
{
|
||||
local cmd="" bargs=""
|
||||
local e
|
||||
|
||||
while [ $# -gt 0 ]
|
||||
do
|
||||
case "$1" in
|
||||
-a) cmd="$cmd everything"
|
||||
;;
|
||||
erase) shift;
|
||||
e=$(cvt "$1")
|
||||
cmd="$cmd erase $1"
|
||||
bargs="$bargs '\"$e\": backward-delete-char'"
|
||||
;;
|
||||
kill) shift
|
||||
e=$(cvt "$1")
|
||||
cmd="$cmd kill $1"
|
||||
bargs="$bargs '\"$e\": unix-line-discard'"
|
||||
;;
|
||||
werase) shift;
|
||||
e=$(cvt "$1")
|
||||
cmd="$cmd erase $1"
|
||||
bargs="$bargs '\"$e\": backward-kill-word'"
|
||||
;;
|
||||
lnext) shift;
|
||||
e=$(cvt "$1")
|
||||
cmd="$cmd erase $1"
|
||||
bargs="$bargs '\"$e\": quoted-insert'"
|
||||
;;
|
||||
*) cmd="$cmd $1"
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
command stty $cmd
|
||||
if [ -n "$bargs" ]; then
|
||||
builtin bind $bargs
|
||||
fi
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
#
|
||||
# func -- print out definitions for functions named by arguments
|
||||
#
|
||||
# usage: func name [name ...]
|
||||
#
|
||||
# Chet Ramey
|
||||
# chet@ins.CWRU.Edu
|
||||
func()
|
||||
{
|
||||
local status=0
|
||||
|
||||
if [ $# -eq 0 ] ; then
|
||||
echo "usage: func name [name...]" 1>&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
for f
|
||||
do
|
||||
if [ "$(builtin type -type $f)" != "function" ] ; then
|
||||
echo "func: $f: not a function" 1>&2
|
||||
status=1 # one failed
|
||||
continue
|
||||
fi
|
||||
builtin type $f | sed 1d
|
||||
done
|
||||
return $status
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
#
|
||||
# inet2hex - Internet address conversion, dotted-decimal to hex
|
||||
#
|
||||
inet2hex ()
|
||||
{
|
||||
local IFS
|
||||
|
||||
IFS=.
|
||||
set -- $1
|
||||
|
||||
if (( $# != 4 )); then
|
||||
echo "inet2hex: incorrect input format: $1" >&2
|
||||
echo "inet2hex: usage: inet2hex XX.XX.XX.XX" >&2
|
||||
return 2
|
||||
fi
|
||||
|
||||
printf "0x%02x%02x%02x%02x\n" $1 $2 $3 $4
|
||||
}
|
||||
|
||||
#
|
||||
# hex2inet - Internet address conversion, hex to dotted-decimal
|
||||
#
|
||||
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 [-r] [0x]XXXXXXXX" >&2 ; exit 2;;
|
||||
esac
|
||||
done
|
||||
shift $(( $OPTIND - 1 ))
|
||||
|
||||
case "$1" in
|
||||
0x*) h=${1#??} ;;
|
||||
*) h=$1 ;;
|
||||
esac
|
||||
|
||||
if (( ${#h} != 8 )); then
|
||||
echo "hex2inet: $h not in inet format" >&2
|
||||
echo "hex2inet: usage: hex2inet [0x]XXXXXXXX" >&2
|
||||
return 2
|
||||
fi
|
||||
|
||||
x1=$(( 0x${h:0:2} ))
|
||||
x2=$(( 0x${h:2:2} ))
|
||||
x3=$(( 0x${h:4:2} ))
|
||||
x4=$(( 0x${h:6:2} ))
|
||||
|
||||
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
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
isnum2()
|
||||
{
|
||||
case "$1" in
|
||||
'[-+]' | '') return 1;; # empty or bare `-' or `+'
|
||||
[-+]*[!0-9]*) return 1;; # non-digit with leading sign
|
||||
[-+]*) return 0;; # OK
|
||||
*[!0-9]*) return 1;; # non-digit
|
||||
*) return 0;; # OK
|
||||
esac
|
||||
}
|
||||
|
||||
# this one handles floating point
|
||||
isnum3()
|
||||
{
|
||||
case "$1" in
|
||||
'') return 1;; # empty
|
||||
*[!0-9.+-]*) return 1;; # non-digit, +, -, or .
|
||||
*?[-+]*) return 1;; # sign as second or later char
|
||||
*.*.*) return 1;; # multiple decimal points
|
||||
*) return 0;; # OK
|
||||
esac
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
#
|
||||
# ksh-like `cd': cd [-LP] [dir [change]]
|
||||
#
|
||||
cd()
|
||||
{
|
||||
OPTIND=1
|
||||
while getopts "LP" opt
|
||||
do
|
||||
case $opt in
|
||||
L|P) CDOPTS="$CDOPTS -$opt" ;;
|
||||
*) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
|
||||
return 2;;
|
||||
esac
|
||||
done
|
||||
|
||||
shift $(( $OPTIND - 1 ))
|
||||
|
||||
case $# in
|
||||
0) builtin cd $CDOPTS "$HOME" ;;
|
||||
1) builtin cd $CDOPTS "$@" ;;
|
||||
2) old="$1" new="$2"
|
||||
case "$PWD" in
|
||||
*$old*) ;;
|
||||
*) echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ; return 1 ;;
|
||||
esac
|
||||
|
||||
dir=${PWD//$old/$new}
|
||||
|
||||
builtin cd $CDOPTS "$dir" && echo "$PWD"
|
||||
|
||||
;;
|
||||
*) echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
|
||||
return 2 ;;
|
||||
esac
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
#
|
||||
# replacements for test/[ that do arithmetic expansion on the operands to
|
||||
# the arithmetic operators, like ksh.
|
||||
#
|
||||
function test()
|
||||
{
|
||||
local -i n1 n3
|
||||
case "$#" in
|
||||
3) case "$2" in
|
||||
-lt|-gt|-eq|-ne|-le|-ge) n1=$(( $1 ))
|
||||
n3=$(( $3 ))
|
||||
builtin test "$n1" $2 "$n3"
|
||||
return $?;;
|
||||
*) builtin test "$@" ;;
|
||||
esac;;
|
||||
*) builtin test "$@" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
function [()
|
||||
{
|
||||
local -i n1 n3
|
||||
case "$#" in
|
||||
4) case "$2" in
|
||||
-lt|-gt|-eq|-ne|-le|-ge) n1=$(( $1 ))
|
||||
n3=$(( $3 ))
|
||||
builtin [ "$n1" $2 "$n3" ]
|
||||
return $?;;
|
||||
*) builtin [ "$@" ;;
|
||||
esac;;
|
||||
*) builtin [ "$@" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
q=7
|
||||
|
||||
[ q -lt 10 ]
|
||||
echo $?
|
||||
[ $q -lt 10 ]
|
||||
echo $?
|
||||
@@ -1,228 +0,0 @@
|
||||
#
|
||||
# .kshenv -- functions and aliases to provide the beginnings of a ksh
|
||||
# environment for bash.
|
||||
#
|
||||
# Chet Ramey
|
||||
# chet@ins.CWRU.Edu
|
||||
#
|
||||
#
|
||||
# These are definitions for the ksh compiled-in `exported aliases'. There
|
||||
# are others, but we already have substitutes for them: "history", "type",
|
||||
# and "hash".
|
||||
#
|
||||
alias r="fc -s"
|
||||
alias functions="typeset -f"
|
||||
alias integer="typeset -i"
|
||||
alias nohup="nohup "
|
||||
alias command="command "
|
||||
alias stop="kill -s STOP"
|
||||
alias redirect="command exec"
|
||||
alias hist="fc"
|
||||
|
||||
#
|
||||
# An almost-ksh compatible `whence' command. This is as hairy as it is
|
||||
# because of the desire to exactly mimic ksh (whose behavior was determined
|
||||
# empirically).
|
||||
#
|
||||
# This depends somewhat on knowing the format of the output of the bash
|
||||
# `builtin type' command.
|
||||
#
|
||||
|
||||
whence()
|
||||
{
|
||||
local vflag pflag fflag defarg c
|
||||
local path
|
||||
|
||||
vflag= aflag= pflag= fflag=
|
||||
path=
|
||||
if [ "$#" = "0" ] ; then
|
||||
echo "whence: usage: whence [-afpv] name..." >&2
|
||||
return 2
|
||||
fi
|
||||
|
||||
OPTIND=1
|
||||
while getopts "avfp" c
|
||||
do
|
||||
case "$c" in
|
||||
a) defarg=-a ;;
|
||||
f) fflag=1 ;; # no-op
|
||||
p) pflag=1 ;;
|
||||
v) vflag=1 ;;
|
||||
?) echo "whence: $1: unknown option" >&2
|
||||
echo "whence: usage: whence [-afpv] name..." >&2
|
||||
return 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
shift $(( $OPTIND - 1 ))
|
||||
|
||||
if [ "$#" = "0" ] ; then
|
||||
echo "whence: usage: whence [-afpv] name..." >&2
|
||||
return 2
|
||||
fi
|
||||
|
||||
for cmd
|
||||
do
|
||||
if [ "$vflag" ] ; then
|
||||
if [ -z "$defarg" ]; then
|
||||
builtin type $cmd | sed 1q
|
||||
else
|
||||
if builtin type $defarg -t $cmd | grep 'function$' >/dev/null 2>&1; then
|
||||
# HAIRY awk script to suppress
|
||||
# printing of function body -- could
|
||||
# do it with sed, but I don't have
|
||||
# that kind of time
|
||||
builtin type $defarg $cmd | awk '
|
||||
BEGIN {printit = 1;}
|
||||
$1 == "'$cmd'" && $2 == "()" {printit=0; next; }
|
||||
/^}$/ { if (printit == 0) printit=1 ; else print $0; next ; }
|
||||
/.*/ { if (printit) print $0; }'
|
||||
else
|
||||
builtin type $defarg $cmd
|
||||
fi
|
||||
fi
|
||||
else
|
||||
path=$(builtin type $defarg -p $cmd)
|
||||
if [ "$path" ] ; then
|
||||
echo $path
|
||||
else
|
||||
case "$cmd" in
|
||||
/*) echo "" ;;
|
||||
*) case "$(builtin type -t $cmd)" in
|
||||
"") echo "" ;;
|
||||
*) echo "$cmd" ;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
fi
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
#
|
||||
# For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
|
||||
# version.
|
||||
#
|
||||
#type()
|
||||
#{
|
||||
# whence -v "$*"
|
||||
#}
|
||||
|
||||
#
|
||||
# ksh-like `cd': cd [-LP] [dir [change]]
|
||||
#
|
||||
cd()
|
||||
{
|
||||
OPTIND=1
|
||||
while getopts "LP" opt
|
||||
do
|
||||
case $opt in
|
||||
L|P) CDOPTS="$CDOPTS -$opt" ;;
|
||||
*) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
|
||||
return 2;;
|
||||
esac
|
||||
done
|
||||
|
||||
shift $(( $OPTIND - 1 ))
|
||||
|
||||
case $# in
|
||||
0) builtin cd $CDOPTS "$HOME" ;;
|
||||
1) builtin cd $CDOPTS "$@" ;;
|
||||
2) old="$1" new="$2"
|
||||
case "$PWD" in
|
||||
*$old*) ;;
|
||||
*) echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ; return 1 ;;
|
||||
esac
|
||||
|
||||
dir=${PWD//$old/$new}
|
||||
|
||||
builtin cd $CDOPTS "$dir" && echo "$PWD"
|
||||
|
||||
;;
|
||||
*) echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
|
||||
return 2 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
#
|
||||
# ksh print emulation
|
||||
#
|
||||
# print [-Rnprsu[n]] [-f format] [arg ...]
|
||||
#
|
||||
# - end of options
|
||||
# -R BSD-style -- only accept -n, no escapes
|
||||
# -n do not add trailing newline
|
||||
# -p no-op (no coprocesses)
|
||||
# -r no escapes
|
||||
# -s print to the history file
|
||||
# -u n redirect output to fd n
|
||||
# -f format printf "$format" "$@"
|
||||
#
|
||||
|
||||
print()
|
||||
{
|
||||
local eflag=-e
|
||||
local nflag= fflag= c
|
||||
local fd=1
|
||||
|
||||
OPTIND=1
|
||||
while getopts "fRnprsu:" c
|
||||
do
|
||||
case $c in
|
||||
R) eflag= ;;
|
||||
r) eflag= ;;
|
||||
n) nflag=-n ;;
|
||||
s) sflag=y ;;
|
||||
f) fflag=y ;;
|
||||
u) fd=$OPTARG ;;
|
||||
p) ;;
|
||||
esac
|
||||
done
|
||||
shift $(( $OPTIND - 1 ))
|
||||
|
||||
if [ -n "$fflag" ]; then
|
||||
builtin printf "$@" >&$fd
|
||||
return
|
||||
fi
|
||||
|
||||
case "$sflag" in
|
||||
y) builtin history -s "$*" ;;
|
||||
*) builtin echo $eflag $nflag "$@" >&$fd
|
||||
esac
|
||||
}
|
||||
|
||||
# substring function
|
||||
# this function should be equivalent to the substring built-in which was
|
||||
# eliminated after the 06/29/84 version
|
||||
substring ()
|
||||
{
|
||||
local lpat flag str #local variables
|
||||
set -f
|
||||
case $1 in
|
||||
-l|-L)
|
||||
flag=$1
|
||||
lpat=$2
|
||||
shift 2
|
||||
;;
|
||||
esac
|
||||
# test for too few or too many arguments
|
||||
if [ x"$1" = x ] || [ $# -gt 2 ]; then
|
||||
print -u2 'substring: bad argument count'
|
||||
return 1
|
||||
fi
|
||||
str=$1
|
||||
if [ x"$flag" = x-l ]; then #substring -l lpat
|
||||
str=${str#$lpat}
|
||||
elif [ x"$flag" = x-L ]; then
|
||||
str=${str##$lpat} #substring -L lpat
|
||||
fi
|
||||
|
||||
if [ x"$2" != x ]; then
|
||||
echo ${str%$2}
|
||||
else
|
||||
echo $str
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
trap _notify CHLD
|
||||
NOTIFY_ALL=false
|
||||
unset NOTIFY_LIST
|
||||
unalias false
|
||||
|
||||
false()
|
||||
{
|
||||
return 1
|
||||
}
|
||||
|
||||
_notify ()
|
||||
{
|
||||
local i j
|
||||
local newlist=
|
||||
|
||||
if $NOTIFY_ALL
|
||||
then
|
||||
return # let bash take care of this itself
|
||||
elif [ -z "$NOTIFY_LIST" ]; then
|
||||
return
|
||||
else
|
||||
set -- $NOTIFY_LIST
|
||||
for i in "$@"
|
||||
do
|
||||
j=$(jobs -n %$i)
|
||||
if [ -n "$j" ]; then
|
||||
echo "$j"
|
||||
jobs -n %$i >/dev/null
|
||||
else
|
||||
newlist="newlist $i"
|
||||
fi
|
||||
done
|
||||
NOTIFY_LIST="$newlist"
|
||||
fi
|
||||
}
|
||||
|
||||
notify ()
|
||||
{
|
||||
local i j
|
||||
|
||||
if [ $# -eq 0 ]; then
|
||||
NOTIFY_ALL=:
|
||||
set -b
|
||||
return
|
||||
else
|
||||
for i in "$@"
|
||||
do
|
||||
# turn a valid job spec into a job number
|
||||
j=$(jobs $i)
|
||||
case "$j" in
|
||||
[*) j=${j%%]*}
|
||||
j=${j#[}
|
||||
NOTIFY_LIST="$NOTIFY_LIST $j"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
fi
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
# 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
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
# Generate a sequence from m to n, m defaults to 1.
|
||||
|
||||
seq ()
|
||||
{
|
||||
declare -i lo hi i # makes local
|
||||
local _SEQ
|
||||
|
||||
case $# in
|
||||
1) seq 1 "$1" ; return $? ;;
|
||||
2) lo=$1 hi=$2
|
||||
i=$lo _SEQ=""
|
||||
while let "i <= hi"; do
|
||||
_SEQ="${_SEQ}$i "
|
||||
let i+=1
|
||||
done
|
||||
echo "${_SEQ# }"
|
||||
return 0 ;;
|
||||
*) echo seq: usage: seq [low] high 1>&2 ; return 2 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# 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
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
# Sort the positional paramters.
|
||||
# Make sure the positional parameters are passed as arguments to the function.
|
||||
# If -u is the first arg, remove duplicate array members.
|
||||
sort_posparams()
|
||||
{
|
||||
local -a R
|
||||
local u
|
||||
|
||||
case "$1" in
|
||||
-u) u=-u ; shift ;;
|
||||
esac
|
||||
|
||||
# if you want the case of no positional parameters to return success,
|
||||
# remove the error message and return 0
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "$FUNCNAME: argument expected" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
# make R a copy of the positional parameters
|
||||
R=( "${@}" )
|
||||
|
||||
# sort R.
|
||||
R=( $( printf "%s\n" "${R[@]}" | sort $u) )
|
||||
|
||||
printf "%s\n" "${R[@]}"
|
||||
return 0
|
||||
}
|
||||
|
||||
# will print everything on separate lines
|
||||
set -- 3 1 4 1 5 9 2 6 5 3 2
|
||||
sort_posparams "$@"
|
||||
|
||||
# sets without preserving quoted parameters
|
||||
set -- $( sort_posparams "$@" )
|
||||
echo "$@"
|
||||
echo $#
|
||||
|
||||
# sets preserving quoted parameters, beware pos params with embedded newlines
|
||||
set -- 'a b' 'a c' 'x z'
|
||||
|
||||
oifs=$IFS
|
||||
IFS=$'\n'
|
||||
set -- $( sort_posparams "$@" )
|
||||
IFS="$oifs"
|
||||
|
||||
echo "$@"
|
||||
echo $#
|
||||
|
||||
sort_posparams
|
||||
@@ -1,81 +0,0 @@
|
||||
#
|
||||
# substr -- a function to emulate the ancient ksh builtin
|
||||
#
|
||||
|
||||
# -l == remove shortest from left
|
||||
# -L == remove longest from left
|
||||
# -r == remove shortest from right (the default)
|
||||
# -R == remove longest from right
|
||||
|
||||
substr()
|
||||
{
|
||||
local flag pat str
|
||||
local usage="usage: substr -lLrR pat string or substr string pat"
|
||||
local options="l:L:r:R:"
|
||||
|
||||
OPTIND=1
|
||||
while getopts "$options" c
|
||||
do
|
||||
case "$c" in
|
||||
l | L | r | R)
|
||||
flag="-$c"
|
||||
pat="$OPTARG"
|
||||
;;
|
||||
'?')
|
||||
echo "$usage"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ "$OPTIND" -gt 1 ] ; then
|
||||
shift $(( $OPTIND -1 ))
|
||||
fi
|
||||
|
||||
if [ "$#" -eq 0 ] || [ "$#" -gt 2 ] ; then
|
||||
echo "substr: bad argument count"
|
||||
return 2
|
||||
fi
|
||||
|
||||
str="$1"
|
||||
|
||||
#
|
||||
# We don't want -f, but we don't want to turn it back on if
|
||||
# we didn't have it already
|
||||
#
|
||||
case "$-" in
|
||||
"*f*")
|
||||
;;
|
||||
*)
|
||||
fng=1
|
||||
set -f
|
||||
;;
|
||||
esac
|
||||
|
||||
case "$flag" in
|
||||
-l)
|
||||
str="${str#$pat}" # substr -l pat string
|
||||
;;
|
||||
-L)
|
||||
str="${str##$pat}" # substr -L pat string
|
||||
;;
|
||||
-r)
|
||||
str="${str%$pat}" # substr -r pat string
|
||||
;;
|
||||
-R)
|
||||
str="${str%%$pat}" # substr -R pat string
|
||||
;;
|
||||
*)
|
||||
str="${str%$2}" # substr string pat
|
||||
;;
|
||||
esac
|
||||
|
||||
echo "$str"
|
||||
|
||||
#
|
||||
# If we had file name generation when we started, re-enable it
|
||||
#
|
||||
if [ "$fng" = "1" ] ; then
|
||||
set +f
|
||||
fi
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
#
|
||||
# substr -- a function to emulate the ancient ksh builtin
|
||||
#
|
||||
|
||||
#
|
||||
# -l == shortest from left
|
||||
# -L == longest from left
|
||||
# -r == shortest from right (the default)
|
||||
# -R == longest from right
|
||||
|
||||
substr()
|
||||
{
|
||||
local flag pat str
|
||||
local usage="usage: substr -lLrR pat string or substr string pat"
|
||||
|
||||
case "$1" in
|
||||
-l | -L | -r | -R)
|
||||
flag="$1"
|
||||
pat="$2"
|
||||
shift 2
|
||||
;;
|
||||
-*)
|
||||
echo "substr: unknown option: $1"
|
||||
echo "$usage"
|
||||
return 1
|
||||
;;
|
||||
*)
|
||||
flag="-r"
|
||||
pat="$2"
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ "$#" -eq 0 ] || [ "$#" -gt 2 ] ; then
|
||||
echo "substr: bad argument count"
|
||||
return 2
|
||||
fi
|
||||
|
||||
str="$1"
|
||||
|
||||
#
|
||||
# We don't want -f, but we don't want to turn it back on if
|
||||
# we didn't have it already
|
||||
#
|
||||
case "$-" in
|
||||
"*f*")
|
||||
;;
|
||||
*)
|
||||
fng=1
|
||||
set -f
|
||||
;;
|
||||
esac
|
||||
|
||||
case "$flag" in
|
||||
-l)
|
||||
str="${str#$pat}" # substr -l pat string
|
||||
;;
|
||||
-L)
|
||||
str="${str##$pat}" # substr -L pat string
|
||||
;;
|
||||
-r)
|
||||
str="${str%$pat}" # substr -r pat string
|
||||
;;
|
||||
-R)
|
||||
str="${str%%$pat}" # substr -R pat string
|
||||
;;
|
||||
*)
|
||||
str="${str%$2}" # substr string pat
|
||||
;;
|
||||
esac
|
||||
|
||||
echo "$str"
|
||||
|
||||
#
|
||||
# If we had file name generation when we started, re-enable it
|
||||
#
|
||||
if [ "$fng" = "1" ] ; then
|
||||
set +f
|
||||
fi
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
#
|
||||
# whatis -- and implementation of the 10th Edition Unix sh builtin `whatis'
|
||||
# command.
|
||||
#
|
||||
# usage: whatis arg [...]
|
||||
#
|
||||
# For each argument, whatis prints the associated value as a parameter,
|
||||
# builtin, function, alias, or executable file as appropriate. In each
|
||||
# case, the value is printed in a form which would yield the same value
|
||||
# if typed as input to the shell itself.
|
||||
#
|
||||
|
||||
whatis()
|
||||
{
|
||||
local wusage='usage: whatis arg [arg...]'
|
||||
local fail=0
|
||||
|
||||
if [ $# -eq 0 ] ; then
|
||||
echo "$wusage"
|
||||
return 1
|
||||
fi
|
||||
|
||||
for arg
|
||||
do
|
||||
case $(builtin type -type $arg 2>/dev/null) in
|
||||
"alias")
|
||||
builtin alias "$arg"
|
||||
;;
|
||||
"function")
|
||||
builtin type "$arg" | sed 1d
|
||||
;;
|
||||
"builtin")
|
||||
echo builtin "$arg"
|
||||
;;
|
||||
"file")
|
||||
builtin type -path "$arg"
|
||||
;;
|
||||
*)
|
||||
# OK, we could have a variable, or we could have nada
|
||||
if [ "$(eval echo \${$arg+set})" = "set" ] ; then
|
||||
# It is a variable, and it is set
|
||||
echo -n "$arg="
|
||||
eval echo '\"'\$$arg'\"'
|
||||
else
|
||||
echo whatis: $arg: not found
|
||||
fail=1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
return $fail
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
#
|
||||
# An almost-ksh compatible `whence' command. This is as hairy as it is
|
||||
# because of the desire to exactly mimic ksh.
|
||||
#
|
||||
# This depends somewhat on knowing the format of the output of the bash
|
||||
# `builtin type' command.
|
||||
#
|
||||
# Chet Ramey
|
||||
# chet@ins.CWRU.Edu
|
||||
#
|
||||
whence()
|
||||
{
|
||||
local vflag= path=
|
||||
|
||||
if [ "$#" = "0" ] ; then
|
||||
echo "whence: argument expected"
|
||||
return 1
|
||||
fi
|
||||
case "$1" in
|
||||
-v) vflag=1
|
||||
shift 1
|
||||
;;
|
||||
-*) echo "whence: bad option: $1"
|
||||
return 1
|
||||
;;
|
||||
*) ;;
|
||||
esac
|
||||
|
||||
if [ "$#" = "0" ] ; then
|
||||
echo "whence: bad argument count"
|
||||
return 1
|
||||
fi
|
||||
|
||||
for cmd
|
||||
do
|
||||
if [ "$vflag" ] ; then
|
||||
echo $(builtin type $cmd | sed 1q)
|
||||
else
|
||||
path=$(builtin type -path $cmd)
|
||||
if [ "$path" ] ; then
|
||||
echo $path
|
||||
else
|
||||
case "$cmd" in
|
||||
/*) if [ -x "$cmd" ]; then
|
||||
echo "$cmd"
|
||||
fi
|
||||
;;
|
||||
*) case "$(builtin type -type $cmd)" in
|
||||
"") ;;
|
||||
*) echo "$cmd"
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
fi
|
||||
done
|
||||
return 0
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
#
|
||||
# which - emulation of `which' as it appears in FreeBSD
|
||||
#
|
||||
# usage: which [-as] command [command...]
|
||||
#
|
||||
|
||||
which()
|
||||
{
|
||||
local aflag sflag ES a opt
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user