Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Created October 22, 2018 06:54
Show Gist options
  • Save rr-codes/681a629d0e443a4314be85da7eb57a78 to your computer and use it in GitHub Desktop.
Save rr-codes/681a629d0e443a4314be85da7eb57a78 to your computer and use it in GitHub Desktop.
#!/bin/bash
# —————————————————————————————————————————————————————————————————— #
# Question 1.1 #
# #
# Called via: script.sh filename1 filename2 ... #
# If number of files < 2, print "Error" and quit #
# Print which of filename1 and filename2 has larger size #
# —————————————————————————————————————————————————————————————————— #
for filename in $@; do
if [[ -f "$filename" ]]; then bool=1
else bool=0
fi
done
if [[ bool -eq 1 ]]; then
if [[ $# -lt 2 ]]; then echo -e "Use: LT3_A1.sh file_1 file_2"
else
size1="$(wc -c < "$1")"
size2="$(wc -c < "$2")"
if [[ "$size1" -gt "$size2" ]]; then
echo -e "File_1 is bigger than File_2"
elif [[ "$size1" -lt "$size2" ]]; then
echo -e "File_2 is bigger than File_1"
else
echo -e "The two files are of the same size"
fi
fi
else
echo -e "There is a problem with one of the files"
fi
# —————————————————————————————————————————————————————————————————— #
# Question 1.2 #
# #
# Print "Please enter a directory name" and read input #
# If input is a directory, list all files and quit #
# Else, print "I said a directory" and repeat #
# Finally, print "You are hopeless" and quit #
# —————————————————————————————————————————————————————————————————— #
echo "Please enter a directory name "
read dirname
if [[ -d "$dirname" ]]; then; ls "$dirname"
else
echo "I said a directory "
read newdirname
if [[ -d "$newdirname" ]]; then; ls "$newdirname"
else; echo "You are hopeless."
fi
fi
# —————————————————————————————————————————————————————————————————— #
# Question 1.3 #
# #
# Called via: script.sh filename string #
# If filename == "exit", quit program #
# If 'string' is / isn't in 'filename', print "Found" / "Not Found"; #
# Repeat in loop #
# —————————————————————————————————————————————————————————————————— #
while [[ $filename != "exit" ]]; do
read filename
read str
if [[ -f "$filename" ]]; then
if grep -q "$str" "$filename"; then; echo "Found"
else; echo "Not Found"
fi
fi
done
# —————————————————————————————————————————————————————————————————— #
# Question 1.4 / 1.5 #
# #
# Format: (ddd) ddd-dddd #
# Replace with: (xxx) ddd-dddd #
# —————————————————————————————————————————————————————————————————— #
s/([0-9][0-9][0-9])/(xxx)/p
# —————————————————————————————————————————————————————————————————— #
# Question 2.3 #
# #
# Format: last_name first_name course grade1 grade2 grade3 #
# Filename is 'Ras.txt' #
# Called via: script.sh -G -R -C arg1 arg2 #
# For -G: -> arg1 := last_name, arg2 := first_name #
# -> prints grade avg, student name, course name #
# #
# For -R: -> arg1 := course #
# -> prints list of students enrolled in course #
# #
# For -C: -> arg1 := course #
# -> prints num of students in course #
# —————————————————————————————————————————————————————————————————— #
flag="$1"
arg1="$2"
arg2="$3"
if [[ "$flag" = "-G" ]]; then
line=$(grep "$arg1 $arg2" Ras.txt)
read -r last first course gr1 gr2 gr3 <<< "$line"
let "sum = gr1 + gr2 + gr3"
let "result = sum / 3"
echo "-> $arg2 $arg1 has a $result average in $course"
fi
if [[ "$flag" = "-R" ]]; then
echo "Enrolled in $arg1:"
while read line; do
read -r last first course gr1 gr2 gr3 <<< "$line"
echo "-> $first ${last}"
done <<< "$(grep "$arg1" Ras.txt)"
fi
if [[ "$flag" = "-C" ]]; then
filename="$(grep "$arg1" Ras.txt)"
num="$(wc -l <<< $filename)"
echo "-> There are $((num+1)) students in $arg1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment