# Homework 2 Penguin parents Roy and Silo want their daughter, Tango, to get a jump start on her computer science education by learning how to use bash. Roy and Silo have already learned the basics of navigation from their friend Tux, but they need to learn more about bash scripting before they feel confident about teaching Tango! For the problems and problem parts below, write up solutions marked ***Gradescope*** in a .md file and submit them on Gradescope in a .pdf. For problems marked ***cs6-work***, complete the scripts through `cs6-work hw2` and type `exit` to leave the environment and save your work to our backup folders. We will pull your solutions at the due date. You may hand in up to 24 hours late for a 25% deduction in grade. To do so, hand in to Gradescope as per usual. If you wish to turn in your scripts late, use `cs6-work hw2` and `exit` again in the following day. If your files for hw2 are different than they were at the original deadline, we will grade your folder as late. *Side note -- if you accidentally forget to type the exit command and instead close the terminal tab, your work is not lost! You will be able to access it again upon login. We just won't have it in our copy of your folder. Think of `exit` as a handin checkpoint.* *Another side note -- if you accidentally break something or delete one of the files we provided you with, you can find it the cs6-work environment :smile: cd to `~/backups` and copy whichever files you lost/corrupted.* *Yet another side note -- if you like syntax highlighting whilst coding, you can turn on vim's syntax highlighting by following the directions [here](https://mediatemple.net/community/products/dv/204644480/enabling-vi-syntax-colors).* Remember to add `#!/bin/bash` (aka "shebang") as the first line for all the bash scripts you make! This tells the computer which language your program is. ## Problem 1: File Descriptors Roy is confused about the difference between files and file descriptors and why file or output redirection might be helpful. Help him learn this sorcery by answering his questions! We have created files for you in the `~/hw2/problem1` directory in the CS6 environment. 1. ***cs6-work:*** Recall the methods that you can use to pass input to a bash program and think about how data streams and file descriptors relate to input. Write 3 scripts that can add exactly two integers using each of the following input methods listed below. Don't forget to include the shebang line at the beginning of your scripts! These scripts are called `add1.sh`, `add2.sh`, and `add3.sh`, respectively. a. Positional arguments: ```bash $ ./add1.sh 1 2 3 ``` b. `stdin` (hint: `read` and stdin redirection may be helpful): ```bash $ echo -e "1\n2" | ./add2.sh 3 ``` c. User interaction (`read`): see below (`$` is prompt) ```bash $ ./add3.sh type first number: 1 type second number: 2 3 ``` 2. ***Gradescope:*** Using an analogy of your choosing, describe what `stdin`, `stdout`, and `stderr` are and how they are (typically) used. 3. ***Gradescope:*** For each of the following commands, thoroughly explain what is happening and how `stdin`, `stdout`, and `stderr` are (or are not) involved. Assume that nothing fishy is going on with standard bash commands and that none of the commands affect each other. Feel free to use `man` and run these and experiment on your own! Note: `says`, `meow`, `pet`, `the`, `oopsies` are all file names a. `echo 'location'` b. `cat says > meow` (all files here do not exist) c. `cat says 2> meow` (all files here do not exist) d. `cat says 2>&1` (all files here do not exist) e. `< pet > the cat` (all files here exist) f. `ls -al | wc -l 2>> oopsies` (all files here exist) g. `yes | head | wc -l` h. `yes | wc -l` ## Problem 2: Conditions 1. ***Gradescope:*** `type [` echoes that `[ is a shell builtin` while `type [[` outputs `[[ is a shell keyword`. How does this demonstrate the difference between `[` and `[[`? For `[` and `[[`, give each a concrete usage example. 2. ***Gradescope:*** Describe what each of the following conditions/expressions do and evaluate to (be sure to mention exit codes). Assume that all variables are properly defined and typed: \ a. `[[ a = a && b = a ]]` \ b. `[ 1 < 3 ]` \ c. `[[ -e $file2 && $file1 -ot $file2 ]]` ## Problem 3: Conditional Statements ***cs6-work:*** Roy and Silo often play [20 questions](https://en.wikipedia.org/wiki/Twenty_Questions) with Tango in their free time. Now that they've learned a bit more about bash, they're wondering if they can write a script that can play the game with their daughter when both of them are busy! Help the busy fathers write a script that plays the role of 'questioner' and can guess out of **between 8 to 12 options** of your choice within 5 questions (this means each question needs to divide the possibility space approximately in half). Drawing a binary tree might be helpful. Write your script in `~/hw2/problem2/20questions.sh`. A complete list of things that your script can guess should be printed at the beginning of script execution. Users should be able to respond by typing responses 'yes' or 'no' and pressing the return key (`read` may be helpful here). Example of gameplay (`$` represents the prompt, `>` represents the read prompt): ``` $ ./20questions.sh Hello! Think of one of the following animals: - cat - dog - penguin - snake - ostrich - dolphin - parrot - bat Does your animal live exclusively on land? > no Can your animal swim? > yes Does your animal have wings? > yes Is your animal a penguin? > yes Hooray! ``` Get creative with your script! Remember that you are limited to **just yes and no questions**. ## Problem 4: 'Quarrelsome' "Quotes" ***Gradescope:*** Fix the following commands by replacing incorrectly used quotes! For each command, write the correct version and briefly explain how it fixes the original. Some of these commands may or may not already be correct. Quotes include the `"`, `'`, and `` ` `` characters. Feel free to try them out in your terminal :smile: 1. `echo 'let'"'"'s think about this one!'` 2. `mv 'suspicious file.txt' '\"trustworthy\" file.txt'` 3. `echo "Hello, world!!"` 4. `echo the first line of file.txt is "cat file.txt | head -1"` (Note: The intent of this line is for the cat to be executed and the result inserted into the string.) 5. ``echo 'here's my current directory: `pwd`' > 'home directory.txt'`` (Note: The intent of this line is for the file "home directory.txt" to contain the entire message being echoed, along with the working dir.) ## Fun Stuff The things under this section are strictly for fun (i.e. do not turn this stuff in)! Happy bashing! (づ。◕‿‿◕。)づ ### Star Wars Stuff You can watch Star Wars in ascii format using `telnet` (you can install this on your own computer)! Just run the following command: ``` telnet towel.blinkenlights.nl ```