diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 15:38:56 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 15:38:56 +0000 |
commit | 6c20c8ed2cb9ab69a1a57ccb2b9b79969a808321 (patch) | |
tree | f63ce19d57fad3ac4a15bc26dbfbfa2b834111b5 /examples/scripts | |
parent | Initial commit. (diff) | |
download | bash-6c20c8ed2cb9ab69a1a57ccb2b9b79969a808321.tar.xz bash-6c20c8ed2cb9ab69a1a57ccb2b9b79969a808321.zip |
Adding upstream version 5.2.15.upstream/5.2.15upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/scripts')
-rw-r--r-- | examples/scripts/bcalc | 104 | ||||
-rw-r--r-- | examples/scripts/cat.sh | 12 | ||||
-rw-r--r-- | examples/scripts/center | 41 | ||||
-rw-r--r-- | examples/scripts/inpath | 19 | ||||
-rw-r--r-- | examples/scripts/shprompt | 153 | ||||
-rw-r--r-- | examples/scripts/spin.bash | 37 | ||||
-rw-r--r-- | examples/scripts/xterm_title | 44 | ||||
-rw-r--r-- | examples/scripts/zprintf | 43 |
8 files changed, 453 insertions, 0 deletions
diff --git a/examples/scripts/bcalc b/examples/scripts/bcalc new file mode 100644 index 0000000..bc7e2b4 --- /dev/null +++ b/examples/scripts/bcalc @@ -0,0 +1,104 @@ +#! /bin/bash +# +# bcalc - a coproc example that uses bc to evaluate floating point expressions +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# If supplied command-line arguments, it uses them as the expression to have +# bc evaluate, and exits after reading the result. Otherwise, it enters an +# interactive mode, reading expressions and passing them to bc for evaluation, +# with line editing and history. +# +# You could even use this to write bc programs, but you'd have to rework the +# single-line REPL a little bit to do that (and get over the annoying timeout +# on the read) +# +# Chet Ramey +# chet.ramey@case.edu + +# we force stderr to avoid synchronization issues on calculation errors, even +# with the read timeout +init() +{ + coproc BC { bc -q 2>&1; } + # set scale + printf "scale = 10\n" >&${BC[1]} + # bash automatically sets BC_PID to the coproc pid; we store it so we + # can be sure to use it even after bash reaps the coproc and unsets + # the variables + coproc_pid=$BC_PID +} + +# not strictly necessary; the pipes will be closed when the program exits +# but we can use it in reset() below +fini() +{ + eval exec "${BC[1]}>&- ${BC[0]}<&-" +} + +reset() +{ + fini # close the old pipes + + sleep 1 + kill -1 $coproc_pid >/dev/null 2>&1 # make sure the coproc is dead + unset coproc_pid + + init +} + +# set a read timeout of a half second to avoid synchronization problems +calc() +{ + printf "%s\n" "$1" >&${BC[1]} + read -t 0.5 ANSWER <&${BC[0]} +} + +init + +# if we have command line options, process them as a single expression and +# print the result. we could just run `bc <<<"scale = 10 ; $*"' and be done +# with it, but we init the coproc before this and run the calculation through +# the pipes in case we want to do something else with the answer + +if [ $# -gt 0 ] ; then + calc "$*" + printf "%s\n" "$ANSWER" + fini + exit 0 +fi + +# we don't want to save the history anywhere +unset HISTFILE + +while read -e -p 'equation: ' EQN +do + case "$EQN" in + '') continue ;; + exit|quit) break ;; + reset) reset ; continue ;; + esac + + # save to the history list + history -s "$EQN" + + # run it through bc + calc "$EQN" + if [ -n "$ANSWER" ] ; then + printf "%s\n" "$ANSWER" + fi +done +fini + +exit 0 diff --git a/examples/scripts/cat.sh b/examples/scripts/cat.sh new file mode 100644 index 0000000..3e65b3f --- /dev/null +++ b/examples/scripts/cat.sh @@ -0,0 +1,12 @@ +shcat() +{ + while read -r ; do + printf "%s\n" "$REPLY" + done +} + +if [ -n "$1" ]; then + shcat < "$1" +else + shcat +fi diff --git a/examples/scripts/center b/examples/scripts/center new file mode 100644 index 0000000..dbe6133 --- /dev/null +++ b/examples/scripts/center @@ -0,0 +1,41 @@ +#! /bin/bash +# +# center - center a group of lines +# +# tabs in the lines might cause this to look a little bit off +# +# +# Chet Ramey <chet.ramey@case.edu> +# +# Copyright 1999 Chester Ramey +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# TThis program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +width=${COLUMNS:-80} + +if [[ $# == 0 ]] +then + set -- /dev/stdin +fi + +for file +do + while read -r + do + printf "%*s\n" $(( (width+${#REPLY})/2 )) "$REPLY" + done < $file +done + +exit 0 diff --git a/examples/scripts/inpath b/examples/scripts/inpath new file mode 100644 index 0000000..95f28bc --- /dev/null +++ b/examples/scripts/inpath @@ -0,0 +1,19 @@ +#! /bin/sh +# +# Search $PATH for a file the same name as $1; return TRUE if found. +# + +command=$1 +[ -n "$command" ] || exit 1 + +set `echo $PATH | sed 's/^:/.:/ + s/::/:.:/g + s/:$/:./ + s/:/ /g'` + +while [ $# -ne 0 ] ; do + [ -f $1/$command ] && exit 0 # test -x not universal + shift +done + +exit 1 diff --git a/examples/scripts/shprompt b/examples/scripts/shprompt new file mode 100644 index 0000000..098c45f --- /dev/null +++ b/examples/scripts/shprompt @@ -0,0 +1,153 @@ +# +# shprompt -- give a prompt and get an answer satisfying certain criteria +# +# shprompt [-dDfFsy] prompt +# s = prompt for string +# f = prompt for filename +# F = prompt for full pathname to a file or directory +# d = prompt for a directory name +# D = prompt for a full pathname to a directory +# y = prompt for y or n answer +# +# Chet Ramey +# chet@ins.CWRU.Edu +# +# Copyright 2002 Chester Ramey +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# TThis program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +type=file + +OPTS=dDfFsy + +succeed() +{ + echo "$1" + exit 0 +} + +while getopts "$OPTS" c +do + case "$c" in + s) type=string + ;; + f) type=file + ;; + F) type=path + ;; + d) type=dir + ;; + D) type=dirpath + ;; + y) type=yesno + ;; + ?) echo "usage: $0 [-$OPTS] prompt" 1>&2 + exit 2 + ;; + esac +done + +if [ "$OPTIND" -gt 1 ] ; then + shift $(( $OPTIND - 1 )) +fi + +while : +do + case "$type" in + string) + echo -n "$1" 1>&2 + read ans || exit 1 + if [ -n "$ans" ] ; then + succeed "$ans" + fi + ;; + file|path) + echo -n "$1" 1>&2 + read ans || exit 1 + # + # use `fn' and eval so that bash will do tilde expansion for + # me + # + eval fn="$ans" + case "$fn" in + /*) if test -e "$fn" ; then + succeed "$fn" + else + echo "$0: '$fn' does not exist" 1>&2 + fi + ;; + *) if [ "$type" = "path" ] ; then + echo "$0: must give full pathname to file" 1>&2 + else + if test -e "$fn" ; then + succeed "$fn" + else + echo "$0: '$fn' does not exist" 1>&2 + fi + fi + ;; + esac + ;; + dir|dirpath) + echo -n "$1" 1>&2 + read ans || exit 1 + # + # use `fn' and eval so that bash will do tilde expansion for + # me + # + eval fn="$ans" + case "$fn" in + /*) if test -d "$fn" ; then + succeed "$fn" + elif test -e "$fn" ; then + echo "$0 '$fn' is not a directory" 1>&2 + else + echo "$0: '$fn' does not exist" 1>&2 + fi + ;; + *) if [ "$type" = "dirpath" ] ; then + echo "$0: must give full pathname to directory" 1>&2 + else + if test -d "$fn" ; then + succeed "$fn" + elif test -e "$fn" ; then + echo "$0 '$fn' is not a directory" 1>&2 + else + echo "$0: '$fn' does not exist" 1>&2 + fi + fi + ;; + esac + ;; + yesno) + echo -n "$1" 1>&2 + read ans || exit 1 + case "$ans" in + y|Y|[yY][eE][sS]) + succeed "yes" + ;; + n|N|[nN][oO]) + succeed "no" + exit 0 + ;; + *) + echo "$0: yes or no required" 1>&2 + ;; + esac + ;; + esac +done + +exit 1 diff --git a/examples/scripts/spin.bash b/examples/scripts/spin.bash new file mode 100644 index 0000000..9fa9125 --- /dev/null +++ b/examples/scripts/spin.bash @@ -0,0 +1,37 @@ +#!/bin/bash +# +# spin.bash -- provide a `spinning wheel' to show progress +# +# Chet Ramey +# chet@po.cwru.edu +# +# Copyright 1997 Chester Ramey +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# TThis program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +bs=$'\b' + +chars="|${bs} \\${bs} -${bs} /${bs}" + +# Infinite loop for demo. purposes +while : +do + for letter in $chars + do + echo -n ${letter} + done +done + +exit 0 diff --git a/examples/scripts/xterm_title b/examples/scripts/xterm_title new file mode 100644 index 0000000..839003f --- /dev/null +++ b/examples/scripts/xterm_title @@ -0,0 +1,44 @@ +#! /bin/bash +# +# xterm_title - print the contents of the xterm title bar +# +# Derived from http://www.clark.net/pub/dickey/xterm/xterm.faq.html#how2_title +# + +# Copyright 1997 Chester Ramey +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# TThis program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +P=${0##*/} +[ -z "$DISPLAY" ] && { + echo "${P}: not running X" >&2 + exit 1 +} + +if [ -z "$TERM" ] || [ "$TERM" != "xterm" ]; then + echo "${P}: not running in an xterm" >&2 + exit 1 +fi + +exec </dev/tty +old=$(stty -g) +stty raw -echo min 0 time ${1-10} +echo -e "\033[21t\c" > /dev/tty +IFS='' read -r a +stty $old +b=${a#???} +echo "${b%??}" + +exit 0 diff --git a/examples/scripts/zprintf b/examples/scripts/zprintf new file mode 100644 index 0000000..86f9e95 --- /dev/null +++ b/examples/scripts/zprintf @@ -0,0 +1,43 @@ +#! /bin/bash +# +# zprintf - function that calls gawk to do printf for those systems that +# don't have a printf executable +# +# The format and arguments can have trailing commas, just like gawk +# +# example: +# zprintf 'Eat %x %x and suck %x!\n' 57005 48879 64206 +# +# Chet Ramey +# chet@po.cwru.edu + +# Copyright 1996 Chester Ramey +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# TThis program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +[ $# -lt 1 ] && { + echo "zprintf: usage: zprintf format [args ...]" >&2 + exit 2 +} + +fmt="${1%,}" +shift + +for a in "$@"; do + args="$args,\"${a%,}\"" +done + +gawk "BEGIN { printf \"$fmt\" $args }" |