Added work from my other class repositories before deletion

This commit is contained in:
2017-11-29 10:28:24 -08:00
parent cb0b5f4d25
commit 5ea24c81b5
198 changed files with 739603 additions and 0 deletions

View File

@@ -0,0 +1,301 @@
#!/bin/bash
#
# Program 1
#
# To create a test run of your program put this file and your stats
# program, which must be named "stats", together in a directory. Then run
# this command, which will take perhaps 15 seconds:
#
# % p1gradingscript > p1results
#
# Your whole program must be contained in the single file named "stats".
#
LS=/bin/ls
RM=rm
PS=/bin/ps
TESTDIR=assign1.test.$$
STATS=./stats
if test -d $TESTDIR
then
echo Please rename or remove $TESTDIR
exit 1
fi
mkdir $TESTDIR 2>&1
cp stats $TESTDIR 2>&1
cd $TESTDIR 2>&1
chmod +x stats 2>&1
echo --------------------------------------------------------------------------
echo Creating file1 file2 file3 file4 file5
echo
cat > file1 <<EOF
93 93 93 93 93 93 93 93 100
73 84 95 83 72 86 80 97 100
85 0 82 75 88 79 80 81 100
85 0 87 73 88 79 80 71 100
80 81 83 63 100 85 63 68 100
53 57 61 53 70 61 73 50 100
55 54 41 63 63 45 33 41 100
53 55 43 44 63 75 35 21 100
100 100 100 100 100 100 100 100 100
EOF
cat > file2 <<EOF
97 95 93 91
86 80 97 99
61 73 50 100
95 94 93 92
EOF
cat > file3 <<EOF
17
EOF
cat > file4 <<EOF
EOF
i=0
while [ $i -lt 50000 ]
do
echo "$i `expr $i \* 2`"
i=`expr $i + 100`
done > file5
echo
$LS -l 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo File 1 Statistics by Rows: 1 point per correct number: 18
echo
$STATS -rows file1 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo Check for Temporary Files: 5 points if no temp files
echo
$LS 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo File 1 Statistics by Cols: 1 point per correct number: 18
echo
$STATS -cols file1 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo Check for Temporary Files: 5 points if no temp files
echo
$LS 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo File 2 Statistics by Rows: 1 point per correct number: 8
echo
$STATS -rows file2 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo File 2 Statistics by Cols: 1 point per correct number: 8
echo
$STATS -cols file2 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo File 3 Statistics by Rows: 1 point per correct number: 2
echo
$STATS -rows file3 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo File 3 Statistics by Cols: 1 point per correct number: 2
echo
$STATS -cols file3 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo File 4 Statistics by Rows: lose 3 points if response not something like FILE IS EMPTY
echo
$STATS -rows file4 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo File 4 Statistics by Cols: lose 3 points if response not something like FILE IS EMPTY
echo
$STATS -cols file4 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo File 5 Statistics by Cols: 1 point per number: 4
echo
$STATS -cols file5 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo Check for Temporary Files: 5 points if no temp files
echo
$LS 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo Good Syntax
echo
echo Standard Input on Rows: 2 points for exit value 0, 1 point for no error text
$STATS -rows < file2 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
echo Standard Input on Cols: 2 points for exit value 0, 1 point for no error text
$STATS -cols < file2 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
echo Option -cols: 2 points for exit value 0, 1 point for no error text
$STATS -cols file2 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
echo Option -rrrrrr: 2 points for exit value 0, 1 point for no error text
$STATS -rrrrrr file2 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
echo Option -cccccc: 3 points for exit value 0
$STATS -cccccc file2 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
echo Option -r: 3 points for exit value 0
$STATS -r file2 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
echo
echo --------------------------------------------------------------------------
echo Bad Syntax
echo
echo Too Few Arguments: 1 point for usage text, 2 points for exit value 1
$STATS 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
echo Too Many Arguments: 1 point for usage text, 2 points for exit value 1
$STATS -r file1 file2 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
echo Wrong Format: 1 point for usage text, 2 points for exit value 1
$STATS file1 file2 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
echo Bad Option: 1 point for usage text, 2 points for exit value 1
$STATS -x file1 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
echo
echo --------------------------------------------------------------------------
echo File Not Readable: 2 points for error msg, 2 points for program halting
echo
chmod 000 file3 2>&1
echo
$STATS -r file3 2> err.out
echo Exit Value: $?
echo Error Message:
cat err.out
echo
$RM -f err.out 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo Check for Temporary Files: 5 points for no temp files
echo
$LS 2>&1
echo
echo
echo --------------------------------------------------------------------------
echo Simultaneous Runs
echo
$STATS -rows < file1 > simrun1.out 2>&1 &
PID1=$!
$STATS -rows < file1 > simrun2.out 2>&1 &
PID2=$!
$STATS -rows < file1 > simrun3.out 2>&1 &
PID3=$!
sleep 2
echo
echo Before Finishing
$LS 2>&1
echo
while ($PS | egrep "^ *($PID1)|($PID2)|($PID3)" > /dev/null)
do
echo waiting...
sleep 5
done
echo
echo Run 1 Output: 5 points for succesful run 1
cat simrun1.out 2>&1
echo
echo Run 2 Output: 5 points for succesful run 2
cat simrun2.out 2>&1
echo
echo Run 3 Output: 5 points for succesful run 3
cat simrun3.out 2>&1
echo
$RM -f simrun[123].out 2>&1
echo
echo After Finishing: 5 points if no temp file
$LS 2>&1
echo
echo
echo Trap Signals
echo
$STATS -rows < file5 > /dev/null &
PID=$!
sleep 2
echo
echo Before Kill
$LS 2>&1
echo
$PS
echo
kill $PID
sleep 5
echo
echo After Kill: 8 points if no temp files
$LS 2>&1
echo
$PS
echo
echo
cd ..
$RM -rf $TESTDIR 2>&1

