Skip to content

Commit 54382c7

Browse files
author
djura-san
committed
initial upload
0 parents  commit 54382c7

File tree

138 files changed

+6076
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+6076
-0
lines changed

001-inpath.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
# inpath - verify that a specified program is either valid as-is,
3+
# or can be found in the PATH directory list.
4+
5+
in_path()
6+
{
7+
# given a command and the PATH, try to find the command. Returns
8+
# 0 if found and executable, 1 if not. Note that this temporarily modifies
9+
# the the IFS (input field seperator), but restores it upon completion.
10+
11+
cmd=$1 path=$2 retval=1
12+
oldIFS=$IFS IFS=":"
13+
14+
for directory in $path
15+
do
16+
if [ -x $directory/$cmd ] ; then
17+
retval=0 # if we're here, we found $cmd in $directory
18+
fi
19+
done
20+
IFS=$oldIFS
21+
return $retval
22+
}
23+
24+
checkForCmdInPath()
25+
{
26+
var=$1
27+
28+
# The variable slicing notation in the following conditional
29+
# needs some explanation: ${var#expr} returns everything after
30+
# the match for 'expr' in the variable value (if any), and
31+
# ${var%expr} returns everything that doesn't match (in this
32+
# case just the very first character. You can also do this in
33+
# Bash with ${var:0:1} and you could use cut too: cut -c1
34+
35+
if [ "$var" != "" ] ; then
36+
if [ "${var%${var#?}}" = "/" ] ; then
37+
if [ ! -x $var ] ; then
38+
return 1
39+
fi
40+
elif ! in_path $var $PATH ; then
41+
return 2
42+
fi
43+
fi
44+
}
45+
46+
if [ $# -ne 1 ] ; then
47+
echo "Usage: $0 command" >&2 ; exit 1
48+
fi
49+
50+
checkForCmdInPath "$1"
51+
case $? in
52+
0 ) echo "$1 found in PATH" ;;
53+
1 ) echo "$1 not found or not executable" ;;
54+
2 ) echo "$1 not found in PATH" ;;
55+
esac
56+
57+
exit 0

002-validalnum.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
# validalAlphaNum - Ensures that input only consists of alphabetical
3+
# and numeric characters.
4+
5+
validAlphaNum()
6+
{
7+
# validate arg: returns 0 if all upper+lower+digits, 1 otherwise
8+
9+
# Remove all unacceptable chars
10+
compressed="$(echo $1 | sed -e 's/[^[:alnum:]]//g')"
11+
12+
if [ "$compressed" != "$input" ] ; then
13+
return 1
14+
else
15+
return 0
16+
fi
17+
}
18+
19+
# Sample usage of this function in a script
20+
21+
echo -n "Enter input: "
22+
read input
23+
24+
if ! validAlphaNum "$input" ; then
25+
echo "Your input must consist of only letters and numbers." >&2
26+
exit 1
27+
else
28+
echo "Input is valid."
29+
fi
30+
31+
exit 0

