Bash-4.2 distribution sources and documentation
This commit is contained in:
parent
30d188c293
commit
495aee441b
341 changed files with 108751 additions and 36060 deletions
67
examples/scripts/bash-hexdump.sh
Normal file
67
examples/scripts/bash-hexdump.sh
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#From: "dennis" <dennis@netstrata.com>
|
||||
#To: <bash-maintainers@gnu.org>
|
||||
#Subject: New example script: bash-hexdump
|
||||
#Date: Mon, 4 Jan 2010 22:48:19 -0700
|
||||
#Message-ID: <6dbec42d$64fcdbd2$4a32cf2d$@com>
|
||||
|
||||
#I've written a script that functions like "hexdump -C" or "hd". If you'd
|
||||
#like to include it in a future distribution of example Bash scripts, I have
|
||||
#included it here:
|
||||
|
||||
#!/bin/bash
|
||||
# bash-hexdump# pure Bash, no externals
|
||||
# by Dennis Williamson - 2010-01-04
|
||||
# in response to
|
||||
http://stackoverflow.com/questions/2003803/show-hexadecimal-numbers-of-a-file
|
||||
# usage: bash-hexdump file
|
||||
saveIFS="$IFS"
|
||||
IFS="" # disables interpretation of \t, \n and space
|
||||
saveLANG="$LANG"
|
||||
LANG=C # allows characters > 0x7F
|
||||
bytecount=0
|
||||
valcount=0
|
||||
printf "%08x " $bytecount
|
||||
while read -d '' -r -n 1 char # -d '' allows newlines, -r allows \
|
||||
do
|
||||
((bytecount++))
|
||||
# for information about the apostrophe in this printf command, see
|
||||
# http://www.opengroup.org/onlinepubs/009695399/utilities/printf.html
|
||||
printf -v val "%02x" "'$char"
|
||||
echo -n "$val "
|
||||
((valcount++))
|
||||
if [[ "$val" < 20 || "$val" > 7e ]]
|
||||
then
|
||||
string+="." # show unprintable characters as a dot
|
||||
else
|
||||
string+=$char
|
||||
fi
|
||||
if (( bytecount % 8 == 0 )) # add a space down the middle
|
||||
then
|
||||
echo -n " "
|
||||
fi
|
||||
if (( bytecount % 16 == 0 )) # print 16 values per line
|
||||
then
|
||||
echo "|$string|"
|
||||
string=''
|
||||
valcount=0
|
||||
printf "%08x " $bytecount
|
||||
fi
|
||||
done < "$1"
|
||||
|
||||
if [[ "$string" != "" ]] # if the last line wasn't full, pad it out
|
||||
then
|
||||
length=${#string}
|
||||
if (( length > 7 ))
|
||||
then
|
||||
((length--))
|
||||
fi
|
||||
(( length += (16 - valcount) * 3 + 4))
|
||||
printf "%${length}s\n" "|$string|"
|
||||
printf "%08x " $bytecount
|
||||
fi
|
||||
echo
|
||||
|
||||
LANG="$saveLANG";
|
||||
IFS="$saveIFS"
|
||||
|
||||
exit 0
|
||||
|
|
@ -1,56 +1,57 @@
|
|||
#!/bin/bash
|
||||
#
|
||||
# The Bash shell script executes a command with a time-out.
|
||||
# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal
|
||||
# is blocked, then the subsequent SIGKILL (9) terminates it.
|
||||
#
|
||||
# The Bash script executes a command with a time-out.
|
||||
# Based on the Bash documentation example.
|
||||
|
||||
# Hello Chet,
|
||||
# please find attached a "little easier" :-) to comprehend
|
||||
# time-out example. If you find it suitable, feel free to include
|
||||
# anywhere: the very same logic as in the original examples/scripts, a
|
||||
# little more transparent implementation to my taste.
|
||||
#
|
||||
# Dmitry V Golovashkin <Dmitry.Golovashkin@sas.com>
|
||||
# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal
|
||||
# is blocked, then the subsequent SIGKILL (9) terminates it.
|
||||
# Dmitry V Golovashkin (E-mail: dvg@ieee.org)
|
||||
#
|
||||
script_name="${0##*/}"
|
||||
|
||||
scriptName="${0##*/}"
|
||||
# Default values.
|
||||
readonly param_timeout=5
|
||||
readonly param_interval=1
|
||||
readonly param_delay=1
|
||||
|
||||
declare -i DEFAULT_TIMEOUT=9
|
||||
declare -i DEFAULT_INTERVAL=1
|
||||
declare -i DEFAULT_DELAY=1
|
||||
declare -i timeout=param_timeout
|
||||
declare -i interval=param_interval
|
||||
declare -i delay=param_delay
|
||||
|
||||
# Timeout.
|
||||
declare -i timeout=DEFAULT_TIMEOUT
|
||||
# Interval between checks if the process is still alive.
|
||||
declare -i interval=DEFAULT_INTERVAL
|
||||
# Delay between posting the SIGTERM signal and destroying the process by SIGKILL.
|
||||
declare -i delay=DEFAULT_DELAY
|
||||
blue="$(tput setaf 4)"
|
||||
bold_red="$(tput bold; tput setaf 1)"
|
||||
off="$(tput sgr0)"
|
||||
|
||||
function printUsage() {
|
||||
cat <<EOF
|
||||
function print_usage() {
|
||||
cat <<EOF
|
||||
|
||||
Synopsis
|
||||
$scriptName [-t timeout] [-i interval] [-d delay] command
|
||||
Execute a command with a time-out.
|
||||
Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM
|
||||
signal is blocked, then the subsequent SIGKILL (9) terminates it.
|
||||
Synopsis: $script_name [-t timeout] [-i interval] [-d delay] command
|
||||
|
||||
-t timeout
|
||||
Number of seconds to wait for command completion.
|
||||
Default value: $DEFAULT_TIMEOUT seconds.
|
||||
Executes the command with a time-out. Upon time-out expiration SIGTERM (15) is
|
||||
sent to the process. If SIGTERM signal is blocked, then the subsequent SIGKILL
|
||||
(9) terminates it.
|
||||
|
||||
-i interval
|
||||
Interval between checks if the process is still alive.
|
||||
Positive integer, default value: $DEFAULT_INTERVAL seconds.
|
||||
$blue-t timeout$off
|
||||
Number of seconds to wait for command completion.
|
||||
Default value: $param_timeout seconds. In some practical situations
|
||||
this value ${bold_red}must$off be increased (for instance -t 180) to allow
|
||||
the command to complete.
|
||||
|
||||
-d delay
|
||||
Delay between posting the SIGTERM signal and destroying the
|
||||
process by SIGKILL. Default value: $DEFAULT_DELAY seconds.
|
||||
$blue-i interval$off
|
||||
Interval between checks if the process is still alive.
|
||||
Positive integer, default value: $param_interval seconds.
|
||||
Default value is OK for most situations.
|
||||
|
||||
$blue-d delay$off
|
||||
Delay between posting the SIGTERM signal and destroying the process by
|
||||
SIGKILL. Default value: $param_delay seconds.
|
||||
Default value is OK for most situations.
|
||||
|
||||
As of today, Bash does not support floating point arithmetic (sleep does),
|
||||
therefore all delay/time values must be integers.
|
||||
therefore all time values must be integers.
|
||||
Dmitry Golovashkin (E-mail: dvg@ieee.org)
|
||||
EOF
|
||||
exit 1 # No useful work was done.
|
||||
}
|
||||
|
||||
# Options.
|
||||
|
|
@ -59,7 +60,7 @@ while getopts ":t:i:d:" option; do
|
|||
t) timeout=$OPTARG ;;
|
||||
i) interval=$OPTARG ;;
|
||||
d) delay=$OPTARG ;;
|
||||
*) printUsage; exit 1 ;;
|
||||
*) print_usage ;;
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND - 1))
|
||||
|
|
@ -67,11 +68,10 @@ shift $((OPTIND - 1))
|
|||
# $# should be at least 1 (the command to execute), however it may be strictly
|
||||
# greater than 1 if the command itself has options.
|
||||
if (($# == 0 || interval <= 0)); then
|
||||
printUsage
|
||||
exit 1
|
||||
print_usage
|
||||
fi
|
||||
|
||||
# kill -0 pid Exit code indicates if a signal may be sent to $pid process.
|
||||
# kill -0 pid Exit code indicates if a signal may be sent to "pid" process.
|
||||
(
|
||||
((t = timeout))
|
||||
|
||||
|
|
@ -89,3 +89,4 @@ fi
|
|||
) 2> /dev/null &
|
||||
|
||||
exec "$@"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
if [ -z "$PS1" ]; then
|
||||
return
|
||||
fi
|
||||
case $- in
|
||||
*i*) ;;
|
||||
*) return ;;
|
||||
esac
|
||||
|
||||
# bogus
|
||||
if [ -f /unix ] ; then
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue