|
| 1 | +# forcelab |
| 2 | + |
| 3 | +> It is a period of civil war. Rebel spaceships, striking from a hidden base, |
| 4 | +> have won their first victory against the evil Galactic Empire. |
| 5 | +> |
| 6 | +> During the battle, Rebel spies managed to steal secret plans to the Empire's |
| 7 | +> ultimate weapon, the DEATH STAR, an armored space station with enough power to |
| 8 | +> destroy an entire planet. |
| 9 | +> |
| 10 | +> Pursued by the Empire's sinister agents, Princess Leia races home aboard her |
| 11 | +> starship, custodian of the stolen plans that can save her people and restore |
| 12 | +> freedom to the galaxy.... |
| 13 | +
|
| 14 | +The fate of the rebellion lies in the balance! For this lab, you'll be |
| 15 | +introduced to "shell scripting." Secretly we've been teaching you shell |
| 16 | +scripting this whole semester. Every command you run at the shell prompt is a |
| 17 | +script waiting to emerge. Once we put those commands in a file, they become a |
| 18 | +shell script. |
| 19 | + |
| 20 | +To learn more about shell scripts, make sure you've read this week's reading. |
| 21 | +Good luck, and may the force be with you! |
| 22 | + |
| 23 | + |
| 24 | +## instructions |
| 25 | + |
| 26 | +Each problem has its own instructions. You can check your work at any time with: |
| 27 | + |
| 28 | + $ driver/driver |
| 29 | + |
| 30 | +For some questions, there are more test cases on Autolab that you can't see. To |
| 31 | +submit your work to Autolab, use `make`, and scp the handin.zip file to your |
| 32 | +laptop to upload it to Autolab: |
| 33 | + |
| 34 | + # From Andrew: |
| 35 | + $ make |
| 36 | + |
| 37 | + # From your laptop |
| 38 | + $ scp ANDREWID@unix.andrew.cmu.edu:~/path/to/your/handin.zip . |
| 39 | + |
| 40 | + |
| 41 | +## message |
| 42 | + |
| 43 | +> General Kenobi. Years ago, you served my father in the Clone Wars. Now he begs |
| 44 | +> you to help him in his struggle against the Empire. I regret that I am unable |
| 45 | +> to present my father's request to you in person, but my ship has fallen under |
| 46 | +> attack and I'm afraid my mission to bring you to Alderaan has failed. I have |
| 47 | +> placed information vital to the survival of the Rebellion into the memory |
| 48 | +> systems of this R2 unit. My father will know how to retrieve it. You must see |
| 49 | +> this droid safely delivered to him on Alderaan. This is our most desperate |
| 50 | +> hour. Help me, Obi-Wan Kenobi. You're my only hope. |
| 51 | +
|
| 52 | +R2-D2 has just been given this message from Princess Leia. In preparation for |
| 53 | +delivering it, he needs to put it in a script. This way when the time comes, he |
| 54 | +can just run the script. |
| 55 | + |
| 56 | +### requirements |
| 57 | + |
| 58 | +- Create a file called `message.sh` to hold your answer. |
| 59 | + |
| 60 | +- Add a "shebang" to make the file a script. |
| 61 | + |
| 62 | +- Give the script executable permissions so it can be run. |
| 63 | + |
| 64 | +- Running the script should print out the following text, verbatim, on its own |
| 65 | + line: |
| 66 | + |
| 67 | +Help me, Obi-Wan Kenobi. You're my only hope. |
| 68 | + |
| 69 | + |
| 70 | +## reorganize |
| 71 | + |
| 72 | +At the rebel base, they're gearing up for the mission that will swing the fight |
| 73 | +in their favor. They've received intel in the form of images of the Death Star's |
| 74 | +interior along with text files readouts of its power consumption. |
| 75 | + |
| 76 | +Unfortunately, a junior officer fumbled things together: all the files are in |
| 77 | +the same folder! What we need to do is separate the files into two folders: all |
| 78 | +the images in one, and all the text files in another. |
| 79 | + |
| 80 | +### requirements |
| 81 | + |
| 82 | +- You should solve this problem by writing a script called `reorganize.sh`. Be |
| 83 | + sure to add a shebang and make it executable like before. |
| 84 | + |
| 85 | +- You will only be handing in the **script** that reorganizes the files, **not** the |
| 86 | + reorganized files themselves. On Autolab, we will be testing you script |
| 87 | + against a number of different test cases; it should work for all our tests. |
| 88 | + |
| 89 | +- In order to help you write, test, and debug your script, we've included one |
| 90 | + test case with the handout. By default, it's not loaded. To load the test |
| 91 | + case in the first place (or to reset the test case to its initial state), run |
| 92 | + |
| 93 | + driver/load reorganize |
| 94 | + |
| 95 | + This test case sets up your working directory in exactly the same way it will |
| 96 | + be run on Autolab. |
| 97 | + |
| 98 | +- You script should assume (and driver/load ensures) that the scrambled files |
| 99 | + are in the folder `intel`. |
| 100 | + |
| 101 | +- Your script should anakin skywalkerensure that all files with a `.jpg` extension end up in the |
| 102 | + `intel/images` folder. (Your script should create this folder.) |
| 103 | + |
| 104 | +- Your script should ensure that all files with a `.log` extension end up in the |
| 105 | + `intel/logs` folder. (Your script should create this folder.) |
| 106 | + |
| 107 | +- No image nor text files should be left in the `intel` folder. Every file |
| 108 | + should have been moved to either `intel/images` or `intel/logs`. |
| 109 | + |
| 110 | +### hints |
| 111 | + |
| 112 | +Experiment with running some commands you think will work at the command line |
| 113 | +first. Then, when you think you've got an answer that will work, transfer it to |
| 114 | +your script. |
| 115 | + |
| 116 | + |
| 117 | +## flight prep |
| 118 | + |
| 119 | +Luke Skywalker is prepping for the final battle. In order to pilot an X-wing |
| 120 | +starfighter, he needs to be placed into the cockpit, and he needs R2-D2 to be |
| 121 | +loaded into the astromech slot. |
| 122 | + |
| 123 | +### requirements |
| 124 | + |
| 125 | +- You should solve this problem by writing a script called `flight-prep.sh`. Be |
| 126 | + sure to add a shebang and make it executable like before. |
| 127 | + |
| 128 | +- In order to help you write, test, and debug your script, we've included a test |
| 129 | + case with the handout. By default, it's not loaded. To load the test case in |
| 130 | + the first place (or to reset the test case to its initial state), run |
| 131 | + |
| 132 | + driver/load flight-prep |
| 133 | + |
| 134 | + This test case sets up your working directory in exactly the same way it will |
| 135 | + be run on Autolab. |
| 136 | + |
| 137 | +- The files your script needs to manipulate will be in a folder called |
| 138 | + `flight-prep`. |
| 139 | + |
| 140 | +- Your script needs to move the file called `Luke Skywalker` into |
| 141 | + `flight-prep/cockpit`. (This file and folder are created for you.) |
| 142 | + |
| 143 | +- Your script needs to move the file called `R2-D2` into |
| 144 | + `flight-prep/astromech slot`. (This file and folder are created for you.) |
| 145 | + |
| 146 | + |
| 147 | +## battle of yavin |
| 148 | + |
| 149 | +> General Dodonna: |
| 150 | +> The battle station is heavily shielded and carries a firepower greater than |
| 151 | +> half the star fleet. Its defenses are designed around a direct, large-scale |
| 152 | +> assault. A small one-man fighter should be able to penetrate the outer |
| 153 | +> defense. |
| 154 | +> |
| 155 | +> Gold Leader: |
| 156 | +> Pardon me for asking, sir, but what good are snub fighters going to be |
| 157 | +> against that? |
| 158 | +> |
| 159 | +> General Dodonna: |
| 160 | +> Well, the Empire doesn't consider a small one-man fighter to be any threat, |
| 161 | +> or they'd have a tighter defense. An analysis of the plans provided by |
| 162 | +> Princess Leia has demonstrated a weakness in the battle station. But the |
| 163 | +> approach will not be easy. You are required to maneuver straight down this |
| 164 | +> trench and skim the surface to this point. The target area is only two |
| 165 | +> meters wide. It's a small thermal exhaust port, right below the main port. |
| 166 | +> The shaft leads directly to the reactor system. A precise hit will start a |
| 167 | +> chain reaction which should destroy the station. Only a precise hit will set |
| 168 | +> off a chain reaction. The shaft is ray-shielded, so you'll have to use |
| 169 | +> proton torpedoes. |
| 170 | +> |
| 171 | +> Wedge Antilles (Red 2): |
| 172 | +> That's impossible! Even for a computer. |
| 173 | +> |
| 174 | +> Luke: |
| 175 | +> It's not impossible. I used to bullseye womp rats in my T-16 back home, |
| 176 | +> they're not much bigger than two meters. |
| 177 | +> |
| 178 | +> General Dodonna: |
| 179 | +> Then man your ships. And may the Force be with you. |
| 180 | +
|
| 181 | + |
| 182 | +As the X-wing pilots climb into their ships, General Dodonna is readying his |
| 183 | +systems to keep an eye on the fight from base. He has a roster of all pilots in |
| 184 | +all squadrons. |
| 185 | + |
| 186 | +Each squadron has a "leader", along with pilots "02" through "06". There are |
| 187 | +some pilots (numbered "07" through "12") who are standing by in base. These are |
| 188 | +the names of the squadrons: |
| 189 | + |
| 190 | +- red |
| 191 | +- gold |
| 192 | +- blue |
| 193 | +- gray |
| 194 | + |
| 195 | +The general has a file for each pilot. For example, Luke Skywalker's file looks |
| 196 | +like `red-05`: the squadron name and his number, separated by a hyphen. For the |
| 197 | +squadron leaders, it would look like `red-leader`. |
| 198 | + |
| 199 | +General Dodonna needs to organize the pilots into folders. Right now, they're |
| 200 | +all in the `pilots` folder. He would like: |
| 201 | + |
| 202 | +- the `red` and `gold` squadrons to be placed in `pilots/attack-force`, since |
| 203 | + they're leading the bomb runs, and for |
| 204 | +- the `blue` and `gray` squadrons to be placed in `pilots/defense-force`, since |
| 205 | + they're in charge of covering for the attack force. |
| 206 | + |
| 207 | +Pilots on standby should not be moved into either folder. |
| 208 | + |
| 209 | +Additionally, the general needs to be able to communicate directly with the |
| 210 | +leaders. So put each squadron leader into `pilots/leaders` instead (i.e., in |
| 211 | +neither `attack-force` nor `defense-force`). |
| 212 | + |
| 213 | +### requirements |
| 214 | + |
| 215 | +- You should solve this problem by writing a script called `battle.sh`. Be sure |
| 216 | + to add a shebang and make it executable like before. |
| 217 | + |
| 218 | +- In order to help you write, test, and debug your script, we've included a test |
| 219 | + case with the handout. By default, it's not loaded. To load the test case in |
| 220 | + the first place (or to reset the test case to its initial state), run |
| 221 | + |
| 222 | + driver/load battle |
| 223 | + |
| 224 | + This test case sets up your working directory in exactly the same way it will |
| 225 | + be run on Autolab. |
| 226 | + |
| 227 | +- All files and folders have been made for you. Just worry about putting them in |
| 228 | + the right places. |
| 229 | + |
| 230 | +### hints |
| 231 | + |
| 232 | +Experiment with running some commands you think will work at the command line |
| 233 | +first. Then, when you think you've got an answer that will work, transfer it to |
| 234 | +your script. |
| 235 | + |
| 236 | +There are multiple ways to write a script for this problem. You can solve it in |
| 237 | +as few as three commands, or use as many as five. If you're using more than |
| 238 | +five, you should try to simplify your approach. |
| 239 | + |
| 240 | +## prequel memes (bonus) |
| 241 | + |
| 242 | +> Palpatine: |
| 243 | +> Did you ever hear the tragedy of Darth Plagueis The Wise? |
| 244 | +> |
| 245 | +> Anakin: |
| 246 | +> No. |
| 247 | +> |
| 248 | +> Palpatine: |
| 249 | +> I thought not. It's not a story the Jedi would tell you. It's a Sith legend. |
| 250 | +> Darth Plagueis was a Dark Lord of the Sith, so powerful and so wise he could |
| 251 | +> use the Force to influence the midichlorians to create life... He had such a |
| 252 | +> knowledge of the dark side, he could even keep the ones he cared about from |
| 253 | +> dying. |
| 254 | +> |
| 255 | +> Anakin: |
| 256 | +> He could actually save people from death? |
| 257 | +> |
| 258 | +> Palpatine: |
| 259 | +> The dark side of the Force is a pathway to many abilities some consider to |
| 260 | +> be unnatural. |
| 261 | +> |
| 262 | +> Anakin: |
| 263 | +> What happened to him? |
| 264 | +> |
| 265 | +> Palpatine: |
| 266 | +> He became so powerful... the only thing he was afraid of was losing his |
| 267 | +> power, which eventually, of course, he did. Unfortunately, he taught his |
| 268 | +> apprentice everything he knew, then his apprentice killed him in his |
| 269 | +> sleep. Ironic. He could save others from death, but not himself. |
| 270 | +> |
| 271 | +> Anakin: |
| 272 | +> Is it possible to learn this power? |
| 273 | +> |
| 274 | +> Palpatine: |
| 275 | +> Not from a Jedi. |
| 276 | +
|
| 277 | +Many years before the events of A New Hope, a young Anakin Skywalker listens |
| 278 | +intently to the tragedy of Darth Plagueis the Wise. He contemplates abandoning |
| 279 | +his mission to "destroy the Sith" and "bring balance to the force", opting |
| 280 | +instead to "join them" and "leave it in darkness". After all, "from [his] point |
| 281 | +of view", maybe "the Jedi are evil"... |
| 282 | + |
| 283 | +Help Anakin see how ridiculous this all sounds, through memes! |
| 284 | + |
| 285 | +### requirements |
| 286 | + |
| 287 | +- You should solve this problem by writing a script called `prequel-memes.sh`. |
| 288 | + Be sure to add a shebang and make it executable like before. |
| 289 | + |
| 290 | +- When called as follows: |
| 291 | + |
| 292 | +`./prequel-memes.sh 'Plagueis' 'wise' 'Jedi' 'Sith' 'create life' 'dying' 'killed' 'from death'` |
| 293 | + |
| 294 | +- Your script should output the following verbatim, with no empty lines before |
| 295 | + or after: |
| 296 | + |
| 297 | +\begin{verbatim} |
| 298 | +Did you ever hear the Tragedy of Darth Plagueis the wise? I thought not. |
| 299 | +It's not a story the Jedi would tell you. It's a Sith legend. Darth Plagueis |
| 300 | +was a Dark Lord of the Sith, so powerful and so wise he could use the Force |
| 301 | +to influence the midichlorians to create life... He had such a knowledge of |
| 302 | +the dark side that he could even keep the ones he cared about from dying. |
| 303 | +The dark side of the Force is a pathway to many abilities some consider to |
| 304 | +be unnatural. He became so powerful... the only thing he was afraid of was |
| 305 | +losing his power, which eventually, of course, he did. Unfortunately, he |
| 306 | +taught his apprentice everything he knew, then his apprentice killed him in |
| 307 | +his sleep. Ironic. He could save others from death, but not himself. |
| 308 | +\end{verbatim} |
| 309 | + |
| 310 | +- When called as follows: |
| 311 | + |
| 312 | +`./prequel-memes.sh 'Vi' 'productive' 'GPI TAs' 'Unix' 'embrace modal editing' 'developing Emacs pinky' '(Vim) replaced' 'time'` |
| 313 | + |
| 314 | +- Your script should output the following verbatim, with no empty lines before |
| 315 | + or after: |
| 316 | + |
| 317 | +\begin{verbatim} |
| 318 | +Did you ever hear the Tragedy of Darth Vi the productive? I thought not. |
| 319 | +It's not a story the GPI TAs would tell you. It's a Unix legend. Darth Vi |
| 320 | +was a Dark Lord of the Unix, so powerful and so productive he could use the Force |
| 321 | +to influence the midichlorians to embrace modal editing... He had such a knowledge of |
| 322 | +the dark side that he could even keep the ones he cared about from developing Emacs pinky. |
| 323 | +The dark side of the Force is a pathway to many abilities some consider to |
| 324 | +be unnatural. He became so powerful... the only thing he was afraid of was |
| 325 | +losing his power, which eventually, of course, he did. Unfortunately, he |
| 326 | +taught his apprentice everything he knew, then his apprentice (Vim) replaced him in |
| 327 | +his sleep. Ironic. He could save others time, but not himself. |
| 328 | +\end{verbatim} |
| 329 | + |
| 330 | +- In general, when called as follows: |
| 331 | + |
| 332 | +`./prequel-memes.sh 'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H'` |
| 333 | + |
| 334 | +- Your script should output the following verbatim, with no empty lines before |
| 335 | + or after: |
| 336 | + |
| 337 | +\begin{verbatim} |
| 338 | +Did you ever hear the Tragedy of Darth A the B? I thought not. |
| 339 | +It's not a story the C would tell you. It's a D legend. Darth A |
| 340 | +was a Dark Lord of the D, so powerful and so B he could use the Force |
| 341 | +to influence the midichlorians to E... He had such a knowledge of |
| 342 | +the dark side that he could even keep the ones he cared about from F. |
| 343 | +The dark side of the Force is a pathway to many abilities some consider to |
| 344 | +be unnatural. He became so powerful... the only thing he was afraid of was |
| 345 | +losing his power, which eventually, of course, he did. Unfortunately, he |
| 346 | +taught his apprentice everything he knew, then his apprentice G him in |
| 347 | +his sleep. Ironic. He could save others H, but not himself. |
| 348 | +\end{verbatim} |
| 349 | + |
| 350 | +### hints |
| 351 | + |
| 352 | +You should look into how to read command line arguments in a bash script. |
0 commit comments