diff options
Diffstat (limited to 'examples/functions')
-rw-r--r-- | examples/functions/array-stuff | 103 | ||||
-rw-r--r-- | examples/functions/isnum.bash | 31 | ||||
-rw-r--r-- | examples/functions/ksh-compat-test | 40 | ||||
-rw-r--r-- | examples/functions/kshenv | 88 | ||||
-rw-r--r-- | examples/functions/lowercase | 1 | ||||
-rw-r--r-- | examples/functions/pathfuncs | 3 | ||||
-rw-r--r-- | examples/functions/which | 2 | ||||
-rw-r--r-- | examples/functions/xfind.bash | 52 |
8 files changed, 291 insertions, 29 deletions
diff --git a/examples/functions/array-stuff b/examples/functions/array-stuff new file mode 100644 index 0000000..97ed512 --- /dev/null +++ b/examples/functions/array-stuff @@ -0,0 +1,103 @@ +# 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[@]}" diff --git a/examples/functions/isnum.bash b/examples/functions/isnum.bash index 1eff13f..b733965 100644 --- a/examples/functions/isnum.bash +++ b/examples/functions/isnum.bash @@ -11,7 +11,7 @@ # BASH NOTE: make sure you have executed `shopt -s extglob' before # trying to use this function, or it will not work -function isnum # string +isnum() # string { case $1 in ?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) ) @@ -21,3 +21,32 @@ function isnum # string *) return 1;; esac } + +isnum2() # string +{ + case $1 in + ?([-+])+([[:digit:]])?(.)*([[:digit:]])?([Ee]?([-+])+([[:digit:]])) ) + return 0;; + ?([-+])*([[:digit:]])?(.)+([[:digit:]])?([Ee]?([-+])+([[:digit:]])) ) + return 0;; + *) return 1;; + esac +} + +isint() # string +{ + case $1 in + ?([-+])+([0-9]) ) + return 0;; + *) return 1;; + esac +} + +isint2() # string +{ + case $1 in + ?([-+])+([[:digit:]]) ) + return 0;; + *) return 1;; + esac +} diff --git a/examples/functions/ksh-compat-test b/examples/functions/ksh-compat-test new file mode 100644 index 0000000..feee965 --- /dev/null +++ b/examples/functions/ksh-compat-test @@ -0,0 +1,40 @@ +# +# 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 $? diff --git a/examples/functions/kshenv b/examples/functions/kshenv index 636405e..35cc212 100644 --- a/examples/functions/kshenv +++ b/examples/functions/kshenv @@ -10,12 +10,13 @@ # are others, but we already have substitutes for them: "history", "type", # and "hash". # -alias r="fc -e -" +alias r="fc -s" alias functions="typeset -f" alias integer="typeset -i" alias nohup="nohup " -alias true=":" -alias false="let 0" +alias command="command " +alias stop="kill -s STOP" +alias redirect="command exec" alias hist="fc" # @@ -29,38 +30,65 @@ alias hist="fc" whence() { - local vflag + local vflag pflag fflag defarg c local path - vflag= + vflag= aflag= pflag= fflag= path= if [ "$#" = "0" ] ; then - echo "whence: argument expected" - return 1 + echo "whence: usage: whence [-afpv] name..." >&2 + return 2 fi - case "$1" in - -v) vflag=1 ; shift 1 ;; - -*) echo "whence: bad option: $1" ; return 1 ;; - *) ;; - esac + + 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: bad argument count" - return 1 + echo "whence: usage: whence [-afpv] name..." >&2 + return 2 fi for cmd do if [ "$vflag" ] ; then - echo $(builtin type $cmd | sed 1q) + 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 -path $cmd) + path=$(builtin type $defarg -p $cmd) if [ "$path" ] ; then echo $path else case "$cmd" in /*) echo "" ;; - *) case "$(builtin type -type $cmd)" in + *) case "$(builtin type -t $cmd)" in "") echo "" ;; *) echo "$cmd" ;; esac @@ -88,22 +116,27 @@ cd() 1) builtin cd "$@" ;; 2) old="$1" new="$2" - dir=$(echo "$PWD" | sed "s:$old:$new:g") + # dir=$(echo "$PWD" | sed "s:$old:$new:g") + dir=${PWD//$old/$new} case "$dir" in - "$PWD") echo "bash: cd: bad substitution" >&2 ; return 1 ;; + "$PWD") case "$PWD" in + *$old*) ;; + *) echo "$FUNCNAME: bad substitution" >&2 ; return 1 ;; + esac ;; *) echo "$dir" builtin cd "$dir" ;; esac ;; - *) echo "cd: wrong arg count" >&2 ; return 1 ;; + *) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2 + return 1 ;; esac } # # ksh print emulation # -# print [-Rnprsu[n]] [arg ...] +# print [-Rnprsu[n]] [-f format] [arg ...] # # - end of options # -R BSD-style -- only accept -n, no escapes @@ -112,27 +145,34 @@ cd() # -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= + local nflag= fflag= c local fd=1 OPTIND=1 - while getopts "Rnprsu:" c + 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 ] + shift $(( $OPTIND - 1 )) + + if [ -n "$fflag" ]; then + builtin printf "$@" >&$fd + return + fi case "$sflag" in y) builtin history -s "$*" ;; diff --git a/examples/functions/lowercase b/examples/functions/lowercase index a0649f8..e3f866e 100644 --- a/examples/functions/lowercase +++ b/examples/functions/lowercase @@ -9,6 +9,7 @@ lowercase() { for file; do + [ -f "$file" ] || continue filename=${file##*/} case "$filename" in */*) dirname=${file%/*} ;; diff --git a/examples/functions/pathfuncs b/examples/functions/pathfuncs index 47896bf..56fdca3 100644 --- a/examples/functions/pathfuncs +++ b/examples/functions/pathfuncs @@ -17,9 +17,6 @@ # AUTHOR: # Simon J. Gerraty <sjg@zen.void.oz.au> -# RCSid: -# $Id: add_path.sh,v 1.1 1995/09/30 12:45:23 sjg Exp $ -# # @(#)Copyright (c) 1991 Simon J. Gerraty # # This file is provided in the hope that it will diff --git a/examples/functions/which b/examples/functions/which index 54ace77..ca33703 100644 --- a/examples/functions/which +++ b/examples/functions/which @@ -6,7 +6,7 @@ which() { - local aflag sflag ES a + local aflag sflag ES a opt OPTIND=1 while builtin getopts as opt ; do diff --git a/examples/functions/xfind.bash b/examples/functions/xfind.bash new file mode 100644 index 0000000..578e883 --- /dev/null +++ b/examples/functions/xfind.bash @@ -0,0 +1,52 @@ +#! /bin/bash +#From: kaz@cafe.net (Kaz Kylheku) +#Newsgroups: comp.unix.shell +#Subject: Why not roll your own @#$% find! (was: splitting directory off from filename) +#Message-ID: <6n1117$tp1@espresso.cafe.net> +#Date: Fri, 26 Jun 1998 20:47:34 GMT + +# $1 = dirname, $2 = pattern, optional $3 = action +xfind() +{ + local x + local dir="$1" + + # descend into specified directory + + builtin cd -L "$1" || { + echo "${FUNCNAME}: cannot change dir to $1" >&2 + return 1 + } + + # + # default action is to print the filename + # + if [ -n "$3" ]; then + action="$3" + else + action='printf -- "%s\n"' + fi + + # process ordinary files that match pattern + + for x in $2 ; do + if [ -f "$x" ] ; then + eval "$action" "$x" + fi + done + + # now descend into subdirectories, avoiding symbolic links + # and directories that start with a period. + + for x in * ; do + if [ -d "$x" -a ! -L "$x" ] ; then + $FUNCNAME "$x" "$2" "$action" + fi + done + + # finally, pop back up + + builtin cd -L .. +} + +#xfind "$@" |