Update shell_cheatsheet.md
[swc-modular-shell.git] / shell_cheatsheet.md
index df2185534ff8ffa091b6d4f7d4ce64204107d9b9..95b7bf351ca4c24f4034137cea01a1185f218928 100644 (file)
@@ -60,73 +60,118 @@ text strings as arguments.
 See the cheatsheet on regular expressions for more "wildcard" shortcuts.
 
 ### b) How to redirect to a file and get input from a file ...
+Redirection operators can be used to redirect the ouput from a program from the display screen to a file where it is saved (or many other places too, like your printer or to another program where it can be used as input).
 
-#### Definitions:
-*  **stdout**:
-*  **stdin**:
-*  **stderr**:
 
-| Command | Description |  
-|---------|-------------|
-| `>`     |
-| `>>`    |
-| `<`     |
-| `<<`    |
+| Command | Description                                                                                                                     |  
+|---------|---------------------------------------------------------------------------------------------------------------------------------|  
+| `>`     | write `stdout` to a new file; overwrites any file with that name (e.g., `ls *.md > mardkownfiles.txt`)                          |  
+| `>>`    | append `stdout` to a previously existing file; if the file does not exist, it is created (e.g., `ls *.md >> markdownfiles.txt`) |  
+| `<`     | assigns the information in a file to a variable, loop, etc (e.g., `n < markdownfiles.md`)                                       | 
 
-### e) How to use the output of one command as the input to another with a pipe...
-### f) That combining single-purpose filters with pipes is the most productive way to use the shell...
-### g) That if a program conforms to Unix conventions, it can easily be combined with others...
 
 
+#### b.1) How to use the output of one command as the input to another with a pipe...
+A special kind of redirection is called a pipe and is denoted by `|`. 
 
-## 4. Variables
-### a) Assignment
-* **`varname=1`** -->
 
-### b) Indexing 
-* **`varname[0]`** --> _Note:_ the shell is zero indexed.  That means you always start counting from zero
+| Command | Description                                                                                                                                           |  
+|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------|  
+| `|`     | Output from one command line program can be used as input to another one (e.g. `ls *.md | head` gives you the first 5 `*.md` files in your directory) |  
+
+
+
+
+##### Example:   
+
+    ls *.md | head | sed -i `s/markdown/software/g`
+   
+changes all the instances of the word `markdown` to `software` in the first 5 `*.md` files in your current directory.
 
-### c) Referencing
-* **`${varname}` -->
-* **`${varname[@]` --> 
 
  
 
-## 5. Loops
-NEED TO DO VARIABLE ASSIGNMENT FIRST!!!!
-### a) How to repeat operations using a loop...
-* **`for`** -->  
-    `for filename in *.dat
+## 4. How to repeat operations using a loop...
+Loops assign a value in a list or counter to a variable that takes on a different value each time through the loop.
+There are 2 primary kinds of loops: `for` loops and `while` loops.
+
+### a) For loop
+For loops loop through variables in a list
+
+
+    for varname in list
     do
-      mv ${filename} ${newname}
-    done`
+        command 1
+        command 2
+    done
+
+where,
+
+*  `for`, `in`, `do`, and `done` are keywords
+*  `list` contains a list of values separated by spaces. e.g. `list` can be replaced by `1 2 3 4 5 6` or by `Bob Mary Sue Greg`. `list` can also be a variable:
+
+--
+
+    list[0]=Sam
+    list[1]=Lynne
+    list[2]=Dhavide
+    list[3]=Trevor
+    .
+    .
+    .
+    list[n]=Mark
     
-* **`while`** -->
-    `count=0   
-     while ${count} -lte 6
-     do
-       COMMAND HERE
-     done`
+which is referenced in the loop by:
 
-### b) That the loop variable takes on a different value each time through the loop...
-### c) The difference between a variable's name and its value...
-### d) Why spaces and some punctuation characters shouldn't be used in files' names...
-### e) How to display history and re-use commands...
-* **`history`** --> displays your command history to the standard output (usually the screen)
+    for varname in ${list[@]}
+    do
+        command 1
+        command 2
+    done
 
 
+_Note:_ Bash is zero indexed, so counting always starts at `0`, not `1`.
+    
+
+### b) While Loop
+While loops loop through the commands until a condition is met. For example
+    
+    COUNTER=0
+    while [ ${COUNTER} -lt 10 ]; do
+        command 1
+        command 2
+        COUNTER=`expr ${COUNTER} + 1` 
+    done
+
+continues the loop as long as the value in the variable COUNTER is less than 10 (incremented by 1 on each iteration of the loop).
+
+*  `while`, `do`, and `done` are keywords
+
 
-## 6. Shell Scripts
-### a) How to store shell commands in a file...
-### b) How to run a shell script...
-### c) How to pass filenames into a shell script...
+#### b.1) Commonly used conditional operators
 
+| Operator | Definition               |  
+|----------|--------------------------|  
+| `-eq`    | is equal to              |  
+| `-ne`    | is not equal to          |  
+| `-gt`    | greater than             |
+| `-ge`    | greater than or equal to |
+| `-lt`    | less than                |
+| `-le`    | less than or equal to    |
 
 
-## 7. Finding Things
+
+
+
+## 6. Finding Things
 ### a) How to select lines matching patterns in text files...
-* **`grep [options] day haiku.txt`** --> finds every instance of the string `day` in the file haiku.txt and pipes it to standard output. 
-       * **`-E`** --> tells grep you will be using a regular expression. Enclose the regular expression in quotes. _Note:_ the power of `grep` comes from using regular expressions. Please see the regular expressions sheet for examples
+
+| Example command | Description |
+|-----------------|-------------|
+| `grep [options] day haiku.txt` | finds every instance of the string `day` in the file haiku.txt and pipes it to standard output |  
+| *  `-E`                  | tells grep you will be using a regular expression. Enclose the regular expression in quotes. _Note:_ the power of `grep` comes from using regular expressions. Please see the regular expressions sheet for examples |  
+
+
        * **`-i`** --> makes matching case-insensitive
        * **`-n`** --> limits the number of lines that match to the first n matches
        * **`-v`** --> shows lines that do not match the pattern (inverts the match)