aboutsummaryrefslogtreecommitdiffstats
path: root/examples/scripts
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/scripts
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/scripts')
-rw-r--r--examples/scripts/fixfiles.bash92
-rw-r--r--examples/scripts/hanoi.bash21
-rwxr-xr-xexamples/scripts/krand.bash74
-rw-r--r--examples/scripts/randomcard.bash18
-rwxr-xr-xexamples/scripts/scrollbar10
-rwxr-xr-xexamples/scripts/scrollbar224
-rw-r--r--examples/scripts/showperm.bash53
-rw-r--r--examples/scripts/timeout53
8 files changed, 340 insertions, 5 deletions
diff --git a/examples/scripts/fixfiles.bash b/examples/scripts/fixfiles.bash
new file mode 100644
index 0000000..67311ee
--- /dev/null
+++ b/examples/scripts/fixfiles.bash
@@ -0,0 +1,92 @@
+#! /bin/bash
+#
+# From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
+# Newsgroups: comp.unix.shell,comp.os.linux.misc
+# Subject: GNU Bash Script to fix filenames
+# Date: 28 Mar 1996 14:54:43 -0800
+# Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
+#
+#This is a script which takes a list of directories, descends through each one
+#and ``corrects'' filenames that:
+#
+# - contain filename globbing characters: * ? [ ]
+# - quote characters: ' "
+# - control characters: 0-31 (127 is not dealt with---oops)
+# - - or + as the first character
+#
+# The GNU version of 'tr' is required. Also requires 'sed'.
+#
+# Script to process a given list of directories recursively
+# and rename each file to something that is reasonable.
+#
+# The rules are:
+#
+# 1. replace each space, [, ], *, ", and ' character in the name with a
+# period.
+# 2. replace each control character 1..31 with a printable character obtained
+# by adding 64 to the ascii value. ^A becomes A, ^B becomes B and so on.
+# 3. replace a - or + occuring at the beginning of the name with a #
+#
+# 4. if the resulting name has been changed in any way, then
+# 5. if a file of the new name already exists, then
+# 6. add a . to the new name and goto step 5.
+# 7. rename the old name to the new name
+#
+# written by Kaz Kylheku <kaz@cafe.net>
+# March 1996
+# Vancouver, Canada
+#
+# requires GNU 'bash', GNU 'tr', and some sort of 'sed' program.
+#
+# minimal conversion to bash v2 syntax done by Chet Ramey
+
+processfile()
+{
+ new_name="`echo -n $1 | tr '\173\175\052\077\042\047 ' '.......' |
+ tr '[\000-\037]' '[\100-\137]' |
+ sed -e 's/^-/#/' -e 's/+/#/'`"
+ if [ "$new_name" != "$1" ] ; then
+ while [ -e "$new_name" ] ; do
+ new_name="${new_name}."
+ done
+ echo changing \"$1\" to \"$new_name\" in `pwd`
+ mv -- "$1" "$new_name"
+ fi
+}
+
+processdir()
+{
+ set -f
+ local savepwd="$PWD"
+ if cd "$1" ; then
+ set +f
+ for file in * ; do
+ set -f
+ if [ "$file" != "." -a "$file" != ".." ] ; then
+ if [ -L "$file" ] ; then
+ echo "skipping symlink" $file in `pwd`
+ elif [ -d "$file" ] ; then
+ processdir "$file"
+ elif [ -f "$file" ] ; then
+ processfile "$file"
+ fi
+ fi
+ done
+ cd "$savepwd"
+ fi
+}
+
+shopt -s nullglob dotglob
+
+if [ $# = 0 ] ; then
+ echo "$0: must specify a list of directories" >&2
+ echo "$0: usage: $0 directory [directory ...]" >&2
+ exit 2
+fi
+
+while [ $# != 0 ] ; do
+ processdir "$1"
+ shift
+done
+
+exit 0
diff --git a/examples/scripts/hanoi.bash b/examples/scripts/hanoi.bash
new file mode 100644
index 0000000..c308125
--- /dev/null
+++ b/examples/scripts/hanoi.bash
@@ -0,0 +1,21 @@
+# Towers of Hanoi in bash
+#
+# cribbed from the ksh93 book, example from exercises on page 85
+#
+# Chet Ramey
+# chet@po.cwru.edu
+
+hanoi() # n from to spare
+{
+ typeset -i nm1=$1-1
+ ((nm1>0)) && hanoi $nm1 $2 $4 $3
+ echo "Move disc $2 to $3"
+ ((nm1>0)) && hanoi $nm1 $4 $3 $2
+}
+
+case $1 in
+[1-9])
+ hanoi $1 1 2 3;;
+*) echo "${0##*/}: Argument must be from 1 to 9"
+ exit 1;;
+esac
diff --git a/examples/scripts/krand.bash b/examples/scripts/krand.bash
new file mode 100755
index 0000000..dfdfd32
--- /dev/null
+++ b/examples/scripts/krand.bash
@@ -0,0 +1,74 @@
+# Originally
+#
+# From: bsh20858@news.fhda.edu (Brian S Hiles)
+# Newsgroups: comp.unix.shell
+# Subject: Re: getting random numbers
+# Date: 23 Jan 1997 23:27:30 GMT
+# Message-ID: <5c8s52$eif@tiptoe.fhda.edu>
+
+# @(#) krand Produces a random number within integer limits
+# "krand" Korn shell script generates a random number in a
+# specified range with an optionally specified ``seed'' value.
+# Author: Peter Turnbull, May 1993
+# Modified by: Becca Thomas, January 1994
+
+# changed the optional third argument to a -s option, converted to
+# bash v2 syntax -- chet@po.cwru.edu
+
+PROGNAME=${0##*/}
+USAGE="usage: $PROGNAME [-s seed] lower-limit upper-limit"
+
+Seed=$$ # Initialize random-number seed value with PID
+
+usage()
+{
+ echo ${PROGNAME}: "$USAGE" >&2
+}
+
+errexit()
+{
+ echo ${PROGNAME}: "$@" >&2
+ exit 1
+}
+
+# Process command-line arguments:
+while getopts "s:" opt; do
+ case "$opt" in
+ s) Seed=$OPTARG ;;
+ *) usage ; exit 2;;
+ esac
+done
+
+shift $(($OPTIND - 1))
+
+case $# in
+ 2) Lower=$1; Upper=$2 ;;
+ *) usage ; exit 2;;
+esac
+
+# Check that specified values are integers:
+expr "$Lower" + 0 >/dev/null 2>&1
+[ $? -eq 2 ] && { errexit "lower ($Lower) not an integer"; }
+expr "$Upper" + 0 >/dev/null 2>&1
+[ $? -eq 2 ] && { errexit "upper ($Upper) not an integer"; }
+expr "$Seed" + 0 >/dev/null 2>&1
+[ $? -eq 2 ] && { errexit "seed ($Seed) not an integer"; }
+
+# Check that values are in the correct range:
+if (( "$Lower" < 0 )) || [ ${#Lower} -gt 5 ]; then
+ errexit "lower limit ($Lower) less than zero"
+fi
+if (( "$Upper" > 32767 )) || [ ${#Upper} -gt 5 ]; then
+ errexit "upper limit ($Upper) greater than 32767"
+fi
+if (( "$Seed" < 0 )) || (( "$Seed" > 32767 )) || [ ${#Seed} -gt 5 ]; then
+ errexit "seed value ($Seed) out of range (0 to 32767)"
+fi
+(( "$Upper" <= "$Lower" )) && errexit "upper limit ($Upper) <= lower limit ($Lower)"
+
+# Seed the random-number generator:
+RANDOM=$Seed
+# Compute value, scaled within range:
+let rand="$RANDOM % ($Upper - $Lower + 1) + $Lower"
+# Report result:
+echo $rand
diff --git a/examples/scripts/randomcard.bash b/examples/scripts/randomcard.bash
new file mode 100644
index 0000000..9cb6b50
--- /dev/null
+++ b/examples/scripts/randomcard.bash
@@ -0,0 +1,18 @@
+# The following prints a random card from a card deck.
+#
+# cribbed from the ksh93 book, example from page 70
+#
+# chet@po.cwru.edu
+#
+declare -i i=0
+
+# load the deck
+for suit in clubs diamonds hearts spades; do
+ for n in ace 2 3 4 5 6 7 8 9 10 jack queen king; do
+ card[i]="$n of $suit"
+ i=i+1 # let is not required with integer variables
+ done
+done
+
+# and print a random card
+echo ${card[RANDOM%52]}
diff --git a/examples/scripts/scrollbar b/examples/scripts/scrollbar
index c177179..388bea8 100755
--- a/examples/scripts/scrollbar
+++ b/examples/scripts/scrollbar
@@ -9,17 +9,17 @@
# converted from ksh syntax to bash v2 syntax by Chet Ramey
WIDTH=${COLUMNS:-80}
+WMINUS=$(( $WIDTH - 1 ))
[ $# -lt 1 ] && set -- TESTING
-# Posix.2 compatible printf command or bash loadable builtin
-# in examples/loadables/printf
+# use the bash-2.02 printf builtin
Text=$(printf "%-${WIDTH}s" "$*")
-Text=$(echo "$Text" | tr ' ' '_')
+Text=${Text// /_}
while :
do
printf "%-.${WIDTH}s\r" "$Text"
- LastC=$(expr "$Text" : '.*\(.\)$')
- Text=$(printf "%-.${WIDTH}s" "$LastC$Text")
+ LastC=${Text:${WMINUS}:1}
+ Text="$LastC""${Text%?}"
done
diff --git a/examples/scripts/scrollbar2 b/examples/scripts/scrollbar2
new file mode 100755
index 0000000..0e53634
--- /dev/null
+++ b/examples/scripts/scrollbar2
@@ -0,0 +1,24 @@
+#!/bin/bash
+#
+# scrollbar - display scrolling text
+#
+# usage: scrollbar args
+#
+# A cute hack originally from Heiner Steven <hs@bintec.de>
+#
+# converted from ksh syntax to bash v2 syntax by Chet Ramey
+
+WIDTH=${COLUMNS:-80}
+WMINUS=$(( $WIDTH - 1 ))
+
+[ $# -lt 1 ] && set -- TESTING
+
+# use the bash-2.02 printf builtin
+Text=$(printf "%-${WIDTH}s" "$*")
+
+while :
+do
+ printf "%-.${WIDTH}s\r" "$Text"
+ LastC=${Text:${WMINUS}:1}
+ Text="$LastC""${Text%?}"
+done
diff --git a/examples/scripts/showperm.bash b/examples/scripts/showperm.bash
new file mode 100644
index 0000000..2e06c0b
--- /dev/null
+++ b/examples/scripts/showperm.bash
@@ -0,0 +1,53 @@
+#Newsgroups: comp.unix.shell
+#From: gwc@root.co.uk (Geoff Clare)
+#Subject: Re: Determining permissions on a file
+#Message-ID: <Dr79nw.DtL@root.co.uk>
+#Date: Fri, 10 May 1996 17:23:56 GMT
+
+#Here's a bit of Korn shell that converts the symbolic permissions produced
+#by "ls -l" into octal, using only shell builtins. How to create a script
+#combining this with an "ls -l" is left as an exercise...
+#
+#
+# Converted to Bash v2 syntax by Chet Ramey <chet@po.cwru.edu>
+#
+# usage: showperm modestring
+#
+# example: showperm '-rwsr-x--x'
+#
+
+[ -z "$1" ] && {
+ echo "showperm: usage: showperm modestring" >&2
+ exit 2
+}
+
+tmode="$1"
+
+typeset -i omode sbits
+typeset pmode
+
+# check for set-uid, etc. bits
+sbits=0
+case $tmode in
+???[sS]*) (( sbits += 8#4000 )) ;; # set-uid
+??????[sSl]*) (( sbits += 8#2000 )) ;; # set-gid or mand. lock
+?????????[tT]*) (( sbits += 8#1000 )) ;; # sticky
+esac
+
+omode=0
+while :
+do
+ tmode=${tmode#?}
+ case $tmode in
+ "") break ;;
+ [-STl]*) (( omode *= 2 )) ;;
+ [rwxst]*) (( omode = omode*2 + 1 )) ;;
+ *) echo "$0: first letter of \"$tmode\" is unrecognized" >&2
+ (( omode *= 2 ))
+ ;;
+ esac
+done
+
+(( omode += sbits ))
+
+printf "0%o\n" $omode
diff --git a/examples/scripts/timeout b/examples/scripts/timeout
new file mode 100644
index 0000000..ac8d88f
--- /dev/null
+++ b/examples/scripts/timeout
@@ -0,0 +1,53 @@
+#Newsgroups: comp.unix.admin,comp.unix.solaris,comp.unix.shell
+#From: gwc@root.co.uk (Geoff Clare)
+#Subject: Re: timeout -t <sec> <unix command> (Re: How to give rsh a shorter timeout?)
+#Message-ID: <EoBxrs.223@root.co.uk>
+#Date: Fri, 13 Feb 1998 18:23:52 GMT
+
+#
+# Conversion to bash v2 syntax done by Chet Ramey <chet@po.cwru.edu
+# UNTESTED
+#
+
+prog=${0##*/}
+usage="usage: $prog [-signal] [timeout] [:interval] [+delay] [--] <command>"
+
+SIG=-TERM # default signal sent to the process when the timer expires
+timeout=60 # default timeout
+interval=15 # default interval between checks if the process is still alive
+delay=2 # default delay between posting the given signal and
+ # destroying the process (kill -KILL)
+
+while :
+do
+ case $1 in
+ --) shift; break ;;
+ -*) SIG=$1 ;;
+ [0-9]*) timeout=$1 ;;
+ :*) EXPR='..\(.*\)' ; interval=`expr x"$1" : "$EXPR"` ;;
+ +*) EXPR='..\(.*\)' ; delay=`expr x"$1" : "$EXPR"` ;;
+ *) break ;;
+ esac
+ shift
+done
+
+case $# in
+0) echo "$prog: $usage" >&2 ; exit 2 ;;
+esac
+
+(
+ for t in $timeout $delay
+ do
+ while (( $t > $interval ))
+ do
+ sleep $interval
+ kill -0 $$ || exit
+ t=$(( $t - $interval ))
+ done
+ sleep $t
+ kill $SIG $$ && kill -0 $$ || exit
+ SIG=-KILL
+ done
+) 2> /dev/null &
+
+exec "$@"