003-normdate.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
# normdate - Normalizes month field in date specification
3+
# to three letters, first letter capitalized. A helper
4+
# function for hack #7, validdate. Exits w/ zero if no error.
5+
6+
monthnoToName()
7+
{
8+
# sets the variable 'month' to the appropriate value
9+
case $1 in
10+
1 ) month="Jan" ;; 2 ) month="Feb" ;;
11+
3 ) month="Mar" ;; 4 ) month="Apr" ;;
12+
5 ) month="May" ;; 6 ) month="Jun" ;;
13+
7 ) month="Jul" ;; 8 ) month="Aug" ;;
14+
9 ) month="Sep" ;; 10) month="Oct" ;;
15+
11) month="Nov" ;; 12) month="Dec" ;;
16+
* ) echo "$0: Unknown numeric month value $1" >&2; exit 1
17+
esac
18+
return 0
19+
}
20+
21+
## Begin main script
22+
23+
if [ $# -eq 1 ] ; then # try to compensate for / or - formats
24+
set -- $(echo $1 | sed 's/[\/\-]/ /g')
25+
fi
26+
27+
if [ $# -ne 3 ] ; then
28+
echo "Usage: $0 month day year" >&2
29+
echo "Typical input formats are August 3 1962 and 8 3 2002" >&2
30+
exit 1
31+
fi
32+
33+
if [ $3 -lt 99 ] ; then
34+
echo "$0: expected four-digit year value." >&2; exit 1
35+
fi
36+
37+
if [ -z $(echo $1|sed 's/[[:digit:]]//g') ]; then
38+
monthnoToName $1
39+
else
40+
# normalize to first three letters, first upper, rest lowercase
41+
month="$(echo $1|cut -c1|tr '[:lower:]' '[:upper:]')"
42+
month="$month$(echo $1|cut -c2-3 | tr '[:upper:]' '[:lower:]')"
43+
fi
44+
45+
echo $month $2 $3
46+
47+
exit 0

004-nicenumber.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/sh
2+
3+
# nicenumber - given a number, show it with comma separated values
4+
# expects DD and TD to be instantiated. instantiates nicenum
5+
# or, if a second arg is specified, the output is echoed to stdout
6+
7+
nicenumber()
8+
{
9+
# Note that we use the '.' as the decimal separator for parsing
10+
# the INPUT value to this script. The output value is as specified
11+
# by the user with the -d flag, if different from a '.'
12+
13+
integer=$(echo $1 | cut -d. -f1) # left of the decimal
14+
decimal=$(echo $1 | cut -d. -f2) # right of the decimal
15+
16+
if [ $decimal != $1 ]; then
17+
# there's a fractional part, let's include it.
18+
result="${DD:="."}$decimal"
19+
fi
20+
21+
thousands=$integer
22+
23+
while [ $thousands -gt 999 ]; do
24+
remainder=$(($thousands % 1000)) # three least significant digits
25+
26+
while [ ${#remainder} -lt 3 ] ; do # force leading zeroes as needed
27+
remainder="0$remainder"
28+
done
29+
30+
thousands=$(($thousands / 1000)) # to left of remainder, if any
31+
result="${TD:=","}${remainder}${result}" # builds right-to-left
32+
done
33+
34+
nicenum="${thousands}${result}"
35+
if [ ! -z $2 ] ; then
36+
echo $nicenum
37+
fi
38+
}
39+
40+
DD="." # decimal point delimiter, between integer & fractional value
41+
TD="," # thousands delimiter, separates every three digits
42+
43+
while getopts "d:t:" opt; do
44+
case $opt in
45+
d ) DD="$OPTARG" ;;
46+
t ) TD="$OPTARG" ;;
47+
esac
48+
done
49+
50+
shift $(($OPTIND - 1))
51+
52+
if [ $# -eq 0 ] ; then
53+
cat << "EOF" >&2
54+
Usage: $(basename $0) [-d c] [-t c] numeric value
55+
-d specifies the decimal point delimiter (default '.')
56+
-t specifies the thousands delimiter (default ',')
57+
EOF
58+
exit 1
59+
fi
60+
61+
nicenumber $1 1 # second arg forces this to 'echo' output
62+
63+
exit 0

005-validint.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/sh
2+
# validint - validate integer input, allow negative ints too
3+
4+
validint()
5+
{
6+
# validate first field. Optionally test against min value $2 and/or
7+
# max value $3: if you'd rather skip these tests, send "" as values.
8+
# returns 1 for error, 0 for success.
9+
10+
number="$1"; min="$2"; max="$3"
11+
12+
if [ -z $number ] ; then
13+
echo "You didn't enter anything. Unacceptable." >&2 ; return 1
14+
fi
15+
16+
if [ "${number%${number#?}}" = "-" ] ; then # first char '-' ?
17+
testvalue="${number#?}" # all but first character
18+
else
19+
testvalue="$number"
20+
fi
21+
22+
nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')"
23+
24+
if [ ! -z $nodigits ] ; then
25+
echo "Invalid number format! Only digits, no commas, spaces, etc." >&2
26+
return 1
27+
fi
28+
29+
if [ ! -z $min ] ; then
30+
if [ "$number" -lt "$min" ] ; then
31+
echo "Your value is too small: smallest acceptable value is $min" >&2
32+
return 1
33+
fi
34+
fi
35+
if [ ! -z $max ] ; then
36+
if [ "$number" -gt "$max" ] ; then
37+
echo "Your value is too big: largest acceptable value is $max" >&2
38+
return 1
39+
fi
40+
fi
41+
return 0
42+
}
43+
44+
# uncomment these lines to test, but beware that it'll break Hack #6
45+
# because Hack #6 wants to source this file to get the validint()
46+
# function. :-)
47+
48+
# if validint "$1" "$2" "$3" ; then
49+
# echo "That input is a valid integer value within your constraints"
50+
# fi

006-validfloat.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/sh
2+
3+
# validfloat - test whether a number is a valid floating point value.
4+
# Note that this cannot accept scientific (1.304e5) notation.
5+
6+
# To test whether an entered value is a valid floating point number, we
7+
# need to split the value at the decimal point, then test the first part
8+
# to see if it's a valid integer, then the second part to see if it's a
9+
# valid >=0 integer, so -30.5 is valid, but -30.-8 isn't.
10+
11+
. 005-validint.sh # source the validint function
12+
13+
validfloat()
14+
{
15+
fvalue="$1"
16+
17+
if [ ! -z $(echo $fvalue | sed 's/[^.]//g') ] ; then
18+
19+
decimalPart="$(echo $fvalue | cut -d. -f1)"
20+
fractionalPart="$(echo $fvalue | cut -d. -f2)"
21+
22+
if [ ! -z $decimalPart ] ; then
23+
if ! validint "$decimalPart" "" "" ; then
24+
return 1
25+
fi
26+
fi
27+
28+
if [ "${fractionalPart%${fractionalPart#?}}" = "-" ] ; then
29+
echo "Invalid floating point number: '-' not allowed \
30+
after decimal point" >&2
31+
return 1
32+
fi
33+
if [ "$fractionalPart" != "" ] ; then
34+
if ! validint "$fractionalPart" "0" "" ; then
35+
return 1
36+
fi
37+
fi
38+
39+
if [ "$decimalPart" = "-" -o -z $decimalPart ] ; then
40+
if [ -z $fractionalPart ] ; then
41+
echo "Invalid floating point format." >&2 ; return 1
42+
fi
43+
fi
44+
45+
else
46+
if [ "$fvalue" = "-" ] ; then
47+
echo "Invalid floating point format." >&2 ; return 1
48+
fi
49+
50+
if ! validint "$fvalue" "" "" ; then
51+
return 1
52+
fi
53+
fi
54+
55+
return 0
56+
}
57+
58+
if validfloat $1 ; then
59+
echo "$1 is a valid floating point value"
60+
fi
61+
62+
exit 0

0 commit comments

Comments
 (0)