initial commit

This commit is contained in:
Fabio Scotto di Santolo
2020-07-28 19:28:25 +02:00
commit 4cc88d2f6e
245 changed files with 22820 additions and 0 deletions

17
chapter11/elapsed_time Executable file
View File

@@ -0,0 +1,17 @@
elapsed_time ()
{
# This function is intended for Bash shell scripts
SEC=$1
(( SEC < 60 )) && echo -e "[Elasped time: \
$SEC seconds]\c"
(( SEC >= 60 && SEC < 3600 )) && echo -e \
"[Elasped time: $(( SEC / 60 )) min $(( SEC % 60 )) sec]\c"
(( SEC > 3600 )) && echo -e "[Elasped time: \
$(( SEC / 3600 )) hr $(( (SEC % 3600) / 60 )) min \
$(( (SEC % 3600) % 60 )) sec]\c"
}

View File

@@ -0,0 +1,19 @@
build_random_line ()
{
# This function extracts random characters
# from the KEYS array by using the RANDOM
# shell variable
C=1
LINE=
until (( C > 79 ))
do
LINE="${LINE}${KEYS[$(($RANDOM % X + 1))]}"
(( C = C + 1 ))
done
# Return the line of random characters
echo "$LINE"
}

View File

@@ -0,0 +1,6 @@
function get_date_time_stamp
{
DATE_STAMP=$(date +'%m%d%y.%H%M%S')
echo $DATE_STAMP
}

View File

@@ -0,0 +1,8 @@
function get_random_number
{
# This function gets the next random number from the
# $RANDOM variable. The range is 0 to 32767.
echo "$RANDOM"
}

6
chapter11/function_get_second Executable file
View File

@@ -0,0 +1,6 @@
function get_second
{
THIS_SECOND=$(date +%S)
echo $THIS_SECOND
}

View File

