Skip to content

Commit

Permalink
initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
djura-san committed Jan 26, 2014
0 parents commit 54382c7
Show file tree
Hide file tree
Showing 138 changed files with 6,076 additions and 0 deletions.
57 changes: 57 additions & 0 deletions 001-inpath.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh
# inpath - verify that a specified program is either valid as-is,
# or can be found in the PATH directory list.

in_path()
{
# given a command and the PATH, try to find the command. Returns
# 0 if found and executable, 1 if not. Note that this temporarily modifies
# the the IFS (input field seperator), but restores it upon completion.

cmd=$1 path=$2 retval=1
oldIFS=$IFS IFS=":"

for directory in $path
do
if [ -x $directory/$cmd ] ; then
retval=0 # if we're here, we found $cmd in $directory
fi
done
IFS=$oldIFS
return $retval
}

checkForCmdInPath()
{
var=$1

# The variable slicing notation in the following conditional
# needs some explanation: ${var#expr} returns everything after
# the match for 'expr' in the variable value (if any), and
# ${var%expr} returns everything that doesn't match (in this
# case just the very first character. You can also do this in
# Bash with ${var:0:1} and you could use cut too: cut -c1

if [ "$var" != "" ] ; then
if [ "${var%${var#?}}" = "/" ] ; then
if [ ! -x $var ] ; then
return 1
fi
elif ! in_path $var $PATH ; then
return 2
fi
fi
}

if [ $# -ne 1 ] ; then
echo "Usage: $0 command" >&2 ; exit 1
fi

checkForCmdInPath "$1"
case $? in
0 ) echo "$1 found in PATH" ;;
1 ) echo "$1 not found or not executable" ;;
2 ) echo "$1 not found in PATH" ;;
esac

exit 0
31 changes: 31 additions & 0 deletions 002-validalnum.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh
# validalAlphaNum - Ensures that input only consists of alphabetical
# and numeric characters.

validAlphaNum()
{
# validate arg: returns 0 if all upper+lower+digits, 1 otherwise

# Remove all unacceptable chars
compressed="$(echo $1 | sed -e 's/[^[:alnum:]]//g')"

if [ "$compressed" != "$input" ] ; then
return 1
else
return 0
fi
}

# Sample usage of this function in a script

echo -n "Enter input: "
read input

if ! validAlphaNum "$input" ; then
echo "Your input must consist of only letters and numbers." >&2
exit 1
else
echo "Input is valid."
fi

exit 0
47 changes: 47 additions & 0 deletions 003-normdate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/sh
# normdate - Normalizes month field in date specification
# to three letters, first letter capitalized. A helper
# function for hack #7, validdate. Exits w/ zero if no error.

monthnoToName()
{
# sets the variable 'month' to the appropriate value
case $1 in
1 ) month="Jan" ;; 2 ) month="Feb" ;;
3 ) month="Mar" ;; 4 ) month="Apr" ;;
5 ) month="May" ;; 6 ) month="Jun" ;;
7 ) month="Jul" ;; 8 ) month="Aug" ;;
9 ) month="Sep" ;; 10) month="Oct" ;;
11) month="Nov" ;; 12) month="Dec" ;;
* ) echo "$0: Unknown numeric month value $1" >&2; exit 1
esac
return 0
}

## Begin main script