View File

@@ -0,0 +1,32 @@
stats {-rows|-cols} [input_file]
DONE: Numbers are separated by tabs, lines by newlines
DONE: Check for the correct number of arguments, failure to standard error
DONE: Check whether file is readable, or exists, stderr if not
DONE: -rows and -cols should work for anything that starts with its character eg. -rad would do rows
Stats output to stdout
Exit value for errors should be 1
You can assume each row will be less than 1000 bytes long, unix limits this, but unlimited numbers of rows
DONE: If using temp files, make sure they include process id's in name to allow for simultaneous runs. Remove files when done.
DONE: Use the trap command to catch interrupt, hangup, and terminate signals to remove temp files if terminates unexpectedly
Values and results must be whole numbers, round like normal, 7.5 to 8, 7.4 to 7
DONE: Calculations must be done with expr or commandline tools. No other languages
DONE: For median, sort values and take the middle value. If even, take the larger of the middle two values.
DONE: Must be in a single file.
DONE: You can return an error if there is no input file
HINTS

View File

@@ -0,0 +1,35 @@
% cat test_file
1 1 1 1 1
9 3 4 5 5
6 7 8 9 7
3 6 8 9 1
3 4 2 1 4
6 4 4 7 7
% stats -rows test_file
Average Median
1 1
5 5
7 7
5 6
3 3
6 6
% cat test_file | stats c
Averages:
5 4 5 5 4
Medians:
6 4 4 7 5
% echo $?
0
% stats
Usage: stats {-rows|-cols} [file]
% stats -r test_file nya-nya-nya
Usage: stats {-rows|-cols} [file]
% stats -both test_file
Usage: stats {-rows|-cols} [file]
% chmod -r test_file
% stats -columns test_file
stats: cannot read test_file
% stats -columns no_such_file
stats: cannot read no_such_file
% echo $?
1

View File

