summaryrefslogtreecommitdiff
path: root/examples/functions
diff options
context:
space:
mode:
Diffstat (limited to 'examples/functions')
-rw-r--r--examples/functions/autoload2
-rw-r--r--examples/functions/autoload.v2192
-rw-r--r--examples/functions/csh-compat49
-rw-r--r--examples/functions/exitstat2
-rw-r--r--examples/functions/inpath15
-rw-r--r--examples/functions/keep62
-rw-r--r--examples/functions/kshenv47
-rw-r--r--examples/functions/login11
-rw-r--r--examples/functions/lowercase26
-rw-r--r--examples/functions/mhfold16
-rw-r--r--examples/functions/repeat243
-rw-r--r--examples/functions/seq29
-rw-r--r--examples/functions/shcat2
13 files changed, 447 insertions, 49 deletions
diff --git a/examples/functions/autoload b/examples/functions/autoload
index a6ae4215..206b0121 100644
--- a/examples/functions/autoload
+++ b/examples/functions/autoload
@@ -29,7 +29,7 @@
aload()
{
- eval $1 '() { . '$2' ; '$1' "$@" ; return $?; }'
+ eval $1 '() { . '$2' ; '$1' "$@" ; return $? ; }'
}
#
diff --git a/examples/functions/autoload.v2 b/examples/functions/autoload.v2
new file mode 100644
index 00000000..8041b84e
--- /dev/null
+++ b/examples/functions/autoload.v2
@@ -0,0 +1,192 @@
+#
+# An almost ksh-compatible `autoload'. A function declared as `autoload' will
+# be read in from a file the same name as the function found by searching the
+# $FPATH (which works the same as $PATH), then that definition will be run.
+#
+# To do this without source support, we define a dummy function that, when
+# executed, will load the file (thereby re-defining the function), then
+# execute that newly-redefined function with the original arguments.
+#
+# It's not identical to ksh because ksh apparently does lazy evaluation
+# and looks for the file to load from only when the function is referenced.
+# This one requires that the file exist when the function is declared as
+# `autoload'.
+#
+# usage: autoload [-pu] [func ...]
+#
+# options:
+# -p print in a format that can be reused as input
+# -u unset each function and remove it from the autoload list
+#
+# The first cut of this was by Bill Trost, trost@reed.bitnet
+#
+# Chet Ramey
+# chet@ins.CWRU.Edu
+
+unset _AUTOLOADS
+_aindex=0
+
+#
+# Declare a function ($1) to be autoloaded from a file ($2) when it is first
+# called. This defines a `temporary' function that will `.' the file
+# containg the real function definition, then execute that new definition with
+# the arguments given to this `fake' function. The autoload function defined
+# by the file and the file itself *must* be named identically.
+#
+
+_aload()
+{
+ eval $1 '() { . '$2' ; '$1' "$@" ; return $? ; }'
+ _autoload_addlist "$1"
+}
+
+_autoload_addlist()
+{
+ local i=0
+
+ while (( i < $_aindex )); do
+ case "${_AUTOLOADS[i]}" in
+ "$1") return 1 ;;
+ esac
+ (( i += 1 ))
+ done
+ _AUTOLOADS[_aindex]="$1"
+ (( _aindex += 1 ))
+ return 0
+}
+
+_autoload_dump()
+{
+ local func
+
+ for func in ${_AUTOLOADS[@]}; do
+ [ -n "$1" ] && echo -n "autoload "
+ echo "$func"
+ done
+}
+
+# Remove $1 from the list of autoloaded functions
+_autoload_remove_one()
+{
+ local i=0 nnl=0
+ local -a nlist
+
+ while (( i < _aindex )); do
+ case "${_AUTOLOADS[i]}" in
+ "$1") ;;
+ *) nlist[nnl]="${_AUTOLOADS[i]}" ; (( nnl += 1 ));;
+ esac
+ (( i += 1 ))
+ done
+ unset _AUTOLOADS _aindex
+ eval _AUTOLOADS=( ${nlist[@]} )
+ _aindex=$nnl
+}
+
+# Remove all function arguments from the list of autoloaded functions
+_autoload_remove()
+{
+ local func i es=0
+
+ # first unset the autoloaded functions
+ for func; do
+ i=0
+ while (( i < _aindex )); do
+ case "${_AUTOLOADS[i]}" in
+ "$func") unset -f $func ; break ;;
+ esac
+ (( i += 1 ))
+ done
+ if (( i == _aindex )); then
+ echo "autoload: $func: not an autoloaded function" >&2
+ es=1
+ fi
+ done
+
+ # then rebuild the list of autoloaded functions
+ for func ; do
+ _autoload_remove_one "$func"
+ done
+
+ return $es
+}
+
+#
+# Search $FPATH for a file the same name as the function given as $1, and
+# autoload the function from that file. There is no default $FPATH.
+#
+
+autoload()
+{
+ local -a fp
+ local _autoload_unset nfp i
+
+ if (( $# == 0 )) ; then
+ _autoload_dump
+ return 0
+ fi
+
+ OPTIND=1
+ while getopts pu opt
+ do
+ case "$opt" in
+ p) _autoload_dump printable; return 0;;
+ u) _autoload_unset=y ;;
+ *) echo "autoload: usage: autoload [-pu] [function ...]" >&2
+ return 1 ;;
+ esac
+ done
+
+ shift $(( $OPTIND - 1 ))
+
+ if [ -n "$_autoload_unset" ]; then
+ _autoload_remove "$@"
+ return $?
+ fi
+
+ #
+ # If there is no $FPATH, there is no work to be done
+ #
+
+ if [ -z "$FPATH" ] ; then
+ echo "autoload: FPATH not set or null" >&2
+ return 1
+ fi
+
+ #
+ # This treats FPATH exactly like PATH: a null field anywhere in the
+ # FPATH is treated the same as the current directory.
+ #
+ # This turns $FPATH into an array, substituting `.' for `'
+ #
+ eval fp=( $(
+ IFS=':'
+ set -- ${FPATH}
+ for p in "$@" ; do echo -n "${p:-.} "; done
+ )
+ )
+
+ nfp=${#fp[@]}
+
+ for FUNC ; do
+ i=0;
+ while (( i < nfp )) ; do
+ if [ -f ${fp[i]}/$FUNC ] ; then
+ break # found it!
+ fi
+ (( i += 1 ))
+ done
+
+ if (( i == nfp )) ; then
+ echo "autoload: $FUNC: autoload function not found" >&2
+ es=1
+ continue
+ fi
+
+# echo auto-loading $FUNC from ${fp[i]}/$FUNC
+ _aload $FUNC ${fp[i]}/$FUNC
+ es=0
+ done
+
+ return $es
+}
diff --git a/examples/functions/csh-compat b/examples/functions/csh-compat
index 8eaf7545..a855eaf5 100644
--- a/examples/functions/csh-compat
+++ b/examples/functions/csh-compat
@@ -1,27 +1,15 @@
# C-shell compatabilty package.
# setenv VAR VALUE
-function setenv () {
+function setenv ()
+{
export $1="$2"
}
-function unsetenv () {
+function unsetenv ()
+{
unset $1
}
-function alias () {
- local name=$1
- shift
- local value="$*"
-
- if [ "$name" = "" ]; then
- builtin alias
- elif [ "$value" = "" ]; then
- builtin alias $name
- else
- builtin alias $name="$value"
- fi
-}
-
# Can't write foreach yet. Need pattern matching, and a few extras.
function foreach () {
echo 'Can'\''t do `foreach'\'' yet. Type "help for".'
@@ -31,6 +19,29 @@ echo 'Can'\''t do `foreach'\'' yet. Type "help for".'
#set () {
#}
-chdir () {
- builtin cd $*
- }
+chdir ()
+{
+ builtin cd "$@"
+}
+
+# alias - convert csh alias commands to bash functions
+# from Mohit Aron <aron@cs.rice.edu>
+# posted to usenet as <4i5p17$bnu@larry.rice.edu>
+function alias ()
+{
+ if [ "x$2" = "x" ]
+ then
+ declare -f $1
+ else
+ echo $2 | egrep -s '(\!|#)' 2>/dev/null
+ if [ $? -eq 0 ]
+ then
+ comm=$(echo $2 | sed 's/\\!\*/\"$\@\"/g
+ s/\\!:\([1-9]\)/\"$\1\"/g
+ s/#/\\#/g')
+ else
+ comm="$2 \"\$@\""
+ fi
+ eval function $1 \(\) "{" command "$comm" "; }"
+ fi
+}
diff --git a/examples/functions/exitstat b/examples/functions/exitstat
index bae3f271..2828a891 100644
--- a/examples/functions/exitstat
+++ b/examples/functions/exitstat
@@ -11,7 +11,7 @@ function check_exit_status ()
if [ ${status} -ne 0 -a ${status} != 128 ]; then
# If process exited by a signal, determine name of signal.
if [ ${status} -gt 128 ]; then
- signal="$(builtin kill -l $[${status} - 128] 2>/dev/null)"
+ signal="$(builtin kill -l $((${status} - 128)) 2>/dev/null)"
if [ "$signal" ]; then signal="($signal)"; fi
fi
echo "[Exit ${status} ${signal}]" 1>&2
diff --git a/examples/functions/inpath b/examples/functions/inpath
new file mode 100644
index 00000000..7755b33b
--- /dev/null
+++ b/examples/functions/inpath
@@ -0,0 +1,15 @@
+inpath()
+{
+ path=$(echo $PATH | sed 's/^:/.:/
+ s/::/:.:/g
+ s/:$/:./
+ s/:/ /g')
+
+ for x in $path
+ do
+ [ -x $x/$1 ] && { PROG=$x/$1; break; }
+ done
+ [ -z "$PROG" ]
+ return
+}
+
diff --git a/examples/functions/keep b/examples/functions/keep
new file mode 100644
index 00000000..4433b353
--- /dev/null
+++ b/examples/functions/keep
@@ -0,0 +1,62 @@
+# From: Seth Chaiklin <psykseth@aau.dk>
+# To: chet@ins.CWRU.Edu
+# Subject: bash functions (sorta)
+
+#
+# keep:
+# usage: keep program
+# declare the a program should be "kept". i.e. try to fg a stopped one
+# and only when that fails start a fresh program.
+#
+
+keep()
+{
+ case $# in
+ 1|2) ;;
+ *) echo "usage: keep [alias] program" 1>&2 ; return 1;;
+ esac
+
+ # progname
+ pn=${1##*/}
+
+ # set up an alias for the kept program
+ if [ $# = 1 ]; then
+ alias "$pn=fg $1 2>/dev/null || $1"
+ else
+ alias "$1=fg $2 2>/dev/null || $2"
+ fi
+}
+
+#
+# unkeep:
+# usage: unkeep program
+# unset the alias set up by the keep function
+#
+
+unkeep()
+{
+ if [ $# != 1 ]; then
+ echo "usage: unkeep program"
+ return 2
+ fi
+
+ # unset the alias for the kept program
+ unalias "${1##*/}"
+}
+
+#
+# kept:
+# lists all kept programs in 'alias: program' form
+#
+
+kept()
+{
+ alias | grep "fg.*2>" | sed "s/alias \(.*\)='fg.*||\(.*\)'$/\1:\2/"
+}
+
+
+# some things that should be kept
+#keep /usr/local/bin/emacs
+#keep e ${EDITOR:-/usr/local/bin/emacs}
+#keep edit ${EDITOR:-/usr/local/bin/emacs}
+#keep /usr/local/bin/emm
diff --git a/examples/functions/kshenv b/examples/functions/kshenv
index fbec76f4..636405e3 100644
--- a/examples/functions/kshenv
+++ b/examples/functions/kshenv
@@ -39,13 +39,9 @@ whence()
return 1
fi
case "$1" in
- -v) vflag=1
- shift 1
- ;;
- -*) echo "whence: bad option: $1"
- return 1
- ;;
- *) ;;
+ -v) vflag=1 ; shift 1 ;;
+ -*) echo "whence: bad option: $1" ; return 1 ;;
+ *) ;;
esac
if [ "$#" = "0" ] ; then
@@ -63,15 +59,12 @@ whence()
echo $path
else
case "$cmd" in
- /*) echo ""
- ;;
- *) case "$(builtin type -type $cmd)" in
- "") echo ""
- ;;
- *) echo "$cmd"
- ;;
- esac
- ;;
+ /*) echo "" ;;
+ *) case "$(builtin type -type $cmd)" in
+ "") echo "" ;;
+ *) echo "$cmd" ;;
+ esac
+ ;;
esac
fi
fi
@@ -117,7 +110,7 @@ cd()
# -n do not add trailing newline
# -p no-op (no coprocesses)
# -r no escapes
-# -s no-op (print to the history file)
+# -s print to the history file
# -u n redirect output to fd n
#
@@ -131,20 +124,20 @@ print()
while getopts "Rnprsu:" c
do
case $c in
- R) eflag=
- ;;
- r) eflag=
- ;;
- n) nflag=-n
- ;;
- u) fd=$OPTARG
- ;;
- p|s) ;;
+ R) eflag= ;;
+ r) eflag= ;;
+ n) nflag=-n ;;
+ s) sflag=y ;;
+ u) fd=$OPTARG ;;
+ p) ;;
esac
done
shift $[ $OPTIND - 1 ]
- builtin echo $eflag $nflag "$@" >&$fd
+ case "$sflag" in
+ y) builtin history -s "$*" ;;
+ *) builtin echo $eflag $nflag "$@" >&$fd
+ esac
}
# substring function
diff --git a/examples/functions/login b/examples/functions/login
new file mode 100644
index 00000000..3d596838
--- /dev/null
+++ b/examples/functions/login
@@ -0,0 +1,11 @@
+# replace the `login' and `newgrp' builtins in old bourne shells
+
+login()
+{
+ exec login "$@"
+}
+
+newgrp()
+{
+ exec newgrp "$@"
+}
diff --git a/examples/functions/lowercase b/examples/functions/lowercase
new file mode 100644
index 00000000..5f4fc9d4
--- /dev/null
+++ b/examples/functions/lowercase
@@ -0,0 +1,26 @@
+#! /bin/bash
+#
+# original from
+# @(#) lowercase.ksh 1.0 92/10/08
+# 92/10/08 john h. dubois iii (john@armory.com)
+#
+# conversion to bash v2 syntax done by Chet Ramey
+
+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
+}
diff --git a/examples/functions/mhfold b/examples/functions/mhfold
new file mode 100644
index 00000000..a4a5f70e
--- /dev/null
+++ b/examples/functions/mhfold
@@ -0,0 +1,16 @@
+# To: chet@ins.CWRU.Edu
+# Subject: Bash functions
+# From: Sandeep Mehta <sxm@philabs.Philips.Com>
+
+# print MH folders, useful only because folders(1) doesn't print
+# mod date/times
+
+mhfold()
+{
+ list=`folders | tail +2 | awk '{print $1}'`
+ /bin/ls -lag ~/Mail > /tmp/fold$$
+ for i in $list; do
+ grep $i /tmp/fold$$
+ done
+ /bin/rm -f /tmp/fold$$
+}
diff --git a/examples/functions/repeat2 b/examples/functions/repeat2
new file mode 100644
index 00000000..2e2dc7a7
--- /dev/null
+++ b/examples/functions/repeat2
@@ -0,0 +1,43 @@
+# To: chet@ins.CWRU.Edu
+# Subject: Bash functions
+# From: Sandeep Mehta <sxm@philabs.Philips.Com>
+
+##########################################
+#
+# repeat - clone of C shell builtin `repeat'
+#
+# usage: repeat <count> <command>
+#
+# It has been tested inside other functions and in conditionals like
+# if [ "`repeat <count> <command>`" ]; then COMMANDS [ else COMMANDS ] fi
+# Please send me fixes/enhancements.
+#
+# Sandeep Mehta <sxm@philabs.Philips.Com>
+##########################################
+repeat()
+{
+ local rcount=$1
+
+ if [ $# -le 1 ] || [ -z "$rcount" ]; then
+ echo "usage: repeat <count> <command>" 1>&2
+ return 2
+ fi
+
+ shift
+
+ local acmd=("$@")
+
+ if [ $rcount -le 0 ]; then
+ echo "count must be greater than 0"
+ echo "usage: repeat <count> <command>" 1>&2
+ return 2
+ fi
+
+ st=0
+ while [ $rcount -gt 0 ]; do
+ eval "${acmd[@]}"
+ st=$?
+ rcount=$((rcount - 1))
+ done
+ return $st
+}
diff --git a/examples/functions/seq b/examples/functions/seq
new file mode 100644
index 00000000..87c8a2c4
--- /dev/null
+++ b/examples/functions/seq
@@ -0,0 +1,29 @@
+# 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
+}
diff --git a/examples/functions/shcat b/examples/functions/shcat
index 55a30965..c5d3d630 100644
--- a/examples/functions/shcat
+++ b/examples/functions/shcat
@@ -1,6 +1,6 @@
shcat()
{
- while read line
+ while read -r line
do
echo "$line"
done