initial commit
This commit is contained in:
117
chapter14/chg_base.ksh
Executable file
117
chapter14/chg_base.ksh
Executable file
@@ -0,0 +1,117 @@
|
||||
#!/bin/ksh
|
||||
#
|
||||
# SCRIPT: chg_base.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 10/4/2007
|
||||
# REV: 1.1.A
|
||||
#
|
||||
# PURPOSE: This script converts numbers between base
|
||||
# 2 through base 36. The user is prompted for input.
|
||||
#
|
||||
# NOTE: Numbers are in the following format:
|
||||
#
|
||||
# base#number
|
||||
#
|
||||
# EXAMPLE: 16#264bf
|
||||
#
|
||||
##########################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
##########################################
|
||||
|
||||
# Setup the correct awk usage. Solaris needs to
|
||||
# use nawk instead of awk.
|
||||
|
||||
case $(uname) in
|
||||
SunOS) AWK="nawk"
|
||||
;;
|
||||
*) AWK="awk"
|
||||
;;
|
||||
esac
|
||||
|
||||
# Set up the correct echo command usage. Many Linux
|
||||
# distributions will execute in Bash even if the
|
||||
# script specifies Korn shell. Bash shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $SHELL in
|
||||
*/bin/bash) alias echo="echo -e"
|
||||
;;
|
||||
esac
|
||||
|
||||
##########################################
|
||||
# BEGINNING OF MAIN
|
||||
##########################################
|
||||
|
||||
# Prompt the user for an input number with base
|
||||
|
||||
echo "\nInput the number to convert in the following format:
|
||||
|
||||
Format: base#number
|
||||
|
||||
Example: 16#264BF \n"
|
||||
|
||||
read ibase_num
|
||||
|
||||
# Extract the base from the ibase_num variable
|
||||
|
||||
ibase=$(echo $ibase_num | $AWK -F '#' '{print $1}')
|
||||
|
||||
# Test to ensure the input base is an integer
|
||||
|
||||
case $ibase in
|
||||
[0-9]*) : # do nothing
|
||||
;;
|
||||
*) echo "\nERROR: $ibase is not a valid number for input base\n"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Test to ensure the input base is between 2 and 36
|
||||
|
||||
if (( ibase < 2 || ibase > 36 ))
|
||||
then
|
||||
echo "\nERROR: Input base must be between 2 and 36\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ask the user for the output base
|
||||
|
||||
echo "\nWhat base do you want to convert $ibase_num to?
|
||||
NOTE: base 2 through 36 are valid\n"
|
||||
echo "Output base: \c"
|
||||
read obase
|
||||
|
||||
# Test to ensure the output base is an integer
|
||||
|
||||
case $obase in
|
||||
[0-9]*) : # do nothing
|
||||
;;
|
||||
*) echo "\nERROR: $obase is not a valid number\n"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Test to ensure the output base is between 2 and 36
|
||||
|
||||
if (( obase < 2 || obase > 36 ))
|
||||
then
|
||||
echo "\nERROR: Output base must be between 2 and 36\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Save the input number before changing the base
|
||||
|
||||
in_base_num=$ibase_num
|
||||
|
||||
# Convert the input number to the desire output base
|
||||
|
||||
typeset -i$obase ibase_num
|
||||
|
||||
# Assign the output base number to an appropriate variable name
|
||||
|
||||
obase_num=$ibase_num
|
||||
|
||||
# Display the result
|
||||
|
||||
echo "\nInput number $in_base_num is equivalent to $obase_num\n"
|
||||
|
||||
111
chapter14/chg_base_bc.bash
Executable file
111
chapter14/chg_base_bc.bash
Executable file
@@ -0,0 +1,111 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# SCRIPT: chg_base_bc.bash
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 10/4/2007
|
||||
# REV: 1.1.A
|
||||
#
|
||||
# PURPOSE: This script converts numbers between base
|
||||
# 2 through base 16 using the bc utility. The user
|
||||
# is prompted for input.
|
||||
#
|
||||
# NOTE: Numbers are in the following format:
|
||||
#
|
||||
# base#number
|
||||
#
|
||||
# EXAMPLE: 16#264bf
|
||||
#
|
||||
##########################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
##########################################
|
||||
|
||||
# Setup the correct awk usage. Solaris needs to
|
||||
# use nawk instead of awk.
|
||||
|
||||
case $(uname) in
|
||||
SunOS) AWK="nawk"
|
||||
;;
|
||||
*) AWK="awk"
|
||||
;;
|
||||
esac
|
||||
|
||||
##########################################
|
||||
# BEGINNING OF MAIN
|
||||
##########################################
|
||||
|
||||
# Prompt the user for an input number with base
|
||||
|
||||
echo -e "\nInput the number to convert in the following format:
|
||||
|
||||
Format: base#number
|
||||
|
||||
Example: 16#264BF \n"
|
||||
|
||||
read IBASE_NUM
|
||||
|
||||
# Extract the base from the ibase_num variable
|
||||
|
||||
IBASE=$(echo $IBASE_NUM | $AWK -F '#' '{print $1}')
|
||||
INUMBER=$(echo $IBASE_NUM | $AWK -F '#' '{print $2}')
|
||||
|
||||
# Test to ensure the input base is between 2 and 16
|
||||
|
||||
if (( IBASE < 2 || IBASE > 16 ))
|
||||
then
|
||||
echo -e "\nERROR: Input base must be between 2 and 16\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# The bc utility requires all number bases greater
|
||||
# than 10 use uppercase characters for all
|
||||
# non-numeric character numbers, i.e. hex numbers.
|
||||
# We use the tr command to upcase all lowercase
|
||||
# characters.
|
||||
|
||||
if (( IBASE > 10 ))
|
||||
then
|
||||
INUMBER=$(echo $INUMBER | tr '[a-z]' '[A-Z]')
|
||||
fi
|
||||
|
||||
# Ask the user for the output base
|
||||
|
||||
echo -e "\nWhat base do you want to convert $IBASE_NUM to?
|
||||
NOTE: base 2 through 16 are valid\n"
|
||||
echo -e "Output base: \c"
|
||||
read OBASE
|
||||
|
||||
# Test to ensure the output base is an integer
|
||||
|
||||
case $OBASE in
|
||||
[0-9]*) : # do nothing
|
||||
;;
|
||||
*) echo -e "\nERROR: $obase is not a valid number\n"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Test to ensure the output base is between 2 and 16
|
||||
|
||||
if (( OBASE < 2 || OBASE > 16 ))
|
||||
then
|
||||
echo -e "\nERROR: Output base must be between 2 and 16\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Save the input number before changing the base
|
||||
|
||||
IN_BASE_NUM=$IBASE_NUM
|
||||
|
||||
# Convert the input number to decimal
|
||||
|
||||
DEC_EQUIV=$(echo "ibase=$IBASE; $INUMBER" | bc)
|
||||
|
||||
# Convert the number to the desired output base
|
||||
|
||||
RESULT=$(echo "obase=$OBASE; $DEC_EQUIV" | bc)
|
||||
|
||||
# Display the result
|
||||
|
||||
echo -e "\nInput number $IN_BASE_NUM is equivalent to \
|
||||
${OBASE}#${RESULT}\n"
|
||||
|
||||
177
chapter14/equate_any_base.ksh
Executable file
177
chapter14/equate_any_base.ksh
Executable file
@@ -0,0 +1,177 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: equate_any_base.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 07/07/2007
|
||||
# REV: 1.2.P
|
||||
#
|
||||
# PURPOSE: This script is used to convert a number to any
|
||||
# supported number base, which is at least base 36.
|
||||
# This script requires that two command line
|
||||
# auguments and the "number" to be converted
|
||||
# are present on the command line. An example
|
||||
# number base conversion is shown here:
|
||||
#
|
||||
# equate_any_base.ksh -f16 -t2 e245c
|
||||
# 2#11100010010001011100
|
||||
#
|
||||
# This example converts the base 16 number, e245c, to
|
||||
# the base 2 equivalent, 2#11100010010001011100.
|
||||
# The 2#, which precedes the binary number, shows
|
||||
# the base of the number represented.
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 - Normal script execution
|
||||
# 1 - Usage error
|
||||
#
|
||||
# set -x # Uncomment to debug this shell script
|
||||
# set -n # Uncomment to check syntax without any execution
|
||||
#
|
||||
######################################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
######################################################
|
||||
|
||||
SCRIPT_NAME=$(basename $0)
|
||||
COUNT=0
|
||||
MAX_COUNT=$#
|
||||
|
||||
# Setup the correct echo command usage. Many Linux
|
||||
# distributions will execute in BASH even if the
|
||||
# script specifies Korn shell. BASH shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $SHELL in
|
||||
*/bin/bash) alias echo="echo -e"
|
||||
;;
|
||||
esac
|
||||
|
||||
######################################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
######################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nUSAGE: $SCRIPT_NAME -f{starting base} -t{ending base} NUMBER"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME -f16 -t10 FC23"
|
||||
echo "\nWill return the decimal base 10 number 64547"
|
||||
echo "\t ...EXITING...\n"
|
||||
}
|
||||
|
||||
######################################################
|
||||
# CHECK COMMAND LINE AUGUMENTS HERE
|
||||
######################################################
|
||||
|
||||
# The maximum number of command line arguments is five
|
||||
# and the minimum number is three.
|
||||
|
||||
if (($# > 5))
|
||||
then
|
||||
echo "\nERROR: Too many command line arguments\n"
|
||||
usage
|
||||
exit 1
|
||||
elif (($# < 3))
|
||||
then
|
||||
echo "\nERROR: Too few command-line arguments\n"
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check to see if the command-line switches are present
|
||||
|
||||
echo $* | grep -q '\-f' || (usage; exit 1)
|
||||
echo $* | grep -q '\-t' || (usage; exit 1)
|
||||
|
||||
# Use getopts to parse the command line arguments
|
||||
|
||||
while getopts ":f:t:" ARGUMENT
|
||||
do
|
||||
case $ARGUMENT in
|
||||
f) START_BASE="$OPTARG"
|
||||
;;
|
||||
t) END_BASE="$OPTARG"
|
||||
;;
|
||||
\?) usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Ensure that the START_BASE and END_BASE variables
|
||||
# are not NULL.
|
||||
|
||||
if [ -z "$START_BASE" ] || [ "$START_BASE" = '' ] \
|
||||
|| [ -z "$END_BASE" ] || [ "$END_BASE" = '' ]
|
||||
then
|
||||
echo "\nERROR: Base number conversion fields are empty\n"
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ensure that the START_BASE and END_BASE variables
|
||||
# have integer values for the number base conversion.
|
||||
|
||||
case $START_BASE in
|
||||
+([0-9])) : # Do nothing - Colon is a no-op.
|
||||
;;
|
||||
*) echo "\nERROR: $START_BASE is not an integer value"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
case $END_BASE in
|
||||
+([0-9])) : # Do nothing - Colon is a no-op.
|
||||
;;
|
||||
*) echo "\nERROR: $END_BASE is not an integer value"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
######################################################
|
||||
# BEGINNING OF MAIN
|
||||
######################################################
|
||||
|
||||
# Begin by finding the BASE_NUM to be converted.
|
||||
|
||||
# Count from 1 to the max number of command line auguments
|
||||
|
||||
while ((COUNT < MAX_COUNT))
|
||||
do
|
||||
((COUNT == COUNT + 1))
|
||||
TOKEN=$1
|
||||
case $TOKEN in
|
||||
-f) shift; shift
|
||||
((COUNT == COUNT + 1))
|
||||
;;
|
||||
-f${START_BASE}) shift
|
||||
;;
|
||||
-t) shift; shift
|
||||
((COUNT == COUNT + 1))
|
||||
;;
|
||||
-t${END_BASE}) shift
|
||||
;;
|
||||
*) BASE_NUM=$TOKEN
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Typeset the RESULT variable to the target number base
|
||||
|
||||
typeset -i$END_BASE RESULT
|
||||
|
||||
# Assign the BASE_NUM variable to the RESULT variable
|
||||
# and add the starting number base with a pound sign (#)
|
||||
# as a prefix for the conversion to take place.
|
||||
# NOTE: If an invalid number is entered a system error
|
||||
# will be displayed. An example is inputting 1114400 as
|
||||
# a binary number, which is invalid for a binary number.
|
||||
|
||||
RESULT="${START_BASE}#${BASE_NUM}"
|
||||
|
||||
# Display the result to the user or calling program.
|
||||
|
||||
echo "$RESULT"
|
||||
|
||||
# End of script...
|
||||
115
chapter14/equate_base_10_to_16.ksh
Executable file
115
chapter14/equate_base_10_to_16.ksh
Executable file
@@ -0,0 +1,115 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: equate_base_10_to_16.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 07/07/2007
|
||||
# REV: 1.2.P
|
||||
#
|
||||
# PURPOSE: This script is used to convert a base 10 number
|
||||
# to a base 16 hexadecimal representation.
|
||||
# This script expects that a base 10 number
|
||||
# is supplied as a single argument.
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 - Normal script execution
|
||||
# 1 - Usage error
|
||||
#
|
||||
#
|
||||
# REV LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to check command syntax without any execution
|
||||
#
|
||||
#
|
||||
#################################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
#################################################
|
||||
|
||||
SCRIPT_NAME=`basename $0`
|
||||
|
||||
# Set up the correct echo command usage. Many Linux
|
||||
# distributions will execute in Bash even if the
|
||||
# script specifies Korn shell. Bash shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $(basename $SHELL) in
|
||||
bash) alias echo="echo -e"
|
||||
;;
|
||||
esac
|
||||
|
||||
#################################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
#################################################
|
||||
function usage
|
||||
{
|
||||
echo "\nUSAGE: $SCRIPT_NAME {base 10 number}"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 694"
|
||||
echo "\nWill return the hexadecimal number 2b6"
|
||||
echo "\n\t...EXITING...\n"
|
||||
}
|
||||
|
||||
#################################################
|
||||
# BEGINNING OF MAIN
|
||||
#################################################
|
||||
|
||||
# Check for a single command-line argument
|
||||
|
||||
if [ $# -ne 1 ]
|
||||
then
|
||||
echo "\nERROR: A base 10 number must be supplied..."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check that this single command-line argument is a base 10 number!
|
||||
case $1 in
|
||||
+([0-9])) BASE_10_NUM=$1
|
||||
;;
|
||||
*) echo "\nERROR: $1 is NOT a base 10 number"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Assign the base 10 number to the BASE_16_NUM variable
|
||||
|
||||
BASE_16_NUM=10#$BASE_10_NUM
|
||||
|
||||
# NOTE: Since base 10 is implied by default,
|
||||
# the 10# may be omitted as a prefix. Both notations
|
||||
# are supported
|
||||
|
||||
# Now typeset the BASE_16_NUM variable to base 16.
|
||||
# This step converts the base 10 number to a base 16 number.
|
||||
|
||||
typeset -i16 BASE_16_NUM
|
||||
|
||||
# Display the resulting base 16 number representation
|
||||
|
||||
echo $BASE_16_NUM
|
||||
|
||||
# This following code is optional. It removes the number base
|
||||
# prefix. This may be helpful if using this script with
|
||||
# other programs and scripts.
|
||||
#
|
||||
# Set up the correct awk usage. Solaris needs to
|
||||
# use nawk instead of awk.
|
||||
#
|
||||
# case $(uname) in
|
||||
# SunOS) AWK="nawk"
|
||||
# ;;
|
||||
# *) AWK="awk"
|
||||
# ;;
|
||||
# esac
|
||||
#
|
||||
# Strip out the base prefix and the pound sign (#). (Optional)
|
||||
#
|
||||
# echo $BASE_16_NUM | grep -q "#"
|
||||
#
|
||||
# if (($? == 0))
|
||||
# then
|
||||
# echo $BASE_16_NUM | $AWK -F '#' '{print $2}'
|
||||
# else
|
||||
# echo $BASE_16_NUM
|
||||
# fi
|
||||
75
chapter14/equate_base_10_to_2.ksh
Executable file
75
chapter14/equate_base_10_to_2.ksh
Executable file
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: equate_base_10_to_2.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 07/07/2007
|
||||
# REV: 1.2.P
|
||||
#
|
||||
# PURPOSE: This script is used to convert a base 10 number
|
||||
# to a base 2 binary representation.
|
||||
# This scripts expects that a base 10 number
|
||||
# is supplied as a single argument.
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 - Normal script execution
|
||||
# 1 - Usage error
|
||||
#
|
||||
# REV LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to check command syntax without any execution
|
||||
#
|
||||
#################################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
#################################################
|
||||
|
||||
SCRIPT_NAME=`basename $0`
|
||||
typeset -i2 BASE_2_NUM
|
||||
typeset -i BASE_10_NUM
|
||||
|
||||
# Set up the correct echo command usage. Many Linux
|
||||
# distributions will execute in Bash even if the
|
||||
# script specifies Korn shell. Bash shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $SHELL in
|
||||
*/bin/bash) alias echo="echo -e"
|
||||
;;
|
||||
esac
|
||||
|
||||
#################################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
#################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nUSAGE: $SCRIPT_NAME base_10_number"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 14"
|
||||
echo "\nWill return the decimal base 2 number 1110 ...EXITING...\n"
|
||||
}
|
||||
|
||||
#################################################
|
||||
# BEGINNING OF MAIN
|
||||
#################################################
|
||||
|
||||
# Check for a single command line argument
|
||||
|
||||
if [ $# -ne 1 ]
|
||||
then
|
||||
echo "\nERROR: A base 10 number must be supplied..."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
BASE_10_NUM="$1"
|
||||
|
||||
BASE_2_NUM=$((10#${BASE_10_NUM}))
|
||||
|
||||
echo $BASE_2_NUM | grep "#" >/dev/null 2>&1
|
||||
if [ $? -eq 0 ]
|
||||
then
|
||||
echo $BASE_2_NUM | cut -f2 -d "#"
|
||||
else
|
||||
echo $BASE_2_NUM
|
||||
fi
|
||||
74
chapter14/equate_base_10_to_8.ksh
Executable file
74
chapter14/equate_base_10_to_8.ksh
Executable file
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: equate_base_10_to_8.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 07/07/2007
|
||||
# REV: 1.2.P
|
||||
#
|
||||
# PURPOSE: This script is used to convert a base 10 number
|
||||
# to a base 8 octal representation.
|
||||
# This scripts expects that a base 10 number
|
||||
# is supplied as a single argument.
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 - Normal script execution
|
||||
# 1 - Usage error
|
||||
#
|
||||
# REV LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to check command syntax without any execution
|
||||
#
|
||||
#################################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
#################################################
|
||||
|
||||
SCRIPT_NAME=`basename $0`
|
||||
typeset -i8 BASE_8_NUM
|
||||
typeset -i BASE_10_NUM
|
||||
|
||||
# Set up the correct echo command usage. Many Linux
|
||||
# distributions will execute in Bash even if the
|
||||
# script specifies Korn shell. Bash shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $SHELL in
|
||||
*/bin/bash) alias echo="echo -e"
|
||||
;;
|
||||
esac
|
||||
|
||||
#################################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
#################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nUSAGE: $SCRIPT_NAME base_10_number"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 946"
|
||||
echo "\nWill return the octal base 8 number 1662 ...EXITING...\n"
|
||||
}
|
||||
|
||||
#################################################
|
||||
# BEGINNING OF MAIN
|
||||
#################################################
|
||||
|
||||
# Check for a single command line argument
|
||||
|
||||
if [ $# -ne 1 ]
|
||||
then
|
||||
echo "\nERROR: A base 10 number must be supplied..."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
BASE_10_NUM="$1"
|
||||
|
||||
BASE_8_NUM=$((10#${BASE_10_NUM}))
|
||||
echo $BASE_8_NUM | grep "#" >/dev/null 2>&1
|
||||
if [ $? -eq 0 ]
|
||||
then
|
||||
echo $BASE_8_NUM | cut -f2 -d "#"
|
||||
else
|
||||
echo $BASE_8_NUM
|
||||
fi
|
||||
66
chapter14/equate_base_16_to_10.ksh
Executable file
66
chapter14/equate_base_16_to_10.ksh
Executable file
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: equate_base_16_to_10.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 07/07/2007
|
||||
# REV: 1.2.P
|
||||
#
|
||||
# PURPOSE: This script is used to convert a base 16 number
|
||||
# to a base 10 decimal representation.
|
||||
# This scripts expects that a base 16 number
|
||||
# is supplied as a single argument.
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 - Normal script execution
|
||||
# 1 - Usage error
|
||||
#
|
||||
# REV LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to check command syntax without any execution
|
||||
#
|
||||
#################################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
#################################################
|
||||
|
||||
SCRIPT_NAME=`basename $0`
|
||||
|
||||
# Set up the correct echo command usage. Many Linux
|
||||
# distributions will execute in Bash even if the
|
||||
# script specifies Korn shell. Bash shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $SHELL in
|
||||
*/bin/bash) alias echo="echo -e"
|
||||
;;
|
||||
esac
|
||||
|
||||
#################################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
#################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nUSAGE: $SCRIPT_NAME base_16_number"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME FC23"
|
||||
echo "\nWill return the decimal base 10 number 64547 ...EXITING...\n"
|
||||
}
|
||||
|
||||
#################################################
|
||||
# BEGINNING OF MAIN
|
||||
#################################################
|
||||
|
||||
# Check for a single command line argument
|
||||
|
||||
if [ $# -ne 1 ]
|
||||
then
|
||||
echo "\nERROR: A base 16 number must be supplied..."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
BASE_16_NUM="$1"
|
||||
|
||||
BASE_10_NUM=$((16#${BASE_16_NUM}))
|
||||
echo $BASE_10_NUM
|
||||
74
chapter14/equate_base_16_to_2.ksh
Executable file
74
chapter14/equate_base_16_to_2.ksh
Executable file
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: equate_base_16_to_2.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 07/07/2007
|
||||
# REV: 1.2.P
|
||||
#
|
||||
# PURPOSE: This script is used to convert a base 16 hexidecimal
|
||||
# number to a base 2 binary representation.
|
||||
# This scripts expects that a base 16 number
|
||||
# is supplied as a single argument.
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 - Normal script execution
|
||||
# 1 - Usage error
|
||||
#
|
||||
# REV LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to check command syntax without any execution
|
||||
#
|
||||
#################################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
#################################################
|
||||
|
||||
SCRIPT_NAME=`basename $0`
|
||||
|
||||
# Set up the correct echo command usage. Many Linux
|
||||
# distributions will execute in Bash even if the
|
||||
# script specifies Korn shell. Bash shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $SHELL in
|
||||
*/bin/bash) alias echo="echo -e"
|
||||
;;
|
||||
esac
|
||||
|
||||
#################################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
#################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nUSAGE: $SCRIPT_NAME base_16_number"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME FC23"
|
||||
echo "\nWill return the binary base 2 number 64547 ...EXITING...\n"
|
||||
}
|
||||
|
||||
#################################################
|
||||
# BEGINNING OF MAIN
|
||||
#################################################
|
||||
|
||||
# Check for a single command line argument
|
||||
|
||||
if [ $# -ne 1 ]
|
||||
then
|
||||
echo "\nERROR: A base 16 number must be supplied..."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check for a hexidecimal number!
|
||||
|
||||
case $1 in
|
||||
+([-0-9]|[a-f]|[A-F])) BASE_16_NUM=$1
|
||||
;;
|
||||
*) usage
|
||||
;;
|
||||
esac
|
||||
|
||||
BASE_2_NUM=$((16#${BASE_16_NUM}))
|
||||
typeset -i2 BASE_2_NUM
|
||||
echo $BASE_2_NUM
|
||||
82
chapter14/equate_base_2_to_10.ksh
Executable file
82
chapter14/equate_base_2_to_10.ksh
Executable file
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: equate_base_2_to_10.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 07/07/2007
|
||||
# REV: 1.2.P
|
||||
#
|
||||
# PURPOSE: This script is used to convert a base 2 number
|
||||
# to a base 10 decimal representation.
|
||||
# This scripts expects that a base 2 number
|
||||
# is supplied as a single argument.
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 - Normal script execution
|
||||
# 1 - Usage error
|
||||
#
|
||||
# REV LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to check command syntax without any execution
|
||||
#
|
||||
#################################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
#################################################
|
||||
|
||||
SCRIPT_NAME=`basename $0`
|
||||
typeset -i BASE_2_NUM
|
||||
typeset -i BASE_10_NUM
|
||||
|
||||
# Set up the correct echo command usage. Many Linux
|
||||
# distributions will execute in Bash even if the
|
||||
# script specifies Korn shell. Bash shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $SHELL in
|
||||
*/bin/bash) alias echo="echo -e"
|
||||
;;
|
||||
esac
|
||||
|
||||
#################################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
#################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nUSAGE: $SCRIPT_NAME base_2_number"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 1110"
|
||||
echo "\nWill return the decimal base 10 number 14 ...EXITING...\n"
|
||||
}
|
||||
|
||||
#################################################
|
||||
# BEGINNING OF MAIN
|
||||
#################################################
|
||||
|
||||
# Check for a single command line argument
|
||||
|
||||
if [ $# -ne 1 ]
|
||||
then
|
||||
echo "\nERROR: A base 2 number must be supplied..."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check that this single command line argument is a binary number!
|
||||
|
||||
case $1 in
|
||||
+([-0-1])) BASE_2_NUM=$1
|
||||
;;
|
||||
*) echo "\nERROR: $1 is NOT a base 2 number"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Assign the base 2 number to the BASE_10_NUM variable
|
||||
|
||||
BASE_2_NUM="$1"
|
||||
|
||||
BASE_10_NUM=$((2#${BASE_2_NUM}))
|
||||
|
||||
echo $BASE_10_NUM
|
||||
87
chapter14/equate_base_2_to_16.ksh
Executable file
87
chapter14/equate_base_2_to_16.ksh
Executable file
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: equate_base_2_to_16.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 07/07/2007
|
||||
# REV: 1.2.P
|
||||
#
|
||||
# PURPOSE: This script is used to convert a base 2 number
|
||||
# to a base 16 hexadecimal representation.
|
||||
# This scripts expects that a base 2 number
|
||||
# is supplied as a single argument.
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 - Normal script execution
|
||||
# 1 - Usage error
|
||||
#
|
||||
# REV LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to check command syntax without any execution
|
||||
#
|
||||
#################################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
#################################################
|
||||
|
||||
SCRIPT_NAME=`basename $0`
|
||||
|
||||
# Set up the correct echo command usage. Many Linux
|
||||
# distributions will execute in Bash even if the
|
||||
# script specifies Korn shell. Bash shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $SHELL in
|
||||
*/bin/bash) alias echo="echo -e"
|
||||
;;
|
||||
esac
|
||||
|
||||
#################################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
#################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nUSAGE: $SCRIPT_NAME {base 2 number}"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 1100101101"
|
||||
echo "\nWill return the hexadecimal base 16 number 32d"
|
||||
echo "\n\t ...EXITING...\n"
|
||||
}
|
||||
|
||||
#################################################
|
||||
# BEGINNING OF MAIN
|
||||
#################################################
|
||||
|
||||
# Check for a single command line argument
|
||||
|
||||
if (($# != 1))
|
||||
then
|
||||
echo "\nERROR: A base 2 number must be supplied..."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check that this single command line argument is a binary number!
|
||||
|
||||
case $1 in
|
||||
+([-0-1])) BASE_2_NUM=$1
|
||||
;;
|
||||
*) echo "\nERROR: $1 is NOT a base 2 number"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Assign the base 2 number to the BASE_16_NUM variable
|
||||
|
||||
BASE_16_NUM=$((2#${BASE_2_NUM}))
|
||||
|
||||
# Now typeset the BASE_16_NUM variable to base 16.
|
||||
# This step converts the base 2 number to a base 16 number.
|
||||
|
||||
typeset -i16 BASE_16_NUM
|
||||
|
||||
# Display the resulting base 16 representation
|
||||
|
||||
echo $BASE_16_NUM
|
||||
|
||||
68
chapter14/equate_base_8_to_10.ksh
Executable file
68
chapter14/equate_base_8_to_10.ksh
Executable file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: equate_base_8_to_10.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 07/07/2007
|
||||
# REV: 1.2.P
|
||||
#
|
||||
# PURPOSE: This script is used to convert a base 8 number
|
||||
# to a base 10 decimal representation.
|
||||
# This scripts expects that a base 8 number
|
||||
# is supplied as a single argument.
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 - Normal script execution
|
||||
# 1 - Usage error
|
||||
#
|
||||
# REV LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to check command syntax without any execution
|
||||
#
|
||||
#################################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
#################################################
|
||||
|
||||
SCRIPT_NAME=`basename $0`
|
||||
typeset -i BASE_8_NUM
|
||||
typeset -i BASE_10_NUM
|
||||
|
||||
#################################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
#################################################
|
||||
|
||||
function usage
|
||||
{
|
||||
echo "\nUSAGE: $SCRIPT_NAME base_8_number"
|
||||
echo "\nEXAMPLE: $SCRIPT_NAME 2774"
|
||||
echo "\nWill return the decimal base 10 number 1532 ...EXITING...\n"
|
||||
}
|
||||
|
||||
#################################################
|
||||
# BEGINNING OF MAIN
|
||||
#################################################
|
||||
|
||||
# Check for a single command line argument
|
||||
|
||||
if [ $# -ne 1 ]
|
||||
then
|
||||
echo "\nERROR: A base 8 number must be supplied..."
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check that this single command line argument is a octal number!
|
||||
|
||||
case $1 in
|
||||
+([-0-8])) BASE_8_NUM=$1
|
||||
;;
|
||||
*) echo "\nERROR: $1 is NOT a base 7 number"
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
BASE_10_NUM=$((8#${BASE_8_NUM}))
|
||||
|
||||
echo $BASE_10_NUM
|
||||
103
chapter14/mk_swkey.ksh
Executable file
103
chapter14/mk_swkey.ksh
Executable file
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/ksh
|
||||
#
|
||||
# SCRIPT: mk_swkey.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 07/07/2007
|
||||
# REV: 1.2.P
|
||||
#
|
||||
# PURPOSE: This script is used to create a software
|
||||
# license key based on the IP address of the
|
||||
# system that this shell script is executed on.
|
||||
# The system is queried for the system's IP
|
||||
# address. The IP address is striped of the
|
||||
# dots (.) and each number is converted to
|
||||
# hexadecimal. Then each hex string is combined
|
||||
# into a single hex string, which is the software
|
||||
# license key.
|
||||
#
|
||||
# REV LIST:
|
||||
#
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
# set -n # Uncomment to check command syntax without any execution
|
||||
#
|
||||
#################################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
#################################################
|
||||
|
||||
# Set up the correct echo command usage. Many Linux
|
||||
# distributions will execute in Bash even if the
|
||||
# script specifies Korn shell. Bash shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $SHELL in
|
||||
*/bin/bash) alias echo="echo -e"
|
||||
echo -e "\nWARNING: This script is executing in Bash shell."
|
||||
echo "This script may fail because of an unsupported typeset"
|
||||
echo -e "command option that is only supported in Korn shell\n"
|
||||
;;
|
||||
esac
|
||||
|
||||
# Set up the correct awk usage. Solaris needs to
|
||||
# use nawk instead of awk.
|
||||
|
||||
case $(uname) in
|
||||
SunOS) alias awk="nawk"
|
||||
;;
|
||||
esac
|
||||
|
||||
#################################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
#################################################
|
||||
|
||||
function convert_base_10_to_16
|
||||
{
|
||||
# set -x # Uncomment to debug this function
|
||||
|
||||
typeset -i16 BASE_16_NUM
|
||||
|
||||
BASE_10_NUM=$1
|
||||
|
||||
BASE_16_NUM=$((10#${BASE_10_NUM}))
|
||||
|
||||
# Strip the number base prefix from the hexadecimal
|
||||
# number. This prefix is not needed here.
|
||||
|
||||
echo $BASE_16_NUM | grep -q '#'
|
||||
if (($? == 0))
|
||||
then
|
||||
echo $BASE_16_NUM | awk -F '#' '{print $2}'
|
||||
else
|
||||
echo $BASE_16_NUM
|
||||
fi
|
||||
}
|
||||
|
||||
#############################################################
|
||||
# BEGINNING OF MAIN
|
||||
#############################################################
|
||||
|
||||
# Query the system for the IP address using the "host $(hostname)"
|
||||
# command substitution.
|
||||
|
||||
IP=$(host $(hostname) | awk '{print $3}' | awk -F ',' '{print $1}')
|
||||
|
||||
# Field delimit the IP address on the dots (.) and assign each
|
||||
# number to a separate variable in a "while read" loop.
|
||||
|
||||
echo $IP | awk -F '.' '{print $1, $2, $3, $4}' | while read a b c d junk
|
||||
do
|
||||
# Convert each of the numbers in the IP address
|
||||
# into hexadecimal by calling the "convert_base_10_to16"
|
||||
# function.
|
||||
|
||||
FIRST=$(convert_base_10_to_16 $a)
|
||||
SECOND=$(convert_base_10_to_16 $b)
|
||||
THIRD=$(convert_base_10_to_16 $c)
|
||||
FORTH=$(convert_base_10_to_16 $d)
|
||||
done
|
||||
|
||||
# Combine all of the hexadecimal strings into a single
|
||||
# hexadecimal string, which represents the software key.
|
||||
|
||||
echo "${FIRST}${SECOND}${THIRD}${FORTH}"
|
||||
|
||||
Reference in New Issue
Block a user