Unix/Lunix Programming CS2351: Programming Homework Stage 1 (3 points) Given two
ID: 3905720 • Letter: U
Question
Unix/Lunix Programming
CS2351: Programming Homework Stage 1 (3 points) Given two inputs, intFile that contains a line of integers and target, write a script that determines if target exists in intFile Your program shall first check if all integers in intFile are valid. If an invalid integer is found in the file, your program shall issue an error message and exit Your script shall be executed as: yourScript intFile target Stage 2 (3 points) Modify your script in the stage 1 such that if both intFile and target are missing, the script shall prompt user to input a line of integers and a target integer to be found. Your program shall now be executed as: yourScript intFile target or yourScript You do not need to consider the situation where only one argument is provided . Stage 3 (4 points) Modify your script in the stage 2 such that the user could also specify an output file to save the result * Your program shall now be executed as: your-Script intFile target or yourScript or yourScript intFile target -o outputFile or yourScript -o outputFile Your program shall check if outputFile already exists. If it exists, it shall prompt user if s/he would like to overwrite the file. If a "y" is provided, your program shall proceed and overwrite the file with the result. If a "n" is provided, your program shall prompt user for another filename and check its existence. Submission Policy Due time: May 5th, 2018 (Saturday) 23:59 pm Submit the highest stage of your script in plain text into "Program" folder in the D2L Dropbox.Explanation / Answer
## intsearch-stage1.sh
#!/bin/bash
function intCheck()
{
ags=$@
for a in $ags
do
if [ "$a" -eq "$a" ] 2>/dev/null
then
continue
else
echo "$a" is NOT an integer
exit 1
fi
done
}
function exitsCheck()
{
tarInt=$1
shift
ags=$@
echo "Input: $ags"
echo "Search: $tarInt"
for a in $ags
do
if [ "$a" -eq "$tarInt" ] 2>/dev/null
then
echo "Status: Yes, found $a in the [$ags]"
exit 0
fi
done
echo "Status: No, $tarInt was NOT found in [$ags]"
}
function readInFil()
{
inFil=$1
targt=$2
#read the integer from the targt file
read -r tarInt < $targt
#read each line from the inFil and check if the contents are valid integers
inFilLine=""
while IFS='' read -r line || [[ -n "$line" ]]
do
intCheck $line
inFilLine=$line
done < "$inFil"
#Now, check if the targt integer (from the targt file) exists in the inFil's contents
exitsCheck $tarInt $inFilLine
}
inFil=$1
targt=$2
if [ "$inFil" == "" ] || [ "$targt" == "" ]
then
echo "Please provide both the input and targt file names"
exit 1
fi
readInFil $inFil $targt
Sample Output:
>> ./intsearch-stage1.sh infill targt
Input: 1 3 5 7 9 11 13 15
Search: 15
Status: Yes, found 15 in the [1 3 5 7 9 11 13 15]
## intsearch-stage2.sh ##
#!/bin/bash
function intCheck()
{
ags=$@
for a in $ags
do
if [ "$a" -eq "$a" ] 2>/dev/null
then
continue
else
echo "$a" is NOT an integer
exit 1
fi
done
}
function exitsCheck()
{
tarInt=$1
shift
ags=$@
echo "Input: $ags"
echo "Search: $tarInt"
for a in $ags
do
if [ "$a" -eq "$tarInt" ] 2>/dev/null
then
echo "Status: Yes, found $a in the [$ags]"
exit 0
fi
done
echo "Status: No, $tarInt was NOT found in [$ags]"
}
function readInFil()
{
inFil=$1
targt=$2
#read the integer from the targt file
read -r tarInt < $targt
#read each line from the inFil and check if the contents are valid integers
inFilLine=""
while IFS='' read -r line || [[ -n "$line" ]]
do
intCheck $line
inFilLine=$line
done < "$inFil"
#Now, check if the targt integer (from the targt file) exists in the inFil's contents
exitsCheck $tarInt $inFilLine
}
function consoleRead()
{
read -p "Enter a list of integers: " input
read -p "Enter the integer to be searched: " search
intCheck $input
exitsCheck $search $input
}
if [ $# -ne 2 ]
then
consoleRead
else
inFil=$1
targt=$2
readInFil $inFil $targt
fi
Sample Output:
>> ./intsearch-stage2.sh
Enter a list of integers: 11 22 33 44 55 66
Enter the integer to be searched: 99
Input: 11 22 33 44 55 66
Search: 99
Status: No, 99 was NOT found in [11 22 33 44 55 66]
## intsearch-stage3.sh ##
#!/bin/bash
OUTFIL="/dev/fd/1"
function intCheck()
{
ags=$@
for a in $ags
do
if [ "$a" -eq "$a" ] 2>/dev/null
then
continue
else
echo "$a" is NOT an integer >$OUTFIL
exit 1
fi
done
}
function exitsCheck()
{
tarInt=$1
shift
ags=$@
echo "Input: $ags" > $OUTFIL
echo "Search: $tarInt" >> $OUTFIL
for a in $ags
do
if [ "$a" -eq "$tarInt" ] 2>/dev/null
then
echo "Status: Yes, found $a in the [$ags]">> $OUTFIL
exit 0
fi
done
echo "Status: No, $tarInt was NOT found in [$ags]">> $OUTFIL
}
function fileRead()
{
inFil=$1
targt=$2
#read the integer from the targt file
read -r tarInt < $targt
#read each line from the inFil and check if the contents are valid integers
inFilLine=""
while IFS='' read -r line || [[ -n "$line" ]]
do
intCheck $line
inFilLine=$line
done < "$inFil"
#Now, check if the targt integer (from the targt file) exists in the inFil's contents
exitsCheck $tarInt $inFilLine
}
function consoleRead()
{
read -p "Enter a list of integers: " input
read -p "Enter the integer to be searched: " search
intCheck $input
exitsCheck $search $input
}
function checkOutFil()
{
OUTFIL=$1
while true
do
if [ -f $OUTFIL ]
then
read -p "Do you want to overwrite the file (Y/N): " choice
case $choice in
[Yy])
break
;;
[N/n])
read -p "Enter a new output file: " OUTFIL
OUTFIL=$OUTFIL
;;
esac
else
#The output file does NOT exist
break
fi
done
}
numOfAgs=$#
case ${numOfAgs} in
"4")
inFil=$1
targt=$2
outputSwitch=$3
OUTFIL=$4
checkOutFil $OUTFIL
fileRead $inFil $targt
;;
"2")
if [ "$1" == "-o" ]
then
OUTFIL=$2
checkOutFil $OUTFIL
consoleRead
else
fileRead $1 $2
fi
;;
"0")
consoleRead
;;
*)
echo "Usage"
echo "$0 <inFil> <targt>"
echo "$0 <inFil> <targt> [-o output file]"
echo "$0 [-o output file]"
echo "$0"
;;
esac
Sample output:
>> ./intsearch-stage3.sh –o outfile
Do you want to overwrite the file (Y/N): Y
Enter a list of integers: 11 22 33 44 55 66
Enter the integer to be searched: 33
>> cat outfile
Input: 11 22 33 44 55 66
Search: 33
Status: Yes, found 33 in the [11 22 33 44 55 66]
** inFil **
1 3 5 7 9 11 13 15
** targt **
15
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.