aboutsummaryrefslogtreecommitdiffstats
path: root/examples/functions
diff options
context:
space:
mode:
authorJari Aalto <jari.aalto@cante.net>1998-04-17 19:52:44 +0000
committerJari Aalto <jari.aalto@cante.net>2009-09-12 16:46:51 +0000
commitcce855bc5b117cb7ae70064131120687bc69fac0 (patch)
tree39c7a4ec8f6d22ef03df74f2684e6a04fef10399 /examples/functions
parente8ce775db824de329b81293b4e5d8fbd65624528 (diff)
downloadandroid_external_bash-cce855bc5b117cb7ae70064131120687bc69fac0.tar.gz
android_external_bash-cce855bc5b117cb7ae70064131120687bc69fac0.tar.bz2
android_external_bash-cce855bc5b117cb7ae70064131120687bc69fac0.zip
Imported from ../bash-2.02.tar.gz.
Diffstat (limited to 'examples/functions')
-rw-r--r--examples/functions/basename243
-rw-r--r--examples/functions/getoptx.bash302
-rw-r--r--examples/functions/inetaddr44
-rw-r--r--examples/functions/inpath5
-rw-r--r--examples/functions/isnum.bash23
-rw-r--r--examples/functions/isnum222
-rw-r--r--examples/functions/jdate.bash78
-rw-r--r--examples/functions/pathfuncs48
-rw-r--r--examples/functions/shcat219
9 files changed, 581 insertions, 3 deletions
diff --git a/examples/functions/basename2 b/examples/functions/basename2
new file mode 100644
index 0000000..a42231c
--- /dev/null
+++ b/examples/functions/basename2
@@ -0,0 +1,43 @@
+#From: "Grigoriy Strokin" <grg@philol.msu.ru>
+#Newsgroups: comp.unix.shell
+#Subject: fast basename and dirname functions for BASH/SH
+#Date: Sat, 27 Dec 1997 21:18:40 +0300
+#
+#Please send your comments to grg@philol.msu.ru
+
+function basename()
+{
+ local name="${1##*/}"
+ echo "${name%$2}"
+}
+
+function dirname()
+{
+ local dir="${1%${1##*/}}"
+ [ "${dir:=./}" != "/" ] && dir="${dir%?}"
+ echo "$dir"
+}
+
+# Two additional functions:
+# 1) namename prints the basename without extension
+# 2) ext prints extension of a file, including "."
+
+function namename()
+{
+ local name=${1##*/}
+ local name0="${name%.*}"
+ echo "${name0:-$name}"
+}
+function ext()
+{
+ local name=${1##*/}
+ local name0="${name%.*}"
+ local ext=${name0:+${name#$name0}}
+ echo "${ext:-.}"
+}
+
+
+
+
+
+
diff --git a/examples/functions/getoptx.bash b/examples/functions/getoptx.bash
new file mode 100644
index 0000000..23e5de5
--- /dev/null
+++ b/examples/functions/getoptx.bash
@@ -0,0 +1,302 @@
+#From: "Grigoriy Strokin" <grg@philol.msu.ru>
+#Newsgroups: comp.unix.shell
+#Subject: BASH: getopt function that parses long-named options
+#Date: Mon, 22 Dec 1997 20:35:18 +0300
+
+#Hi, I have written a BASH function named getoptex, that is like bash builtin
+#"getopts", but does parse long-named options and optional arguments. It only
+#uses builtin bash commands, so it is very fast. In order to use it in your
+#bash scripts, include a command ". getopt.sh" (<dot> getopt.sh) to the file
+#containing your script, and that will define functions getopt, getoptex, and
+#optlistex (the file getopt.sh with its detailed description is listed
+#below).
+
+#*** file getopt.sh ***
+
+#! /bin/bash
+#
+# getopt.sh:
+# functions like getopts but do long-named options parsing
+# and support optional arguments
+#
+# Version 1.0 1997 by Grigoriy Strokin (grg@philol.msu.ru), Public Domain
+# Date created: December 21, 1997
+# Date modified: December 21, 1997
+#
+# IMPORTANT FEATURES
+#
+# 1) Parses both short and long-named options
+# 2) Supports optional arguments
+# 3) Only uses bash builtins, thus no calls to external
+# utilities such as expr or sed is done. Therefore,
+# parsing speed is high enough
+#
+#
+# DESCRIPTION
+#
+# FUNCTION getopt
+# Usage: getopt OPTLIST {"$@"|ALTERNATIVE_PARAMETERS}
+#
+# like getopts, but parse options with both required and optional arguments,
+# Options with optional arguments must have "." instead of ":" after them.
+# Furthemore, a variable name to place option name cannot be specified
+# and is always placed in OPTOPT variable
+#
+# This function is provided for compatibility with getopts()
+# OPTLIST style, and it actually calls getoptex (see bellow)
+#
+# NOTE that a list of parameters is required and must be either "$@",
+# if processing command line arguments, or some alternative parameters.
+#
+# FUNCTION getoptex
+# Usage: getoptex OPTION_LIST {"$@"|ALTERNATIVE_PARAMETERS}
+#
+# like getopts, but parse long-named options.
+#
+# Both getopt and getoptex return 0 if an option has been parsed,
+# and 1 if all options are already parsed or an error occured
+#
+# Both getopt and getoptex set or test the following variables:
+#
+# OPTERR -- tested for whether error messages must be given for invalid
+options
+#
+# OPTOPT -- set to the name of an option parsed,
+# or to "?" if no more options or error
+# OPTARG -- set to the option argument, if any;
+# unset if ther is no argument;
+# on error, set to the erroneous option name
+#
+# OPTIND -- Initialized to 1.
+# Then set to the number of the next parameter to be parsed
+# when getopt or getoptex will be called next time.
+# When all options are parsed, contains a number of
+# the first non-option argument.
+#
+#
+# OPTOFS -- If a parameter number $OPTIND containg an option parsed
+# does not contain any more options, OPTOFS is unset;
+# otherwise, OPTOFS is set to such a number of "?" signs
+# which is equal to the number of options parsed
+#
+# You might not set variables OPTIND and OPTOFS yourself
+# unless you want to parse a list of parameters more than once.
+# Otherwise, you whould unset OPTIND (or set it to 1)
+# and unset OPTOFS each time you want to parse a new parameters
+list
+#
+# Option list format is DIFFERENT from one for getopts or getopt.
+getopts-style
+# option list can be converted to getoptex-style using a function optlistex
+# (see bellow)
+#
+# DESCRIPTION of option list used with getoptex:
+# Option names are separated by whitespace. Options consiting of
+# more than one character are treated as long-named (--option)
+#
+# Special characters can appear at the and of option names specifying
+# whether an argument is required (default is ";"):
+# ";" (default) -- no argument
+# ":" -- required argument
+# "," -- optional argument
+#
+# For example, an option list "a b c help version f: file: separator."
+# defines the following options:
+# -a, -b, -c, --help, --version -- no argument
+# -f, --file -- argument required
+# --separator -- optional argument
+#
+# FUNCTION optlistex
+# Usage new_style_optlist=`optlistex OLD_STYLE_OPTLIST`
+#
+# Converts getopts-style option list in a format suitable for use with getoptex
+# Namely, it inserts spaces after each option name.
+#
+#
+# HOW TO USE
+#
+# In order o use in your bash scripts the functions described,
+# include a command ". getopt.sh" to the file containing the script,
+# which will define functions getopt, getoptex, and optlistex
+#
+# EXAMPLES
+#
+# See files 'getopt1' and 'getopt2' that contain sample scripts that use
+# getopt and getoptex functions respectively
+#
+#
+# Please send your comments to grg@philol.msu.ru
+
+function getoptex()
+{
+ let $# || return 1
+ local optlist="${1#;}"
+ let OPTIND || OPTIND=1
+ [ $OPTIND -lt $# ] || return 1
+ shift $OPTIND
+ if [ "$1" != "-" -a "$1" != "${1#-}" ]
+ then OPTIND=$[OPTIND+1]; if [ "$1" != "--" ]
+ then
+ local o
+ o="-${1#-$OPTOFS}"
+ for opt in ${optlist#;}
+ do
+ OPTOPT="${opt%[;.:]}"
+ unset OPTARG
+ local opttype="${opt##*[^;:.]}"
+ [ -z "$opttype" ] && opttype=";"
+ if [ ${#OPTOPT} -gt 1 ]
+ then # long-named option
+ case $o in
+ "--$OPTOPT")
+ if [ "$opttype" != ":" ]; then return 0; fi
+ OPTARG="$2"
+ if [ -z "$OPTARG" ];
+ then # error: must have an agrument
+ let OPTERR && echo "$0: error: $OPTOPT must have an argument" >&2
+ OPTARG="$OPTOPT";
+ OPTOPT="?"
+ return 1;
+ fi
+ OPTIND=$[OPTIND+1] # skip option's argument
+ return 0
+ ;;
+ "--$OPTOPT="*)
+ if [ "$opttype" = ";" ];
+ then # error: must not have arguments
+ let OPTERR && echo "$0: error: $OPTOPT must not have arguments" >&2
+ OPTARG="$OPTOPT"
+ OPTOPT="?"
+ return 1
+ fi
+ OPTARG=${o#"--$OPTOPT="}
+ return 0
+ ;;
+ esac
+ else # short-named option
+ case "$o" in
+ "-$OPTOPT")
+ unset OPTOFS
+ [ "$opttype" != ":" ] && return 0
+ OPTARG="$2"
+ if [ -z "$OPTARG" ]
+ then
+ echo "$0: error: -$OPTOPT must have an argument" >&2
+ OPTARG="$OPTOPT"
+ OPTOPT="?"
+ return 1
+ fi
+ OPTIND=$[OPTIND+1] # skip option's argument
+ return 0
+ ;;
+ "-$OPTOPT"*)
+ if [ $opttype = ";" ]
+ then # an option with no argument is in a chain of options
+ OPTOFS="$OPTOFS?" # move to the next option in the chain
+ OPTIND=$[OPTIND-1] # the chain still has other options
+ return 0
+ else
+ unset OPTOFS
+ OPTARG="${o#-$OPTOPT}"
+ return 0
+ fi
+ ;;
+ esac
+ fi
+ done
+ echo "$0: error: invalid option: $o"
+ fi; fi
+ OPTOPT="?"
+ unset OPTARG
+ return 1
+}
+function optlistex
+{
+ local l="$1"
+ local m # mask
+ local r # to store result
+ while [ ${#m} -lt $[${#l}-1] ]; do m="$m?"; done # create a "???..." mask
+ while [ -n "$l" ]
+ do
+ r="${r:+"$r "}${l%$m}" # append the first character of $l to $r
+ l="${l#?}" # cut the first charecter from $l
+ m="${m#?}" # cut one "?" sign from m
+ if [ -n "${l%%[^:.;]*}" ]
+ then # a special character (";", ".", or ":") was found
+ r="$r${l%$m}" # append it to $r
+ l="${l#?}" # cut the special character from l
+ m="${m#?}" # cut one more "?" sign
+ fi
+ done
+ echo $r
+}
+function getopt()
+{
+ local optlist=`optlistex "$1"`
+ shift
+ getoptex "$optlist" "$@"
+ return $?
+}
+
+#**************************************
+# cut here
+#**************************************
+#*** (end of getopt.sh) ***
+
+
+#*** file getopt1 ***
+
+#! /bin/bash
+# getopt1:
+# Sample script using the function getopt
+#
+# Type something like "getopt1 -ab -d 10 -e20 text1 text2"
+# on the command line to see how it works
+#
+# See getopt.sh for more information
+#. getopt.sh
+#echo Using getopt to parse arguments:
+#while getopt "abcd:e." "$@"
+#do
+# echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}"
+#done
+#shift $[OPTIND-1]
+#for arg in "$@"
+#do
+# echo "Non option argument <$arg>"
+#done
+#
+#**************************************
+# cut here
+#**************************************
+#*** (end of getopt1) ***
+#
+#
+#*** file getopt2 ***
+#
+#! /bin/bash
+# getopt2:
+# Sample script using the function getoptex
+#
+# Type something like "getopt2 -ab -d 10 -e20 --opt1 --opt4=100 text1 text2"
+# to see how it works
+#
+# See getopt.sh for more information
+. getopt.sh
+#echo Using getoptex to parse arguments:
+#while getoptex "a; b; c; d: e. opt1 opt2 opt3 opt4: opt5." "$@"
+#do
+# echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}"
+#done
+#shift $[OPTIND-1]
+#for arg in "$@"
+#do
+# echo "Non option argument <$arg>"
+#done
+#
+#**************************************
+# cut here
+#**************************************
+#*** (end of getopt2) ***
+
+
diff --git a/examples/functions/inetaddr b/examples/functions/inetaddr
new file mode 100644
index 0000000..776b204
--- /dev/null
+++ b/examples/functions/inetaddr
@@ -0,0 +1,44 @@
+#
+# 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
+
+ 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} ))
+
+ printf "%d.%d.%d.%d\n" $x1 $x2 $x3 $x4
+}
diff --git a/examples/functions/inpath b/examples/functions/inpath
index 7755b33..cb4c93d 100644
--- a/examples/functions/inpath
+++ b/examples/functions/inpath
@@ -1,5 +1,6 @@
inpath()
{
+ local PROG
path=$(echo $PATH | sed 's/^:/.:/
s/::/:.:/g
s/:$/:./
@@ -9,7 +10,5 @@ inpath()
do
[ -x $x/$1 ] && { PROG=$x/$1; break; }
done
- [ -z "$PROG" ]
- return
+ [ -n "$PROG" ]
}
-
diff --git a/examples/functions/isnum.bash b/examples/functions/isnum.bash
new file mode 100644
index 0000000..1eff13f
--- /dev/null
+++ b/examples/functions/isnum.bash
@@ -0,0 +1,23 @@
+#From: jrmartin@rainey.blueneptune.com (James R. Martin)
+#Newsgroups: comp.unix.shell
+#Subject: Re: testing user input on numeric or character value
+#Date: 26 Nov 1997 01:28:43 GMT
+
+# isnum returns True if its argument is a valid number,
+# and False (retval=1) if it is any other string.
+# The first pattern requires a digit before the decimal
+# point, and the second after the decimal point.
+
+# BASH NOTE: make sure you have executed `shopt -s extglob' before
+# trying to use this function, or it will not work
+
+function isnum # string
+{
+ case $1 in
+ ?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
+ return 0;;
+ ?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) )
+ return 0;;
+ *) return 1;;
+ esac
+}
diff --git a/examples/functions/isnum2 b/examples/functions/isnum2
new file mode 100644
index 0000000..e2e7a5f
--- /dev/null
+++ b/examples/functions/isnum2
@@ -0,0 +1,22 @@
+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
+}
diff --git a/examples/functions/jdate.bash b/examples/functions/jdate.bash
new file mode 100644
index 0000000..9488ed9
--- /dev/null
+++ b/examples/functions/jdate.bash
@@ -0,0 +1,78 @@
+#From: damatex@CAM.ORG (Mario Boudreault)
+#Newsgroups: comp.unix.shell
+#Subject: JULIAN DATE CONVERSION SUB
+#Date: 4 Aug 1995 10:23:28 -0400
+#Message-ID: <3vtah0$jb3@ocean.CAM.ORG>
+
+#For those using shells and who want to convert dates to a julian number
+#here is a shell script (wihtout validation) that can be used as a base
+#program for your shell scripts.
+
+#Special thanks to Ed Ferguson@ti.com who sent me the algorithm to compute
+#that date.
+
+#
+# MODIFIED BY CHET RAMEY TO CONVERT TO bash v2 SYNTAX
+#
+
+# cnvdate - Conversion de dates en julienne et vice et versa...
+#
+# Par : Mario Boudreault Damatex Inc Montreal, Canada
+# Date: 2 Aout 1995
+# Rev.: 2 Aout 1995
+#
+# Usage:
+# cvdate [-j] YYYMMDD pour convertir en nbre de jours
+# cvdate -d {julian number} pour convertir en AAAAMMJJ
+#
+
+jul_date()
+{
+ #
+ # Separe ANNEE, MOIS et JOUR...
+ #
+ YEAR=`echo $DATE | awk ' { print substr($0,1,4) } '`
+ MONTH=`echo $DATE | awk ' { print substr($0,5,2) } '`
+ DAY=`echo $DATE | awk ' { print substr($0,7,2) } '`
+ #
+ # Execute la formule magique...
+ #
+ A=$(( $DAY - 32075 + 1461 * ( $YEAR + 4800 - ( 14 - $MONTH ) / 12 ) \
+ / 4 + 367 * ( $MONTH - 2 + ( 14 - $MONTH ) / 12 * 12 ) / 12 - \
+ 3 * ( ( $YEAR + 4900 - ( 14 - $MONTH ) / 12 ) / 100 ) / 4 ))
+ echo $A
+}
+
+day_date()
+{
+ TEMP1=$(( $DATE + 68569 ))
+ TEMP2=$(( 4 * $TEMP1 / 146097 ))
+ TEMP1=$(( $TEMP1 - ( 146097 * $TEMP2 + 3 ) / 4 ))
+ Y=$(( 4000 * ( $TEMP1 + 1 ) / 1461001 ))
+ TEMP1=$(( $TEMP1 - 1461 * $Y / 4 + 31 ))
+ M=$(( 80 * $TEMP1 / 2447 ))
+ D=$(( $TEMP1 - 2447 * $M / 80 ))
+ TEMP1=$(( $M / 11 ))
+ M=$(( $M + 2 - 12 * $TEMP1 ))
+ Y=$(( 100 * ( $TEMP2 - 49 ) + $Y + $TEMP1 ))
+ M=`echo $M | awk ' { M=$0 ; if ( length($0) == 1 ) M="0"$0 } END { print M } '`
+ D=`echo $D | awk ' { D=$0 ; if ( length($0) == 1 ) D="0"$0 } END { print D } '`
+ echo $Y$M$D
+}
+
+# main()
+
+if [ $# -eq 1 ]; then
+ DATE=$1
+ jul_date
+elif [ "$1" = '-j' ]; then
+ DATE=$2
+ jul_date
+elif [ "$1" = '-d' ]; then
+ DATE=$2
+ day_date
+fi
+#
+# Termine
+#
+exit 0
diff --git a/examples/functions/pathfuncs b/examples/functions/pathfuncs
new file mode 100644
index 0000000..47896bf
--- /dev/null
+++ b/examples/functions/pathfuncs
@@ -0,0 +1,48 @@
+#From: "Simon J. Gerraty" <sjg@zen.void.oz.au>
+#Message-Id: <199510091130.VAA01188@zen.void.oz.au>
+#Subject: Re: a shell idea?
+#Date: Mon, 09 Oct 1995 21:30:20 +1000
+
+
+# NAME:
+# add_path.sh - add dir to path
+#
+# DESCRIPTION:
+# These functions originated in /etc/profile and ksh.kshrc, but
+# are more useful in a separate file.
+#
+# SEE ALSO:
+# /etc/profile
+#
+# 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
+# be of use. There is absolutely NO WARRANTY.
+# Permission to copy, redistribute or otherwise
+# use this file is hereby granted provided that
+# the above copyright notice and this notice are
+# left intact.
+
+# is $1 missing from $2 (or PATH) ?
+no_path() {
+ eval "case :\$${2-PATH}: in *:$1:*) return 1;; *) return 0;; esac"
+}
+# if $1 exists and is not in path, append it
+add_path () {
+ [ -d ${1:-.} ] && no_path $* && eval ${2:-PATH}="\$${2:-PATH}:$1"
+}
+# if $1 exists and is not in path, prepend it
+pre_path () {
+ [ -d ${1:-.} ] && no_path $* && eval ${2:-PATH}="$1:\$${2:-PATH}"
+}
+# if $1 is in path, remove it
+del_path () {
+ no_path $* || eval ${2:-PATH}=`eval echo :'$'${2:-PATH}: |
+ sed -e "s;:$1:;:;g" -e "s;^:;;" -e "s;:\$;;"`
+}
diff --git a/examples/functions/shcat2 b/examples/functions/shcat2
new file mode 100644
index 0000000..6fe90f4
--- /dev/null
+++ b/examples/functions/shcat2
@@ -0,0 +1,19 @@
+shcat()
+{
+ while read -r line
+ do
+ echo "$line"
+ done
+}
+
+shcat2()
+{
+ while [ $# -ge 1 ]; do
+ case "$1" in
+ -) shcat ;;
+ *) shcat < "$1" ;;
+ esac
+ shift
+ done
+ exit 0
+}