|
| 1 | +--- |
| 2 | +title: Introducing the Shell |
| 3 | +teaching: 5 |
| 4 | +exercises: 0 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::::: objectives |
| 8 | + |
| 9 | +- Explain how the shell relates to the keyboard, the screen, the operating system, and users' programs. |
| 10 | +- Explain when and why command-line interfaces should be used instead of graphical interfaces. |
| 11 | + |
| 12 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 13 | + |
| 14 | +:::::::::::::::::::::::::::::::::::::::: questions |
| 15 | + |
| 16 | +- What is a command shell and why would I use one? |
| 17 | + |
| 18 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 19 | + |
| 20 | +### Background |
| 21 | + |
| 22 | +Humans and computers commonly interact in many different ways, such as through a keyboard and mouse, |
| 23 | +touch screen interfaces, or using speech recognition systems. |
| 24 | +The most widely used way to interact with personal computers is called a |
| 25 | +**graphical user interface** (GUI). |
| 26 | +With a GUI, we give instructions by clicking a mouse and using menu-driven interactions. |
| 27 | + |
| 28 | +While the visual aid of a GUI makes it intuitive to learn, |
| 29 | +this way of delivering instructions to a computer scales very poorly. |
| 30 | +Imagine the following task: |
| 31 | +for a literature search, you have to copy the third line of one thousand text files in one thousand |
| 32 | +different directories and paste it into a single file. |
| 33 | +Using a GUI, you would not only be clicking at your desk for several hours, |
| 34 | +but you could potentially also commit an error in the process of completing this repetitive task. |
| 35 | +This is where we take advantage of the Unix shell. |
| 36 | +The Unix shell is both a **command-line interface** (CLI) and a scripting language, |
| 37 | +allowing such repetitive tasks to be done automatically and fast. |
| 38 | +With the proper commands, the shell can repeat tasks with or without some modification |
| 39 | +as many times as we want. |
| 40 | +Using the shell, the task in the literature example can be accomplished in seconds. |
| 41 | + |
| 42 | +### The Shell |
| 43 | + |
| 44 | +The shell is a program where users can type commands. |
| 45 | +With the shell, it's possible to invoke complicated programs like climate modeling software |
| 46 | +or simple commands that create an empty directory with only one line of code. |
| 47 | +The most popular Unix shell is Bash (the Bourne Again SHell --- |
| 48 | +so-called because it's derived from a shell written by Stephen Bourne). |
| 49 | +Bash is the default shell on most modern implementations of Unix and in most packages that provide |
| 50 | +Unix-like tools for Windows. |
| 51 | +Note that 'Git Bash' is a piece of software that enables Windows users to use a Bash like interface |
| 52 | +when interacting with Git. |
| 53 | + |
| 54 | +Using the shell will take some effort and some time to learn. |
| 55 | +While a GUI presents you with choices to select, CLI choices are not automatically presented to you, |
| 56 | +so you must learn a few commands like new vocabulary in a language you're studying. |
| 57 | +However, unlike a spoken language, a small number of "words" (i.e. commands) gets you a long way, |
| 58 | +and we'll cover those essential few today. |
| 59 | + |
| 60 | +The grammar of a shell allows you to combine existing tools into powerful |
| 61 | +pipelines and handle large volumes of data automatically. Sequences of |
| 62 | +commands can be written into a *script*, improving the reproducibility of |
| 63 | +workflows. |
| 64 | + |
| 65 | +In addition, the command line is often the easiest way to interact with remote machines |
| 66 | +and supercomputers. |
| 67 | +Familiarity with the shell is near essential to run a variety of specialized tools and resources |
| 68 | +including high-performance computing systems. |
| 69 | +As clusters and cloud computing systems become more popular for scientific data crunching, |
| 70 | +being able to interact with the shell is becoming a necessary skill. |
| 71 | +We can build on the command-line skills covered here |
| 72 | +to tackle a wide range of scientific questions and computational challenges. |
| 73 | + |
| 74 | +Let's get started. |
| 75 | + |
| 76 | +When the shell is first opened, you are presented with a **prompt**, |
| 77 | +indicating that the shell is waiting for input. |
| 78 | + |
| 79 | +```bash |
| 80 | +$ |
| 81 | +``` |
| 82 | + |
| 83 | +The shell typically uses `$ ` as the prompt, but may use a different symbol. |
| 84 | +In the examples for this lesson, we'll show the prompt as `$ `. |
| 85 | +Most importantly, *do not type the prompt* when typing commands. |
| 86 | +Only type the command that follows the prompt. |
| 87 | +This rule applies both in these lessons and in lessons from other sources. |
| 88 | +Also note that after you type a command, you have to press the <kbd>Enter</kbd> key to execute it. |
| 89 | + |
| 90 | +The prompt is followed by a **text cursor**, a character that indicates the position where your |
| 91 | +typing will appear. |
| 92 | +The cursor is usually a flashing or solid block, but it can also be an underscore or a pipe. |
| 93 | +You may have seen it in a text editor program, for example. |
| 94 | + |
| 95 | +Note that your prompt might look a little different. In particular, most popular shell |
| 96 | +environments by default put your user name and the host name before the `$`. Such |
| 97 | +a prompt might look like, e.g.: |
| 98 | + |
| 99 | +```bash |
| 100 | +nelle@localhost $ |
| 101 | +``` |
| 102 | + |
| 103 | +The prompt might even include more than this. Do not worry if your prompt is not |
| 104 | +just a short `$ `. This lesson does not depend on this additional information and it |
| 105 | +should also not get in your way. The only important item to focus on is the `$ ` |
| 106 | +character itself and we will see later why. |
| 107 | + |
| 108 | +So let's try our first command, `ls`, which is short for listing. |
| 109 | +This command will list the contents of the current directory: |
| 110 | + |
| 111 | +```bash |
| 112 | +$ ls |
| 113 | +``` |
| 114 | + |
| 115 | +```output |
| 116 | +Desktop Downloads Movies Pictures |
| 117 | +Documents Library Music Public |
| 118 | +``` |
| 119 | + |
| 120 | +::::::::::::::::::::::::::::::::::::::::: callout |
| 121 | + |
| 122 | +## Command not found |
| 123 | + |
| 124 | +If the shell can't find a program whose name is the command you typed, it |
| 125 | +will print an error message such as: |
| 126 | + |
| 127 | +```bash |
| 128 | +$ ks |
| 129 | +``` |
| 130 | + |
| 131 | +```output |
| 132 | +ks: command not found |
| 133 | +``` |
| 134 | + |
| 135 | +This might happen if the command was mis-typed or if the program corresponding to that command |
| 136 | +is not installed. |
| 137 | + |
| 138 | + |
| 139 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 140 | + |
| 141 | +## Nelle's Pipeline: A Typical Problem |
| 142 | + |
| 143 | +Nelle Nemo, a marine biologist, |
| 144 | +has just returned from a six-month survey of the |
| 145 | +[North Pacific Gyre](https://en.wikipedia.org/wiki/North_Pacific_Gyre), |
| 146 | +where she has been sampling gelatinous marine life in the |
| 147 | +[Great Pacific Garbage Patch](https://en.wikipedia.org/wiki/Great_Pacific_Garbage_Patch). |
| 148 | +She has 1520 samples that she's run through an assay machine to measure the relative abundance |
| 149 | +of 300 proteins. |
| 150 | +She needs to run these 1520 files through an imaginary program called `goostats.sh`. |
| 151 | +In addition to this huge task, she has to write up results by the end of the month, so her paper |
| 152 | +can appear in a special issue of *Aquatic Goo Letters*. |
| 153 | + |
| 154 | +If Nelle chooses to run `goostats.sh` by hand using a GUI, |
| 155 | +she'll have to select and open a file 1520 times. |
| 156 | +If `goostats.sh` takes 30 seconds to run each file, the whole process will take more than 12 hours |
| 157 | +of Nelle's attention. |
| 158 | +With the shell, Nelle can instead assign her computer this mundane task while she focuses |
| 159 | +her attention on writing her paper. |
| 160 | + |
| 161 | +The next few lessons will explore the ways Nelle can achieve this. |
| 162 | +More specifically, |
| 163 | +the lessons explain how she can use a command shell to run the `goostats.sh` program, |
| 164 | +using loops to automate the repetitive steps of entering file names, |
| 165 | +so that her computer can work while she writes her paper. |
| 166 | + |
| 167 | +As a bonus, |
| 168 | +once she has put a processing pipeline together, |
| 169 | +she will be able to use it again whenever she collects more data. |
| 170 | + |
| 171 | +In order to achieve her task, Nelle needs to know how to: |
| 172 | + |
| 173 | +- navigate to a file/directory |
| 174 | +- create a file/directory |
| 175 | +- check the length of a file |
| 176 | +- chain commands together |
| 177 | +- retrieve a set of files |
| 178 | +- iterate over files |
| 179 | +- run a shell script containing her pipeline |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 184 | + |
| 185 | +- A shell is a program whose primary purpose is to read commands and run other programs. |
| 186 | +- This lesson uses Bash, the default shell in many implementations of Unix. |
| 187 | +- Programs can be run in Bash by entering commands at the command-line prompt. |
| 188 | +- The shell's main advantages are its high action-to-keystroke ratio, its support for automating repetitive tasks, and its capacity to access networked machines. |
| 189 | +- A significant challenge when using the shell can be knowing what commands need to be run and how to run them. |
| 190 | + |
| 191 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 192 | + |
| 193 | + |
0 commit comments