@@ -0,0 +1,27 @@
function in_range_fixed_length_random_number_typeset
{
# Create a pseudo-random number less than or equal
# to the $UPPER_LIMIT value, which is user defined.
# This function will also pad the output with leading
# zeros to keep the number of digits consistent using
# the typeset command.
# Find the length of each character string
UL_LENGTH=$(echo ${#UPPER_LIMIT})
# Fix the length of the RANDOM_NUMBER variable to
# the length of the UPPER_LIMIT variable, specified
# by the $UL_LENGTH variable.
typeset -Z$UL_LENGTH RANDOM_NUMBER
# Create a fixed length pseudo-random number
RANDOM_NUMBER=$(($RANDOM % $UPPER_LIMIT + 1))
# Return the value of the fixed length $RANDOM_NUMBER
echo $RANDOM_NUMBER
}

View File

@@ -0,0 +1,10 @@
function in_range_random_number
{
# Create a pseudo-random number less than or equal
# to the $UPPER_LIMIT value, which is user defined
RANDOM_NUMBER=$(($RANDOM % $UPPER_LIMIT + 1))
echo "$RANDOM_NUMBER"
}

View File

@@ -0,0 +1,15 @@
load_default_keyboard ()
{
# Loop through each character in the following list and
# append each character to the $CHAR_FILE file. This
# produces a file with one character on each line.
for CHAR in 1 2 3 4 5 6 7 8 9 0 q w e r t y u i o \
p a s d f g h j k l z x c v b n m \
Q W E R T Y U I O P A S D F G H J K L \
Z X C V B N M 0 1 2 3 4 5 6 7 8 9
do
echo "$CHAR" >> $CHAR_FILE
done
}

11
chapter11/function_my_program Executable file
View File

@@ -0,0 +1,11 @@
function my_program
{
# Put anything you want to process in this function. I
# recommend that you specify an external program of shell
# script to execute.
echo "HELLO WORLD - $DATE_ST" > $UNIQUE_FN &
# : # No-Op - Does nothing but has a return code of zero
}

168
chapter11/mk_unique_filename.ksh Executable file
View File

@@ -0,0 +1,168 @@
#!/usr/bin/ksh
#
# AUTHOR: Randy Micahel
# SCRIPT: mk_unique_filename.ksh
# DATE: 11/12/2007
# REV: 1.2.P
#
# PLATFORM: Not Platform Dependent
#
# EXIT CODES:
# 0 - Normal script execution
# 1 - Usage error
#
# REV LIST:
#
#
# set -x # Uncomment to debug
# set -n # Uncomment to debug without any execution
#
####################################################
########## DEFINE FUNCTIONS HERE ###################
####################################################
function usage
{
echo "\nUSAGE: $SCRIPT_NAME base_file_name\n"
}
####################################################
function get_date_time_stamp
{
DATE_STAMP=$(date +'%m%d%y.%H%M%S')
echo $DATE_STAMP
}
####################################################
function get_second
{
THIS_SECOND=$(date +%S)
echo $THIS_SECOND
}
####################################################
function in_range_fixed_length_random_number_typeset
{
# Create a pseudo-random number less than or equal
# to the $UPPER_LIMIT value, which is user defined.
# This function will also pad the output with leading
# zeros to keep the number of digits consistent using
# the typeset command.
# Find the length of each character string
UL_LENGTH=$(echo ${#UPPER_LIMIT})
# Fix the length of the RANDOM_NUMBER variable to
# the length of the UPPER_LIMIT variable, specified
# by the $UL_LENGTH variable.
typeset -Z$UL_LENGTH RANDOM_NUMBER
# Create a fixed length pseudo-random number
RANDOM_NUMBER=$(($RANDOM % $UPPER_LIMIT + 1))
# Return the value of the fixed length $RANDOM_NUMBER
echo $RANDOM_NUMBER
}
####################################################
function my_program
{
# Put anything you want to process in this function. I
# recommend that you specify an external program of shell
# script to execute.
echo "HELLO WORLD - $DATE_ST" > $UNIQUE_FN &
# : # No-Op - Does nothing but has a return code of zero
}
####################################################
################ BEGINNING OF MAIN #################
####################################################
SCRIPT_NAME=$(basename $0) # Query the system for this script name
# Check for the correct number of arguments - exactly 1
if (( $# != 1 ))
then
echo "\nERROR: Usage error...EXITING..."
usage
exit 1
fi
# What filename do we need to make unique?
BASE_FN=$1 # Get the BASE filenaname to make unique
RANDOM=$$ # Initialize the RANDOM environment variable
# with the current process ID (PID)
UPPER_LIMIT=32767 # Set the UPPER_LIMIT
CURRENT_SECOND=99 # Initialize to a non-second
LAST_SECOND=98 # Initialize to a non-second
USED_NUMBERS= # Initialize to null
PROCESSING="TRUE" # Initialize to run mode
while [[ $PROCESSING = "TRUE" ]]
do
DATE_ST=$(get_date_time_stamp) # Get the current date/time
CURRENT_SECOND=$(get_second) # Get the current second
RN=$(in_range_fixed_length_random_number_typeset) # Get a new number
# Check to see if we have already used this number this second
if (( CURRENT_SECOND == LAST_SECOND ))
then
UNIQUE=FALSE # Initialize to FALSE
while [[ "$UNIQUE" != "TRUE" ]] && [[ ! -z $UNIQUE ]]
do
# Has this number already been used this second?
echo $USED_NUMBERS | grep $RN >/dev/null 2>&1
if (( $? == 0 ))
then
# Has been used...Get another number
RN=$(in_range_fixed_length_random_number_typeset)
else
# Number is unique this second...
UNIQUE=TRUE
# Add this numner to the used number list
USED_NUMBERS="$USED_NUMBERS $RN"
fi
done
else
USED_NUMBERS= # New second...Reinitialize to null
fi
# Assign the unique filename to the UNIQUE_FN variable
UNIQUE_FN=${BASE_FN}.${DATE_ST}.$RN
echo $UNIQUE_FN # Comment out this line!!
LAST_SECOND=$CURRENT_SECOND # Save the last second value
# We have a unique filename...
#
# PROCESS SOMETHING HERE AND REDIRECT OUTPUT TO $UNIQUE_FN
#
my_program
#
# IF PROCESSING IS FINISHED ASSIGN "FALSE" to the PROCESSING VARIABLE
#
# if [[ $MY_PROCESS = "done" ]]
# then
# PROCESSING="FALSE"
# fi
done

204
chapter11/random_file.bash Executable file
View File

@@ -0,0 +1,204 @@
#!/bin/bash
#
# SCRIPT: random_file.bash
# AUTHOR: Randy Michael
# DATE: 8/3/2007
# REV: 1.0
# PLATFORM: Not Platform Dependent
#
# PURPOSE: This script is used to create a
# specific size file of random characters.
# The methods used in this script include
# loading an array with alphanumeric
# characters, then using the /dev/random
# character special file to seed the RANDOM
# shell variable, which in turn is used to
# extract a random set of characters from the
# KEYS array to build the OUTFILE of a
# specified MB size.
#
# set -x # Uncomment to debug this script
#
# set -n # Uncomment to check script syntax
# # without aby execution. Do not forget
# # to put the comment back in or the
# # script will never execute.
#
##########################################
# DEFINE FILES AND VARIABLES HERE
##########################################
typeset -i MB_SIZE=$1
typeset -i RN
typeset -i i=1
typeset -i X=0
WORKDIR=/scripts
OUTFILE=${WORKDIR}/largefile.random.txt
>$OUTFILE
THIS_SCRIPT=$(basename $0)
CHAR_FILE=${WORKDIR}/char_file.txt
##########################################
# DEFINE FUNCTIONS HERE
##########################################
build_random_line ()
{
# This function extracts random characters
# from the KEYS array by using the RANDOM
# shell variable
C=1
LINE=
until (( C > 79 ))
do
LINE="${LINE}${KEYS[$(($RANDOM % X + 1))]}"
(( C = C + 1 ))
done
# Return the line of random characters
echo "$LINE"
}
##########################################
elasped_time ()
{
SEC=$1
(( SEC < 60 )) && echo -e "[Elasped time: \
$SEC seconds]\c"
(( SEC >= 60 && SEC < 3600 )) && echo -e \
"[Elasped time: $(( SEC / 60 )) min $(( SEC % 60 )) sec]\c"
(( SEC > 3600 )) && echo -e "[Elasped time: \
$(( SEC / 3600 )) hr $(( (SEC % 3600) / 60 )) min \
$(( (SEC % 3600) % 60 )) sec]\c"
}
##########################################
load_default_keyboard ()
{
# Loop through each character in the following list and
# append each character to the $CHAR_FILE file. This
# produces a file with one character on each line.
for CHAR in 1 2 3 4 5 6 7 8 9 0 q w e r t y u i o \
p a s d f g h j k l z x c v b n m \
Q W E R T Y U I O P A S D F G H J K L \
Z X C V B N M 0 1 2 3 4 5 6 7 8 9
do
echo "$CHAR" >> $CHAR_FILE
done
}
##########################################
usage ()
{
echo -e "\nUSAGE: $THIS_SCRIPT Mb_size"
echo -e "Where Mb_size is the size of the file to build\n"
}
##########################################
# BEGINNING OF MAIN
##########################################
if (( $# != 1 ))
then
usage
exit 1
fi
# Test for an integer value
case $MB_SIZE in
[0-9]) : # Do nothing
;;
*) usage
;;
esac
# Test for the $CHAR_FILE
if [ ! -s "$CHAR_FILE" ]
then
echo -e "\nNOTE: $CHAR_FILE does not esist"
echo "Loading default keyboard data."
echo -e "Creating $CHAR_FILE...\c"
load_default_keyboard
echo "Done"
fi
# Load Character Array
echo -e "\nLoading array with alphanumeric character elements"
while read ARRAY_ELEMENT
do
(( X = X + 1 ))
KEYS[$X]=$ARRAY_ELEMENT
done < $CHAR_FILE
echo "Total Array Character Elements: $X"
# Use /dev/random to seed the shell variable RANDOM
echo "Querying the kernel random number generator for a random seed"
RN=$(dd if=/dev/random count=1 2>/dev/null \
| od -t u4 | awk '{print $2}'| head -n 1)
# The shell variable RANDOM is limited to 32767
echo "Reducing the random seed value to between 1 and 32767"
RN=$(( RN % 32767 + 1 ))
# Initialize RANDOM with a new seed
echo "Assigning a new seed to the RANDOM shell variable"
RANDOM=$RN
echo "Building a $MB_SIZE MB random character file ==> $OUTFILE"
echo "Please be patient, this may take some time to complete..."
echo -e "Executing: .\c"
# Reset the shell SECONDS variable to zero seconds.
SECONDS=0
TOT_LINES=$(( MB_SIZE * 12800 ))
until (( i > TOT_LINES ))
do
build_random_line >> $OUTFILE
(( $(( i % 100 )) == 0 )) && echo -e ".\c"
(( i = i + 1 ))
done
# Capture the total seconds
TOT_SEC=$SECONDS
echo -e "\n\nSUCCESS: $OUTFILE created at $MB_SIZE MB\n"
elasped_time $TOT_SEC
# Calculate the bytes/second file creation rate
(( MB_SEC = ( MB_SIZE * 1024000 ) / TOT_SEC ))
echo -e "\n\nFile Creation Rate: $MB_SEC bytes/second\n"
echo -e "File size:\n"
ls -l $OUTFILE
echo
##########################################
# END OF random_file.bash SCRIPT
##########################################

150
chapter11/random_number.ksh Executable file
View File

@@ -0,0 +1,150 @@
#!/usr/bin/ksh
#
# AUTHOR: Randy Micahel
# SCRIPT: random_number.ksh
# DATE: 11/12/2007
# REV: 1.2.P
#
# PLATFORM: Not Platform Dependent
#
# EXIT CODES:
# 0 - Normal script execution
# 1 - Usage error
#
# REV LIST:
#
#
# set -x # Uncomment to debug
# set -n # Uncomment to check syntax without any command execution
#
####################################################
########## DEFINE FUNCTIONS HERE ###################
####################################################
function usage
{
echo "\nUSAGE: $SCRIPT_NAME [-f] [upper_number_range]"
echo "\nEXAMPLE: $SCRIPT_NAME"
echo "Will return a random number between 0 and 32767"
echo "\nEXAMPLE: $SCRIPT_NAME 1000"
echo "Will return a random number between 1 and 1000"
echo "\nEXAMPLE: $SCRIPT_NAME -f 1000"
echo "Will add leading zeros to a random number from"
echo "1 to 1000, which keeps the number of digits consistant\n"
}
####################################################
function get_random_number
{
# This function gets the next random number from the
# $RANDOM variable. The range is 0 to 32767.
echo "$RANDOM"
}
####################################################
function in_range_random_number
{
# Create a pseudo-random number less than or equal
# to the $UPPER_LIMIT value, which is user defined
RANDOM_NUMBER=$(($RANDOM % $UPPER_LIMIT + 1))
echo "$RANDOM_NUMBER"
}
####################################################
function in_range_fixed_length_random_number_typeset
{
# Create a pseudo-random number less than or equal
# to the $UPPER_LIMIT value, which is user defined.
# This function will also pad the output with leading
# zeros to keep the number of digits consistent using
# the typeset command.
# Find the length of each character string
UL_LENGTH=$(echo ${#UPPER_LIMIT})
# Fix the length of the RANDOM_NUMBER variable to
# the length of the UPPER_LIMIT variable, specified
# by the $UL_LENGTH variable.
typeset -Z$UL_LENGTH RANDOM_NUMBER
# Create a fixed length pseudo-random number
RANDOM_NUMBER=$(($RANDOM % $UPPER_LIMIT + 1))
# Return the value of the fixed length $RANDOM_NUMBER
echo $RANDOM_NUMBER
}
####################################################
############## BEGINNING OF MAIN ###################
####################################################
SCRIPT_NAME=`basename $0`
RANDOM=$$ # Initialize the RANDOM environment variable
# using the PID as the initial seed
case $# in
0) get_random_number
;;
1) UPPER_LIMIT="$1"
# Test to see if $UPPER_LIMIT is a number
case $UPPER_LIMIT in
+([0-9])) : # Do Nothing...It's a number
# NOTE: A colon (:) is a no-op in Korn shell
;;
*) echo "\nERROR: $UPPER_LIMIT is not a number..."
usage
exit 1
;;
esac
in_range_random_number
;;
2) # Check for the -f switch to fix the length.
if [[ $1 = '-f' ]] || [[ $1 = '-F' ]]
then
UPPER_LIMIT="$2"
# Test to see if $UPPER_LIMIT is a number
case $UPPER_LIMIT in
+([0-9])) : # Do Nothing...It's a number
# NOTE: A colon (:) is a no-op in Korn shell
;;
*) echo "\nERROR: $UPPER_LIMIT is not a number..."
usage
exit 1
;;
esac
in_range_fixed_length_random_number_typeset
else
echo "\nInvalid argument $1, see usage below..."
usage
exit 1
fi
;;
*) usage
exit 1
;;
esac
# End of random_number.ksh Shell Script

View File

@@ -0,0 +1,22 @@
#!/bin/bash
#
# SCRIPT: random_number_testing.bash
# AUTHOR: Randy Michael
# DATE: 8/8/2007
# For each 1K count we ger 64 lines of random numbers
dd if=/dev/urandom count=1k bs=1 2>/dev/null | od -t u2 \
| awk '{print $2}' | sed /^$/d > 64random_numbers.txt
# For each 1M count we get 65536 lines of random numbers
dd if=/dev/urandom count=1M bs=1 2>/dev/null | od -t u2 \
| awk '{print $2}' | sed /^$/d > 65536random_numbers.txt
# The opposite is /dev/zero which will create a null
# file of a specific size
dd if=/dev/zero of=1MBemptyfile count=1M bs=1 2>/dev/null