Skip to content

Instantly share code, notes, and snippets.

@nmagee
Last active February 5, 2025 20:17
Show Gist options
  • Save nmagee/34217f92e1dbd49692c68d674d753072 to your computer and use it in GitHub Desktop.
Save nmagee/34217f92e1dbd49692c68d674d753072 to your computer and use it in GitHub Desktop.
In-class scripting exercises

bash Scripting Exercises

  1. Download the files below. Then try running the textstats.sh script with each of them.

  2. Play wordle using the script below. Can you figure out

    • How to print the secret word before you start guessing? and
    • Where bash is getting the list of 5-letter words?
  3. Using the fileinfo.sh script below, complete the script to display basic information about any file passed as an argument to the script. You will have to complete line 8 to begin, as well as use echo to display values to the user. BONUS: add another variable that calculates how many lines the file contains. Display the 4 file attributes.

  4. Write a bash mad-lib script. Here is an AI mad-lib generator to steal from, or use ChatGPT/Gemini/etc.

  5. Write a script that will fetch, decompress, and clean a TSV bundle, then convert it into a CSV file and re-compress it as a tar.gz bundle. Write it in general form so that you can pass the URL of any remote tar.gz file to it. Instructions are here.

Sample files

  • Sample data - https://s3.amazonaws.com/ds2002-resources/labs/lab3-bundle.tar.gz - tar-zipped TSV
  • Stock Data - https://s3.amazonaws.com/ds2002-resources/labs/stock_data.tsv - TSV
  • Moby Dick - https://s3.amazonaws.com/ds2002-resources/labs/mobydick.txt (plain text)
  • Flight Log - https://s3.amazonaws.com/ds2002-resources/labs/flights.csv - CSV

Resources

#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi
filename=
if [ ! -e "$filename" ]; then
echo "Error: File '$filename' not found."
exit 1
fi
filesize=$(ls -lh "$filename" | awk '{print $5}')
filetype=$(file "$filename" | awk -F: '{print $2}' | xargs) # xargs removes extra whitespace
lastmod=$(date -r "$filename" "+%Y-%m-%d %H:%M:%S")
# add another var in this group that displays how many lines the file contains.
# echo out those fields:values each on its own line
exit 0
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi
filename="$1"
if [ ! -f "$filename" ]; then
echo "Error: '$filename' is not a file."
exit 1
fi
total_words=0
line_count=0
longest_line=""
max_words=0
while IFS= read -r line; do
line_count=$((line_count + 1))
word_count=$(echo "$line" | wc -w)
total_words=$((total_words + word_count))
echo "Line $line_count: $word_count words"
if (( word_count > max_words )); then
max_words="$word_count"
longest_line="Line $line_count ($word_count words)"
fi
done < "$filename"
if (( line_count > 0 )); then # Avoid division by zero
average_words=$((total_words / line_count))
echo "Average words per line: $average_words"
else
echo "File is empty."
fi
echo "Longest line: $longest_line"
exit 0
#!/bin/bash
# Thanks to GitHub user huytd for this: https://gist.github.com/huytd/6a1a6a7b34a0d0abcac00b47e3d01513
words=($(grep '^\w\w\w\w\w$' /usr/share/dict/words | tr '[a-z]' '[A-Z]'))
actual=${words[$[$RANDOM % ${#words[@]}]]} end=false guess_count=0 max_guess=6
if [[ $1 == "unlimit" ]]; then
max_guess=999999
fi
while [[ $end != true ]]; do
guess_count=$(( $guess_count + 1 ))
if [[ $guess_count -le $max_guess ]]; then
echo "Enter your guess ($guess_count / $max_guess):"
read guess
guess=$(echo $guess | tr '[a-z]' '[A-Z]')
if [[ " ${words[*]} " =~ " $guess " ]]; then
output="" remaining=""
if [[ $actual == $guess ]]; then
echo "You guessed right!"
for ((i = 0; i < ${#actual}; i++)); do
output+="\033[30;102m ${guess:$i:1} \033[0m"
done
printf "$output\n"
end=true
else
for ((i = 0; i < ${#actual}; i++)); do
if [[ "${actual:$i:1}" != "${guess:$i:1}" ]]; then
remaining+=${actual:$i:1}
fi
done
for ((i = 0; i < ${#actual}; i++)); do
if [[ "${actual:$i:1}" != "${guess:$i:1}" ]]; then
if [[ "$remaining" == *"${guess:$i:1}"* ]]; then
output+="\033[30;103m ${guess:$i:1} \033[0m"
remaining=${remaining/"${guess:$i:1}"/}
else
output+="\033[30;107m ${guess:$i:1} \033[0m"
fi
else
output+="\033[30;102m ${guess:$i:1} \033[0m"
fi
done
printf "$output\n"
fi
else
echo "Please enter a valid word with 5 letters!";
guess_count=$(( $guess_count - 1 ))
fi
else
echo "You lose! The word is:"
echo $actual
end=true
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment