X-Git-Url: http://git.tremily.us/?a=blobdiff_plain;f=shell_cheatsheet.md;h=aab5063431e83cbca68e06bdcee76bb1919fc9f2;hb=9c2c6aba1fb3dee39db949b3a991a6b8d2122926;hp=5c8f1f3551951a788cdd628933fae187e7a90c3f;hpb=db7c910b15ee1b3b979f5ac531434a882f08af83;p=swc-modular-shell-hearing.git diff --git a/shell_cheatsheet.md b/shell_cheatsheet.md index 5c8f1f3..aab5063 100644 --- a/shell_cheatsheet.md +++ b/shell_cheatsheet.md @@ -74,81 +74,119 @@ Redirection operators can be used to redirect the ouput from a program from the #### 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 `|`. + | 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) | -| | | -* Combining single-purpose filters with pipes is the most productive way to use the shell... - * a program conforms that to Unix conventions, it can easily be combined with others... -Example: + + +##### 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. -## 4. Variables -### a) Assignment -* **`varname=1`** --> + -### b) Indexing -* **`varname[0]`** --> _Note:_ the shell is zero indexed. That means you always start counting from zero +## 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. -### c) Referencing -* **`${varname}` --> -* **`${varname[@]` --> +### a) For loop +For loops loop through variables in a list - -## 5. Loops -NEED TO DO VARIABLE ASSIGNMENT FIRST!!!! -### a) How to repeat operations using a loop... -* **`for`** --> - `for filename in *.dat + 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`. + -## 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) 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 -## 7. Finding Things + +#### 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 | + + + + + +## 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 - * **`-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) - * **`-w`** --> outputs instances where the pattern is a whole word +To find information within files, you use a command called `grep`. +| 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 | | + +#### a.1) Commonly used `grep` options + +| | `grep` options | +|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `-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) | +| `-w` | outputs instances where the pattern is a whole word | ### b) How to find files with certain properties... +To find file and directory names, you use a command called `find` + + * **`find . -type d` --> * **`-type [df]`** --> d lists directories; f lists files * **`-maxdepth n`** --> `find` automatically searches subdirectories. If you don't want that, specify the number of levels below the working directory you would like to search * **`-mindepth n`** --> starts `find`'s search n levels below the working directory - -### c) How to use one command's output as arguments to another command... -### d) How are text and binary files different?...