if [ $# -eq 1 ] ; then # try to compensate for / or - formats
set -- $(echo $1 | sed 's/[\/\-]/ /g')
fi

if [ $# -ne 3 ] ; then
echo "Usage: $0 month day year" >&2
echo "Typical input formats are August 3 1962 and 8 3 2002" >&2
exit 1
fi

if [ $3 -lt 99 ] ; then
echo "$0: expected four-digit year value." >&2; exit 1
fi

if [ -z $(echo $1|sed 's/[[:digit:]]//g') ]; then
monthnoToName $1
else
# normalize to first three letters, first upper, rest lowercase
month="$(echo $1|cut -c1|tr '[:lower:]' '[:upper:]')"
month="$month$(echo $1|cut -c2-3 | tr '[:upper:]' '[:lower:]')"
fi

echo $month $2 $3

exit 0
63 changes: 63 additions & 0 deletions 004-nicenumber.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/sh

# nicenumber - given a number, show it with comma separated values
# expects DD and TD to be instantiated. instantiates nicenum
# or, if a second arg is specified, the output is echoed to stdout

nicenumber()
{
# Note that we use the '.' as the decimal separator for parsing
# the INPUT value to this script. The output value is as specified
# by the user with the -d flag, if different from a '.'

integer=$(echo $1 | cut -d. -f1) # left of the decimal
decimal=$(echo $1 | cut -d. -f2) # right of the decimal

if [ $decimal != $1 ]; then
# there's a fractional part, let's include it.
result="${DD:="."}$decimal"
fi

thousands=$integer

while [ $thousands -gt 999 ]; do
remainder=$(($thousands % 1000)) # three least significant digits

while [ ${#remainder} -lt 3 ] ; do # force leading zeroes as needed
remainder="0$remainder"
done

thousands=$(($thousands / 1000)) # to left of remainder, if any
result="${TD:=","}${remainder}${result}" # builds right-to-left
done

nicenum="${thousands}${result}"
if [ ! -z $2 ] ; then
echo $nicenum
fi
}

DD="." # decimal point delimiter, between integer & fractional value
TD="," # thousands delimiter, separates every three digits

while getopts "d:t:" opt; do
case $opt in
d ) DD="$OPTARG" ;;
t ) TD="$OPTARG" ;;
esac
done

shift $(($OPTIND - 1))

if [ $# -eq 0 ] ; then
cat << "EOF" >&2
Usage: $(basename $0) [-d c] [-t c] numeric value
-d specifies the decimal point delimiter (default '.')
-t specifies the thousands delimiter (default ',')
EOF
exit 1
fi

nicenumber $1 1 # second arg forces this to 'echo' output

exit 0
50 changes: 50 additions & 0 deletions 005-validint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh
# validint - validate integer input, allow negative ints too

validint()
{
# validate first field. Optionally test against min value $2 and/or
# max value $3: if you'd rather skip these tests, send "" as values.
# returns 1 for error, 0 for success.

number="$1"; min="$2"; max="$3"

if [ -z $number ] ; then
echo "You didn't enter anything. Unacceptable." >&2 ; return 1
fi

if [ "${number%${number#?}}" = "-" ] ; then # first char '-' ?
testvalue="${number#?}" # all but first character
else
testvalue="$number"
fi

nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')"

if [ ! -z $nodigits ] ; then
echo "Invalid number format! Only digits, no commas, spaces, etc." >&2
return 1
fi

if [ ! -z $min ] ; then
if [ "$number" -lt "$min" ] ; then
echo "Your value is too small: smallest acceptable value is $min" >&2
return 1
fi
fi
if [ ! -z $max ] ; then
if [ "$number" -gt "$max" ] ; then
echo "Your value is too big: largest acceptable value is $max" >&2
return 1
fi
fi
return 0
}

# uncomment these lines to test, but beware that it'll break Hack #6
# because Hack #6 wants to source this file to get the validint()
# function. :-)

# if validint "$1" "$2" "$3" ; then
# echo "That input is a valid integer value within your constraints"
# fi
62 changes: 62 additions & 0 deletions 006-validfloat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/sh

# validfloat - test whether a number is a valid floating point value.
# Note that this cannot accept scientific (1.304e5) notation.

# To test whether an entered value is a valid floating point number, we
# need to split the value at the decimal point, then test the first part
# to see if it's a valid integer, then the second part to see if it's a
# valid >=0 integer, so -30.5 is valid, but -30.-8 isn't.

. 005-validint.sh # source the validint function

validfloat()
{
fvalue="$1"

if [ ! -z $(echo $fvalue | sed 's/[^.]//g') ] ; then

decimalPart="$(echo $fvalue | cut -d. -f1)"
fractionalPart="$(echo $fvalue | cut -d. -f2)"

if [ ! -z $decimalPart ] ; then
if ! validint "$decimalPart" "" "" ; then
return 1
fi
fi

if [ "${fractionalPart%${fractionalPart#?}}" = "-" ] ; then
echo "Invalid floating point number: '-' not allowed \
after decimal point" >&2
return 1
fi
if [ "$fractionalPart" != "" ] ; then
if ! validint "$fractionalPart" "0" "" ; then
return 1
fi
fi

if [ "$decimalPart" = "-" -o -z $decimalPart ] ; then
if [ -z $fractionalPart ] ; then
echo "Invalid floating point format." >&2 ; return 1
fi
fi

else
if [ "$fvalue" = "-" ] ; then
echo "Invalid floating point format." >&2 ; return 1
fi

if ! validint "$fvalue" "" "" ; then
return 1
fi
fi

return 0
}

if validfloat $1 ; then
echo "$1 is a valid floating point value"
fi

exit 0
Loading

0 comments on commit 54382c7

Please sign in to comment.