This commit is contained in:
gferg 2005-05-08 20:10:18 +00:00
parent 50b038d16c
commit 26e5ae5d42
4 changed files with 349 additions and 0 deletions

View File

@ -0,0 +1,179 @@
# Hash:
# Hash function library
# Author: Mariusz Gniazdowski <mgniazd-at-gmail.com>
# Date: 2005-04-07
# Functions making emulating hashes in Bash a little less painful.
# Limitations:
# * Only global variables are supported.
# * Each hash instance generates one global variable per value.
# * Variable names collisions are possible
#+ if you define variable like __hash__hashname_key
# * Keys must use chars that can be part of a Bash variable name
#+ (no dashes, periods, etc.).
# * The hash is created as a variable:
# ... hashname_keyname
# So if somone will create hashes like:
# myhash_ + mykey = myhash__mykey
# myhash + _mykey = myhash__mykey
# Then there will be a collision.
# (This should not pose a major problem.)
Hash_config_varname_prefix=__hash__
# Emulates: hash[key]=value
#
# Params:
# 1 - hash
# 2 - key
# 3 - value
function hash_set {
eval "${Hash_config_varname_prefix}${1}_${2}=\"${3}\""
}
# Emulates: value=hash[key]
#
# Params:
# 1 - hash
# 2 - key
# 3 - value (name of global variable to set)
function hash_get_into {
eval "$3=\"\$${Hash_config_varname_prefix}${1}_${2}\""
}
# Emulates: echo hash[key]
#
# Params:
# 1 - hash
# 2 - key
# 3 - echo params (like -n, for example)
function hash_echo {
eval "echo $3 \"\$${Hash_config_varname_prefix}${1}_${2}\""
}
# Emulates: hash1[key1]=hash2[key2]
#
# Params:
# 1 - hash1
# 2 - key1
# 3 - hash2
# 4 - key2
function hash_copy {
eval "${Hash_config_varname_prefix}${1}_${2}=\"\$${Hash_config_varname_prefix}${3}_${4}\""
}
# Emulates: hash[keyN-1]=hash[key2]=...hash[key1]
#
# Copies first key to rest of keys.
#
# Params:
# 1 - hash1
# 2 - key1
# 3 - key2
# . . .
# N - keyN
function hash_dup {
local hashName="$1" keyName="$2"
shift 2
until [ ${#} -le 0 ]; do
eval "${Hash_config_varname_prefix}${hashName}_${1}=\"\$${Hash_config_varname_prefix}${hashName}_${keyName}\""
shift;
done;
}
# Emulates: unset hash[key]
#
# Params:
# 1 - hash
# 2 - key
function hash_unset {
eval "unset ${Hash_config_varname_prefix}${1}_${2}"
}
# Emulates something similar to: ref=&hash[key]
#
# The reference is name of the variable in which value is held.
#
# Params:
# 1 - hash
# 2 - key
# 3 - ref - Name of global variable to set.
function hash_get_ref_into {
eval "$3=\"${Hash_config_varname_prefix}${1}_${2}\""
}
# Emulates something similar to: echo &hash[key]
#
# That reference is name of variable in which value is held.
#
# Params:
# 1 - hash
# 2 - key
# 3 - echo params (like -n for example)
function hash_echo_ref {
eval "echo $3 \"${Hash_config_varname_prefix}${1}_${2}\""
}
# Emulates something similar to: $$hash[key](param1, param2, ...)
#
# Params:
# 1 - hash
# 2 - key
# 3,4, ... - Function parameters
function hash_call {
local hash key
hash=$1
key=$2
shift 2
eval "eval \"\$${Hash_config_varname_prefix}${hash}_${key} \\\"\\\$@\\\"\""
}
# Emulates something similar to: isset(hash[key]) or hash[key]==NULL
#
# Params:
# 1 - hash
# 2 - key
# Returns:
# 0 - there is such key
# 1 - there is no such key
function hash_is_set {
eval "if [[ \"\${${Hash_config_varname_prefix}${1}_${2}-a}\" = \"a\" &&
\"\${${Hash_config_varname_prefix}${1}_${2}-b}\" = \"b\" ]]; then return 1; else return 0; fi"
}
# Emulates something similar to:
# foreach($hash as $key => $value) { fun($key,$value); }
#
# It is possible to write different variations of this function.
# Here we use a function call to make it as "generic" as possible.
#
# Params:
# 1 - hash
# 2 - function name
function hash_foreach {
local keyname oldIFS="$IFS"
IFS=' '
for i in $(eval "echo \${!${Hash_config_varname_prefix}${1}_*}"); do
keyname=$(eval "echo \${i##${Hash_config_varname_prefix}${1}_}")
eval "$2 $keyname \"\$$i\""
done
IFS="$oldIFS"
}
# NOTE: In lines 103 and 116, ampersand changed.
# But, it doesn't matter, because these are comment lines anyhow.

View File

@ -0,0 +1,44 @@
#!/bin/bash
# getopt-simple.sh
# Author: Chris Morgan
# Used in the ABS Guide with permission.
getopt_simple()
{
echo "getopt_simple()"
echo "Parameters are '$*'"
until [ -z "$1" ]
do
echo "Processing parameter of: '$1'"
if [ ${1:0:1} = '/' ]
then
tmp=${1:1} # Strip off leading '/' . . .
parameter=${tmp%%=*} # Extract name.
value=${tmp##*=} # Extract value.
echo "Parameter: '$parameter', value: '$value'"
eval $parameter=$value
fi
shift
done
}
# Pass all options to getopt_simple().
getopt_simple $*
echo "test is '$test'"
echo "test2 is '$test2'"
exit 0
---
sh getopt_example.sh /test=value1 /test2=value2
Parameters are '/test=value1 /test2=value2'
Processing parameter of: '/test=value1'
Parameter: 'test', value: 'value1'
Processing parameter of: '/test2=value2'
Parameter: 'test2', value: 'value2'
test is 'value1'
test2 is 'value2'

View File

@ -0,0 +1,60 @@
#!/bin/bash
# hash-example.sh: Colorizing text.
# Author: Mariusz Gniazdowski <mgniazd-at-gmail.com>
. Hash.lib # Load the library of functions.
hash_set colors red "\033[0;31m"
hash_set colors blue "\033[0;34m"
hash_set colors light_blue "\033[1;34m"
hash_set colors light_red "\033[1;31m"
hash_set colors cyan "\033[0;36m"
hash_set colors light_green "\033[1;32m"
hash_set colors light_gray "\033[0;37m"
hash_set colors green "\033[0;32m"
hash_set colors yellow "\033[1;33m"
hash_set colors light_purple "\033[1;35m"
hash_set colors purple "\033[0;35m"
hash_set colors reset_color "\033[0;00m"
# $1 - keyname
# $2 - value
try_colors() {
echo -en "$2"
echo "This line is $1."
}
hash_foreach colors try_colors
hash_echo colors reset_color -en
echo -e '\nLet us overwrite some colors with yellow.\n'
# It's hard to read yellow text on some terminals.
hash_dup colors yellow red light_green blue green light_gray cyan
hash_foreach colors try_colors
hash_echo colors reset_color -en
echo -e '\nLet us delete them and try colors once more . . .\n'
for i in red light_green blue green light_gray cyan; do
hash_unset colors $i
done
hash_foreach colors try_colors
hash_echo colors reset_color -en
hash_set other txt "Other examples . . ."
hash_echo other txt
hash_get_into other txt text
echo $text
hash_set other my_fun try_colors
hash_call other my_fun purple "`hash_echo colors purple`"
hash_echo colors reset_color -en
echo; echo "Back to normal?"; echo
exit $?
# On some terminals, the "light" colors print in bold,
# and end up looking darker than the normal ones.
# Why is this?

View File

@ -0,0 +1,66 @@
#! /bin/sh
## Duplicate DaveG's ident-scan thingie using netcat. Oooh, he'll be p*ssed.
## Args: target port [port port port ...]
## Hose stdout _and_ stderr together.
##
## Advantages: runs slower than ident-scan, giving remote inetd less cause
##+ for alarm, and only hits the few known daemon ports you specify.
## Disadvantages: requires numeric-only port args, the output sleazitude,
##+ and won't work for r-services when coming from high source ports.
# Script author: Hobbit <hobbit@avian.org>
# Used in ABS Guide with permission.
# ---------------------------------------------------
E_BADARGS=65 # Need at least two args.
TWO_WINKS=2 # How long to sleep.
THREE_WINKS=3
IDPORT=113 # Authentication "tap ident" port.
RAND1=999
RAND2=31337
TIMEOUT0=9
TIMEOUT1=8
TIMEOUT2=4
# ---------------------------------------------------
case "${2}" in
"" ) echo "Need HOST and at least one PORT." ; exit $E_BADARGS ;;
esac
# Ping 'em once and see if they *are* running identd.
nc -z -w $TIMEOUT0 "$1" $IDPORT || { echo "Oops, $1 isn't running identd." ; exit 0 ; }
# -z scans for listening daemons.
# -w $TIMEOUT = How long to try to connect.
# Generate a randomish base port.
RP=`expr $$ % $RAND1 + $RAND2`
TRG="$1"
shift
while test "$1" ; do
nc -v -w $TIMEOUT1 -p ${RP} "$TRG" ${1} < /dev/null > /dev/null &
PROC=$!
sleep $THREE_WINKS
echo "${1},${RP}" | nc -w $TIMEOUT2 -r "$TRG" $IDPORT 2>&1
sleep $TWO_WINKS
# Does this look like a lamer script or what . . . ?
# ABS Guide author comments: "It ain't really all that bad,
#+ rather clever, actually."
kill -HUP $PROC
RP=`expr ${RP} + 1`
shift
done
exit $?
# Notes:
# -----
# Try commenting out line 30 and running this script
#+ with "localhost.localdomain 25" as arguments.
# For more of Hobbit's 'nc' example scripts,
#+ look in the documentation:
#+ the /usr/share/doc/nc-X.XX/scripts directory.