@@ -0,0 +1,235 @@
#!/bin/bash
########## Programmer Info ############
# Name: Corwin Perren
# OSU ID: 931759527
# Assignment: Assignment 1 - stats
# Filename: stats
########## Global Variables ############
# Used for transposing the axis on a two dimensional array
awk_transpose='{
for ( i=1; i <= NF; i++ )
row[i] = row[i]((row[i])?" ":"")$i
}
END{
for ( x = 1; x <= length(row) ; x++ )
print row[x]
}'
# Used to properly round the floating point values
awk_proper_rounding='
{printf("%d\n",$1 + 0.5)}
'
# Stores the PID used to name and delete temp files
master_pid=$!
########## Functions ###########
# Error function for when user input is wrong
show_usage_error ()
{
echo "Usage: stats {-rows|-cols} [file]" >&2
exit 1
}
# Error function for when a user feeds in an empty file
show_empty_file_error ()
{
echo "stats: Input empty. Please provide valid input." >&2
exit 1
}
# Error function for when the file doesn't exist or can't be accessed
show_invalid_file_error ()
{
echo "stats: Cannot read file. Please verify file exists or check permissions." >&2
exit 1
}
# Function to delete temporary files
remove_temp_if_exist ()
{
rm -f ${master_pid}"_"transposed
rm -f ${master_pid}"_"temp
}
# Function to handle file cleanup and returning an error when an interrupt happens
handle_unexpected_termination_error ()
{
remove_temp_if_exist
echo "CTRL+C received. Exiting." >&2
exit 1
}
#####################################################
#####################################################
########## "stats" script "main"-ish code ###########
# Handle unexpected termination
trap handle_unexpected_termination_error INT HUP TERM
# Determine if we're getting data from stdin or a file, error if neither
if [ $# -eq 0 ] || [ $# -gt 2 ]; then
show_usage_error
elif [ $# -eq 1 ]; then
is_stdin=1
elif [ $# -eq 2 ]; then
is_stdin=0
fi
# Determine if we're doing statistics based on columns or rows, error if neither
if [[ $1 == -r* ]]; then
is_rows=1
elif [[ $1 == -c* ]]; then
is_rows=0
else
show_usage_error
fi
# If the input is a file, make sure we can open it and that it exists
if [ ${is_stdin} -eq 0 ]; then
if [ ! -e $2 ] || [ ! -r $2 ] || [ ! -f $2 ]; then
show_invalid_file_error
fi
fi
# If stats should be on columns, transpose the table so the columns are the new rows and rows are the new columns
# This will make it so the same stats math can be used to generate the correct data
# If the data is coming from stdin and needs to be columns, it makes a temp file, stores the data in it, and then
# transposes it just as if it were a file being fed in as an argument
# If the flags here are anything but a standard in piping with rows, it opens the file with a file descriptor for access
# This also handles showing an error if the input from stdin with -cols is empty
if [ ${is_rows} -eq 0 ] && [ ${is_stdin} -eq 0 ]; then
cat $2 | awk "${awk_transpose}" > ${master_pid}"_"transposed
exec 3<> ${master_pid}"_"transposed
elif [ ${is_rows} -eq 0 ] && [ ${is_stdin} -eq 1 ]; then
line_count=0
while read current_line
do
echo -e ${current_line} >> ${master_pid}"_"temp
((line_count = line_count + 1))
done
if [ ${line_count} -eq 0 ]; then
show_empty_file_error
fi
cat ${master_pid}"_"temp | awk "${awk_transpose}" > ${master_pid}"_"transposed
exec 3<> ${master_pid}"_"transposed
is_stdin=0
elif [ ${is_rows} -eq 1 ] && [ ${is_stdin} -eq 0 ]; then
exec 3<> $2
fi
# Now we perform the stats math operations on the data
line_count=0
declare -a averages
declare -a medians
while [ 1 ];
do
# We read in the current line
if [ ${is_stdin} -eq 1 ]; then
read current_line
else
read -u 3 current_line
fi
# Here we get the result code from read, which tells us if there's data left
read_result=$?
# If there was no data, and we haven't looped yet, the file is empty and we error
# Otherwise, it means we've reached the end of the file and it's time to leave the loop
if [ ${read_result} -eq 1 ] && [ ${line_count} -eq 0 ]; then
show_empty_file_error
elif [ ${read_result} -eq 1 ]; then
break
fi
# Initialize variables for doing the calculations
sum=0
count=0
avg=0
newline="\n"
numbers_string=""
# This part does the summing and adds the numbers to a new string so it can be parsed by sort
for word in ${current_line}
do
numbers_string=${numbers_string}${newline}${word}
((sum = word + sum))
((count = count + 1))
done
# Here we use bc and awk to handle the floating point results of division and proper rounding
# The avg then gets added to the average array for display later
avg=$(echo "(${sum}/${count})" | bc -l | awk "${awk_proper_rounding}")
averages[${line_count}]=${avg}
# Now the new string we created is sorted numerically so we can easily find the median
sorted=$(echo -e ${numbers_string} | sort -n)
# Then we find and add the median number to our medians array
i=0
for word in ${sorted}
do
if [ ${i} == $(((count/2))) ]; then
medians[${line_count}]=${word}
break
fi
((i = i + 1))
done
# Here we increment our line count so we can properly handle empty files
((line_count = line_count + 1))
done
# For rows display, we print out the header then one value from averages and count, separated by tabs
if [ ${is_rows} -eq 1 ]; then
echo -e "Average\tMedian"
count=0
for word in ${averages[*]}
do
echo -e "${averages[count]}\t${medians[count]}"
((count = count + 1))
done
# For cols display, we print a header, then all the contents of average, another header, and the contents of median
# Takes a little more work to print this one and not have extra tabs left over
else
echo -e "Averages:"
first=1
for word in ${averages[*]}
do
if [ ${first} -eq 1 ]; then
echo -e -n "${word}"
first=0
else
echo -e -n "\t${word}"
fi
((count = count + 1))
done
echo
echo -e "Medians:"
first=1
for word in ${medians[*]}
do
if [ ${first} -eq 1 ]; then
echo -e -n "${word}"
first=0
else
echo -e -n "\t${word}"
fi
((count = count + 1))
done
echo
fi
# Assuming we make it this far, the trap handler will not have taken care of our temp files, so we do that now
remove_temp_if_exist
# Again, having made it this far the program has completed successfully. Exit with no error.
exit 0