Skip to content

Instantly share code, notes, and snippets.

@dandevri
Created January 24, 2019 16:56
Show Gist options
  • Save dandevri/9568a8dff8f572a0ea67627445aca5b2 to your computer and use it in GitHub Desktop.
Save dandevri/9568a8dff8f572a0ea67627445aca5b2 to your computer and use it in GitHub Desktop.
tutorial.sh
#!/bin/bash
###
### Hi students! Please don’t change this file,
### run it with `bash tutorial.sh` instead.
###
function bold {
printf "\\033[1m%s\\033[22m" "$1"
}
function green {
printf "\\033[32m%s\\033[0m" "$1"
}
function red {
printf "\\033[31m%s\\033[0m" "$1"
}
function yellow {
printf "\\033[33m%s\\033[0m" "$1"
}
function intro {
clear
if [ -n "$username" ]; then
echo "$(bold "Hi, $username! 👋")"
else
echo "$(bold "Hi! 👋")"
fi
echo "Follow this tutorial by running \`sh $currentScript\`. $(yellow "Stuck")? Use \`man\` (such"
echo "as \`man ls\`) for help!"
echo
echo "$(bold "Results") ($(yellow "$ok")/14)"
echo
for index in $(seq 0 $ok); do
case $index in
0)
echo "$(green "✓") $question0"
;;
1)
echo "$(green "✓") $question1"
;;
2)
echo "$(green "✓") $question2"
;;
3)
echo "$(green "✓") $question3:" $answer3
;;
4)
echo "$(green "✓") $question4:" $answer4
;;
5)
echo "$(green "✓") $question5"
;;
6)
echo "$(green "✓") $question6"
;;
7)
echo "$(green "✓") $question7:" $answer7
;;
8)
echo "$(green "✓") $question8:" $answer8
;;
9)
echo "$(green "✓") $question9"
;;
10)
echo "$(green "✓") $question10"
;;
11)
echo "$(green "✓") $question11:" $answer11
;;
12)
echo "$(green "✓") $question12"
;;
13)
echo "$(green "✓") $question13"
;;
14)
echo "$(green "✓") $question14"
;;
esac
done
}
###
### Questions
###
currentScript="$(basename "$0")"
typeset -i ok=0
question0="Directory"
question1="Username"
question2="Hello World"
question3="Where are we"
question4="Files"
question5="Add a file"
question6="Add a directory"
question7="Enter a directory"
question8="Exit a directory"
question9="Remove a file"
question10="Redirect"
question11="Print a file"
question12="Move a file"
question13="Copy a file"
question14="Append to file"
answer3=""
answer4=""
answer7=""
answer8=""
answer11=""
###
### Question 0: make a directory named `run`.
###
if [[ "$(basename "$(pwd)")" != "run" ]]; then
intro
echo "$(red "𐄂") Create a directory named \`run\`"
echo
echo " $(bold "$(yellow "Make sure to change the current directory to \`run\` as well")")"
exit 1
fi
###
### Question 1: Type your GitHub username.
###
intro
echo "○ $(yellow "$question1")"
echo
while true; do
printf "Could you give us your GitHub username? Type here: "
read -e username
if [ -n "$username" ]; then
code="$(base64 <<<"$username")"
(( ok++ ))
intro
break
fi
echo
echo "$(bold "$(yellow "Please try again")")"
echo
done
###
### Question 2: print `hello world`.
###
echo "○ $(yellow "$question2")"
echo
echo "The next step is to print out something."
while true; do
printf "Type the code needed to print out 'Hello World!': "
read -e cmd
if [[ "$cmd" == echo* ]]; then
if [[ "$(eval "$cmd")" == "Hello World!" ]]; then
(( ok++ ))
intro
break
fi
fi
echo
echo "$(bold "$(yellow "Hmm, that doesn’t seem right. Make sure to use the proper casing and")")"
echo "$(bold "$(yellow "don’t forget the exclamation mark! Try again.")")"
echo
done
###
### Question 3: Current working directory.
###
echo "○ $(yellow "$question3")"
echo
echo "The next step is to print where we are."
while true; do
printf "Type the code needed to print out the current working directory: "
read -e cmd
if [[ "$cmd" == pwd* ]]; then
answer3="$(eval "$cmd")"
if [[ "$answer3" == "$(pwd)" ]]; then
(( ok++ ))
intro
break
fi
fi
echo
echo "$(bold "$(yellow "Hmm, that doesn’t seem right. Try again.")")"
echo
done
###
### Question 4: Files
###
echo "○ $(yellow "$question4")"
echo
echo "The next step is to print files."
while true; do
printf "Type the code needed to print all files where we are: "
read -e cmd
if [[ "$cmd" == ls* ]]; then
answer4="$(eval "$cmd")"
if [[ "$answer4" == "$(ls)" ]]; then
(( ok++ ))
intro
break
fi
fi
echo
echo "$(bold "$(yellow "Hmm, that doesn’t seem right. Try again.")")"
echo "$(bold "$(yellow "Print out files *just* where we are.")")"
echo
done
###
### Question 5: Create a file
###
echo "○ $(yellow "$question5")"
echo
echo "Now, add a file."
while true; do
printf "Type the code needed to add a file \`index.txt\`: "
read -e cmd
if [[ "$cmd" == touch* ]]; then
eval "$cmd"
if [ -f "index.txt" ]; then
(( ok++ ))
intro
break
fi
fi
echo
echo "$(bold "$(yellow "Hmm, that doesn’t seem right. Try again.")")"
echo "$(bold "$(yellow "Hint: create \`index.txt\`, not another file.")")"
echo
done
###
### Question 6: Create a directory
###
echo "○ $(yellow "$question6")"
echo
echo "Next up, add a directory."
while true; do
printf "Type the code needed to add a directory \`doc\`: "
read -e cmd
if [[ "$cmd" == mkdir* ]]; then
eval "$cmd"
if [ -d "doc" ]; then
(( ok++ ))
intro
break
fi
fi
echo
echo "$(bold "$(yellow "Hmm, that doesn’t seem right. Try again.")")"
echo "$(bold "$(yellow "Hint: create \`doc\`, not another directory.")")"
echo
done
###
### Question 7: Change directories
###
currentDirectory="$(pwd)"
echo "○ $(yellow "$question7")"
echo
echo "And now, change directories."
while true; do
printf "Type the code needed to enter \`doc\`: "
read -e cmd
if [[ "$cmd" == cd* ]]; then
eval "$cmd"
if [[ "$(basename "$(pwd)")" == "doc" ]]; then
answer7="$(pwd)"
(( ok++ ))
intro
break
fi
fi
# Restore place we were at, for people that cd to a wrong place.
cd "$currentDirectory" || exit
echo
echo "$(bold "$(yellow "Hmm, that doesn’t seem right. Try again.")")"
echo "$(bold "$(yellow "Hint: Change directories.")")"
echo
done
###
### Question 8: Exit directories
###
docDirectory="$(pwd)"
echo "○ $(yellow "$question8")"
echo
echo "Next, exit that directory."
while true; do
printf "Type the code needed to exit \`doc\`: "
read -e cmd
if [[ "$cmd" == cd* ]]; then
eval "$cmd"
if [[ "$(basename "$(pwd)")" == "run" ]]; then
answer8="$(pwd)"
(( ok++ ))
intro
break
fi
fi
# Restore place we were at, for people that cd to a wrong place.
cd "$docDirectory" || exit
echo
echo "$(bold "$(yellow "Hmm, that doesn’t seem right. Try again.")")"
echo "$(bold "$(yellow "Hint: . refers to this directory, and .. to it’s parent.")")"
echo
done
###
### Question 9: Remove a file
###
echo "○ $(yellow "$question9")"
echo
echo "Now, we’re going to remove the files and directories we just created."
while true; do
echo "There are currently some files in this directory, here’s what \`ls\` produces:"
echo
echo "$(ls | sed "/^$currentScript$/d")"
echo
printf "Type the code needed to remove one of the files until everything is gone: "
read -e cmd
if [[ "$cmd" == rm* ]]; then
eval "$cmd"
if [[ "$(ls)" == "$currentScript" ]]; then
(( ok++ ))
intro
break
fi
fi
echo
echo "$(bold "$(yellow "Not yet done! (note: use \`-r\` to remove directories)")")"
echo
done
###
### Question 10: Redirect to a file
###
echo "○ $(yellow "$question10")"
echo
echo "Now, create a file with some content in it."
echo
while true; do
echo "Could you add the text 'Alpha' to a file \`foo.txt\` using the \`>\` operator?"
echo
printf "Type the code needed to echo 'Alpha' into \`foo.txt\`: "
read -e cmd
if [[ "$cmd" == echo* ]]; then
eval "$cmd"
value="$(cat foo.txt)"
if [ "$value" == "Alpha" ]; then
(( ok++ ))
intro
break
fi
fi
echo
echo "$(bold "$(yellow "Don’t forget to use echo and >.")")"
echo
done
###
### Question 11: Print a file
###
echo "○ $(yellow "$question11")"
echo
echo "Next, print a file."
echo
while true; do
printf "Type code needed to $(bold "print") out \`foo.txt\`: "
read -e cmd
if [[ "$cmd" == cat* ]]; then
answer11="$(eval "$cmd")"
if [ "$answer11" == "Alpha" ]; then
(( ok++ ))
intro
break
fi
fi
echo
echo "$(bold "$(yellow "Tip: you actually want to concatenate the file.")")"
echo
done
###
### Question 12: Move a file
###
echo "○ $(yellow "$question12")"
echo
echo "Now, rename \`foo.txt\` to \`bar.txt\` (aka $(bold "move"))?"
echo
while true; do
echo "Here’s what \`ls\` currently produces:"
echo
echo "$(ls | sed "/^$currentScript$/d")"
echo
printf "Type the code needed to $(bold "move") \`foo.txt\` to \`bar.txt\`: "
read -e cmd
if [[ "$cmd" == mv* ]]; then
eval "$cmd"
if [ -f "bar.txt" ]; then
(( ok++ ))
intro
break
fi
fi
echo
echo "$(bold "$(yellow "Tip: Moving a file is the same as renaming it.")")"
echo
done
###
### Question 13: Copy a file
###
echo "○ $(yellow "$question13")"
echo
echo "Next, could you $(bold "copy") \`bar.txt\` to \`baz.txt\`?"
echo
while true; do
echo "Here’s what \`ls\` currently produces:"
echo
echo "$(ls | sed "/^$currentScript$/d")"
echo
printf "Type the code needed to $(bold "copy") \`bar.txt\` to \`baz.txt\`: "
read -e cmd
if [[ "$cmd" == cp* ]]; then
eval "$cmd"
if [ -f "baz.txt" ]; then
(( ok++ ))
intro
break
fi
fi
echo
echo "$(bold "$(yellow "Not yet done!")")"
echo
done
###
### Question 14: Append to a file
###
echo "○ $(yellow "$question14")"
echo
echo "Previously, we added 'Alpha' to \`foo.txt\`, but then renamed it to \`bar.txt\`."
echo "Then we copied \`bar.txt\` to \`baz.txt\`. We now have two files, with the same contents."
echo
echo "For the final question, could you append 'Bravo' to \`baz.txt\`?"
while true; do
echo "Here’s what \`cat baz.txt\` currently produces:"
echo
echo "$(cat baz.txt)"
echo
printf "Type the code needed to $(bold "append") 'Bravo' to \`baz.txt\`: "
read -e cmd
if [[ "$cmd" == echo* ]]; then
eval "$cmd"
value="$(cat baz.txt | tail -n 1)"
echo "Value: .$value."
if [ "$value" == "Bravo" ]; then
(( ok++ ))
break
fi
fi
echo
echo "$(bold "$(yellow "Don’t forget to use echo and >>.")")"
echo
done
###
### Done!
###
clear
echo "$(bold "Hi, $username! 👋")"
echo "All done! You’re now ready to continue with the steps found on GitHub."
echo
echo "You'll need this code:"
echo
echo " $(bold "$code")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment