summaryrefslogtreecommitdiff
path: root/examples/functions
diff options
context:
space:
mode:
authorJari Aalto <jari.aalto@cante.net>2001-04-06 19:14:31 +0000
committerJari Aalto <jari.aalto@cante.net>2009-09-12 16:46:53 +0000
commit28ef6c316f1aff914bb95ac09787a3c83c1815fd (patch)
tree2812fe7ffc9beec4f99856906ddfcafda54cf16a /examples/functions
parentbb70624e964126b7ac4ff085ba163a9c35ffa18f (diff)
downloadbash-28ef6c316f1aff914bb95ac09787a3c83c1815fd.tar.gz
Imported from ../bash-2.05.tar.gz.
Diffstat (limited to 'examples/functions')
-rw-r--r--examples/functions/array-to-string15
-rw-r--r--examples/functions/emptydir28
-rw-r--r--examples/functions/fact2
-rw-r--r--examples/functions/gethtml35
-rw-r--r--examples/functions/ksh-cd35
-rw-r--r--examples/functions/kshenv44
-rw-r--r--examples/functions/lowercase2
-rw-r--r--examples/functions/recurse64
-rw-r--r--examples/functions/sort-pos-params50
9 files changed, 257 insertions, 18 deletions
diff --git a/examples/functions/array-to-string b/examples/functions/array-to-string
new file mode 100644
index 00000000..0d2fbe50
--- /dev/null
+++ b/examples/functions/array-to-string
@@ -0,0 +1,15 @@
+#! /bin/bash
+
+# Format: array_to_string vname_of_array vname_of_string separator
+array_to_string()
+{
+ (( ($# < 2) || ($# > 3) )) && {
+ "$FUNCNAME: usage: $FUNCNAME arrayname stringname [separator]"
+ return 2
+ }
+
+ local array=$1 string=$2
+ ((3==$#)) && [[ $3 = ? ]] && local IFS="${3}${IFS}"
+ eval $string="\"\${$array[*]}\""
+ return 0
+}
diff --git a/examples/functions/emptydir b/examples/functions/emptydir
new file mode 100644
index 00000000..412af5b1
--- /dev/null
+++ b/examples/functions/emptydir
@@ -0,0 +1,28 @@
+#! /bin/bash
+#
+#Derived from:
+#
+#From: damercer@mmm.com (Dan Mercer)
+#Newsgroups: comp.unix.admin,comp.unix.shell,comp.unix.programmer,comp.sys.sun.admin
+#Subject: Re: Command to find out if a directory is empty
+#Date: 17 Aug 2000 14:35:56 GMT
+#Message-ID: <8ngt8c$fmr$1@magnum.mmm.com>
+
+# usage: emptydir [dirname] ; default dirname is "."
+
+emptydir()
+{
+ typeset file dir=${1:-.}
+ [[ -d $dir ]] || {
+ echo "$FUNCNAME: $dir is not a directory" >&2
+ return 2
+ }
+ for file in $dir/.* $dir/*
+ do
+ case ${file#$dir/} in
+ .|..) ;;
+ \*) [[ -e $file ]];let $?;return;;
+ *) return 1;;
+ esac
+ done
+}
diff --git a/examples/functions/fact b/examples/functions/fact
index cd7bf467..97efd498 100644
--- a/examples/functions/fact
+++ b/examples/functions/fact
@@ -9,5 +9,5 @@ fact ()
echo 1
return ;
fi;
- echo $[ $num * $(fact $[ $num - 1 ])]
+ echo $(( $num * $(fact $(( $num - 1 )) ) ))
}
diff --git a/examples/functions/gethtml b/examples/functions/gethtml
new file mode 100644
index 00000000..2eec1d89
--- /dev/null
+++ b/examples/functions/gethtml
@@ -0,0 +1,35 @@
+#
+# get_html -- get a web page from a remote server
+#
+# Original Author: Jeff Korn <jlk@cs.princeton.edu>
+# Modified for bash by Chet Ramey <chet@po.cwru.edu>
+#
+# Example: get_html cnswww.cns.cwru.edu /~chet/ | more
+
+get_html()
+{
+ local host port
+
+ (($# < 2)) && {
+ echo "usage: $FUNCNAME hostname path [port]" >&2
+ return 1
+ }
+
+ host="$1"
+ port="${3:-80}"
+
+ exec 3<> /dev/tcp/$host/$port || {
+ echo "$FUNCNAME: $host/$port: cannot connect" >&2
+ exit 1
+ }
+
+ echo -e "GET $2 HTTP/1.0\n" >&3
+
+ cat <&3
+
+ exec 3<&-
+
+ return 0
+}
+
+get_html "$@"
diff --git a/examples/functions/ksh-cd b/examples/functions/ksh-cd
new file mode 100644
index 00000000..801a4909
--- /dev/null
+++ b/examples/functions/ksh-cd
@@ -0,0 +1,35 @@
+#
+# 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
+}
diff --git a/examples/functions/kshenv b/examples/functions/kshenv
index 35cc2124..2b9a6ebb 100644
--- a/examples/functions/kshenv
+++ b/examples/functions/kshenv
@@ -109,27 +109,39 @@ $1 == "'$cmd'" && $2 == "()" {printit=0; next; }
# 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 "$HOME" ;;
- 1) builtin cd "$@" ;;
- 2) old="$1"
- new="$2"
- # dir=$(echo "$PWD" | sed "s:$old:$new:g")
- dir=${PWD//$old/$new}
- case "$dir" in
- "$PWD") case "$PWD" in
- *$old*) ;;
- *) echo "$FUNCNAME: bad substitution" >&2 ; return 1 ;;
- esac ;;
- *) echo "$dir"
- builtin cd "$dir"
- ;;
+ 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 "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
- return 1 ;;
+ *) echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
+ return 2 ;;
esac
}
diff --git a/examples/functions/lowercase b/examples/functions/lowercase
index e3f866e0..3cf6bde2 100644
--- a/examples/functions/lowercase
+++ b/examples/functions/lowercase
@@ -11,7 +11,7 @@ lowercase()
for file; do
[ -f "$file" ] || continue
filename=${file##*/}
- case "$filename" in
+ case "$file" in
*/*) dirname=${file%/*} ;;
*) dirname=.;;
esac
diff --git a/examples/functions/recurse b/examples/functions/recurse
new file mode 100644
index 00000000..14c4b9e3
--- /dev/null
+++ b/examples/functions/recurse
@@ -0,0 +1,64 @@
+#!/bin/bash
+
+#From: kaz@ashi.footprints.net (Kaz Kylheku)
+#Newsgroups: comp.os.linux.misc
+#Subject: Re: bash question: subdirectories
+#Message-ID: <slrn8a0gu9.v5n.kaz@ashi.FootPrints.net>
+#Date: Tue, 08 Feb 2000 16:24:35 GMT
+
+#Actually it can be made to. That is to say, it is possible to code a recursive
+#descender function in the bash language. Here is an example.
+#
+#What is nice about this is that you can embed the function into your shell
+#script. The function changes the current working directory as it descends.
+#So it can handle arbitrarily deep paths. Whereas paths generated by the
+#find command can cause a problem when they get too long; the kernel has a
+#hard limit on the length of the string passed to the open() and other
+#system calls.
+
+#There are races; what if the directory tree is blown away during the traversal?
+#The function won't be able to crawl back up using the .. link and will just
+#bail.
+
+# Recursive Directory Traverser
+# Author: Kaz Kylheku
+# Date: Feb 27, 1999
+# Copyright 1999
+
+# Function parameter usage:
+# $1 directory to search
+# $2 pattern to search for
+# $3 command to execute
+# $4 secret argument for passing down path
+
+function recurse
+{
+ local file
+ local path
+
+ if [ "$4" = "" ] ; then
+ path="${1%/}/"
+ else
+ path="$4$1/"
+ fi
+
+ if cd "$1" ; then
+ for file in $2; do
+ if [ -f "$file" -o -d "$file" ]; then
+ eval "$3"
+ fi
+ done
+ for file in .* * ; do
+ if [ "$file" = "." -o "$file" = ".." ] ; then
+ continue
+ fi
+ if [ -d "$file" -a ! -L "$file" ]; then
+ recurse "$file" "$2" "$3" "$path"
+ fi
+ done
+ cd ..
+ fi
+}
+
+recurse "$1" "$2" 'echo "$path$file"'
+
diff --git a/examples/functions/sort-pos-params b/examples/functions/sort-pos-params
new file mode 100644
index 00000000..0052b466
--- /dev/null
+++ b/examples/functions/sort-pos-params
@@ -0,0 +1,50 @@
+# 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