Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
command-line_ninjary [2013/03/06 23:40] – ishivvers | command-line_ninjary [2013/04/01 14:59] (current) – [Navigation] c.axen | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Command-line Ninjary GSPS 2013 ====== | ||
+ | This is meant to be a quick reference guide to command-line usage for those of us | ||
+ | who have the basics down, but aren't yet true ninjas. | ||
+ | or quick examples! | ||
+ | so learn to love it and use it well. Note: this page was written for '' | ||
+ | but much of this stuff should translate to '' | ||
+ | |||
+ | ------------------------------------ | ||
+ | |||
+ | ==== Navigation ==== | ||
+ | General notes about navigation commands: First, " | ||
+ | |||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * ('' | ||
+ | * Ctrl-f: move forward one character | ||
+ | * Ctrl-b: move backward one character | ||
+ | * use backslash '' | ||
+ | | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * e.g.: '' | ||
+ | | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * keep typing to refine search== | ||
+ | * '' | ||
+ | * and hit '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | | ||
+ | * '' | ||
+ | * executes command upon exit | ||
+ | * can be invoked in the middle of composing command | ||
+ | | ||
+ | * curly braces perform parameter expansion, inserting a copied string with contents of braces swapped | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | |||
+ | |||
+ | ------------------------------------ | ||
+ | |||
+ | |||
+ | |||
+ | ==== Really Useful Built-ins ==== | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * really helpful when in some other program like a text editor or python/IRAF terminal | ||
+ | | ||
+ | * '' | ||
+ | * ('' | ||
+ | | ||
+ | * '' | ||
+ | * '' | ||
+ | | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | | ||
+ | * '' | ||
+ | * //xxxx// can be simple string or regular expression | ||
+ | * '' | ||
+ | * //path// is optional - defaults to working directory | ||
+ | * //xxxx// can be simple string or regular expression | ||
+ | | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * sadly, no equivalent to above on most macs | ||
+ | * '' | ||
+ | * '' | ||
+ | | ||
+ | * '' | ||
+ | * can choose output format - i.e.: '' | ||
+ | * '' | ||
+ | * good examples [[http:// | ||
+ | * '' | ||
+ | * '' | ||
+ | * i.e.: '' | ||
+ | * understands pretty broad range of //time// definitions | ||
+ | | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * great for '' | ||
+ | * full guide [[http:// | ||
+ | |||
+ | == Get A Package Manager == | ||
+ | |||
+ | Best way to install and manage command-line tools. Native for all linux builds; have a couple good choices for macs. | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | |||
+ | |||
+ | ------------------------------------ | ||
+ | |||
+ | |||
+ | |||
+ | ==== Pipes & Redirects ==== | ||
+ | |||
+ | === Invoking multiple commands === | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | === Piping stuff around === | ||
+ | * '' | ||
+ | * overwrites //file// | ||
+ | * '' | ||
+ | | ||
+ | * '' | ||
+ | | ||
+ | * '' | ||
+ | * '' | ||
+ | * e.g.: '' | ||
+ | * '' | ||
+ | * e.g.: '' | ||
+ | | ||
+ | * '' | ||
+ | |||
+ | |||
+ | == Useful tools when piping == | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | | ||
+ | * '' | ||
+ | * can be used with a pipe or with a file | ||
+ | * use it to translate between lines in a file or pipe and arguments to a function | ||
+ | * best to understand by example | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | | ||
+ | * '' | ||
+ | * basic usages are powerful, learn more [[http:// | ||
+ | * '' | ||
+ | * '' | ||
+ | * Example: search for all running python instances, find their //PID//, and kill them | ||
+ | * '' | ||
+ | * Example: count and display all the items in current working directory | ||
+ | * '' | ||
+ | * Example: replace every occurrence of '' | ||
+ | * '' | ||
+ | |||
+ | ------------------------------------ | ||
+ | |||
+ | |||
+ | ==== Loops & Functions ==== | ||
+ | |||
+ | === Variables === | ||
+ | Variables that have already been declared are prefaced by a '' | ||
+ | one when first declaring a variable. | ||
+ | to declare a number use the '' | ||
+ | wrapping it in back-ticks or parentheses. | ||
+ | There are some system-wide variables that should always be available, like '' | ||
+ | Note that though convention says system-wide variables should be all caps, they don't need to be. | ||
+ | |||
+ | === Simple Loops === | ||
+ | There are several ways to write loops, the most useful of which are below. | ||
+ | Note that in bash an '' | ||
+ | by default, treated as strings. | ||
+ | '' | ||
+ | A few examples: | ||
+ | |||
+ | for i in {1..20} | ||
+ | do | ||
+ | echo $i | ||
+ | done | ||
+ | |||
+ | for i in {a,b,d,g}; do echo $i; done | ||
+ | |||
+ | for i in a 2 b 6; | ||
+ | do | ||
+ | echo $i; | ||
+ | done | ||
+ | |||
+ | for file in *filePattern*; | ||
+ | do | ||
+ | echo file; | ||
+ | done | ||
+ | |||
+ | |||
+ | === Simple Tests === | ||
+ | |||
+ | The shell can do arithmetic and logic tests, but I think if you're doing | ||
+ | anything very complex you should move into '' | ||
+ | should be put in square brackets (spacing is important), '' | ||
+ | be closed with a '' | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * ''&&'': | ||
+ | * '' | ||
+ | |||
+ | |||
+ | == Use more aliases! == | ||
+ | Use aliases to make hard-to-remember commands less terrible. | ||
+ | |||
+ | Example: | ||
+ | http:// | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | |||
+ | == Defining your own functions == | ||
+ | |||
+ | You can declare functions and use them at any time. The most common way | ||
+ | to interact with functions is through required arguments, as shown below. | ||
+ | Adding in flags (like '' | ||
+ | (look up '' | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Some examples: | ||
+ | |||
+ | function speak () { | ||
+ | echo here are all $# arguments: $@ | ||
+ | echo here is argument 1: $1 | ||
+ | echo here is argument 3: $3 | ||
+ | } | ||
+ | |||
+ | function killme () { | ||
+ | ps -e | grep $1 | awk ' | ||
+ | echo $1 is dead | ||
+ | } | ||
+ | |||
+ | function cls () { | ||
+ | cd $1 | ||
+ | ls | ||
+ | } | ||
+ | |||
+ | |||
+ | == .profile or .bashrc == | ||
+ | |||
+ | There are a set of system files in your home directory that are executed every time | ||
+ | a terminal is opened - use those to define aliases, functions, or variables | ||
+ | you want always available. |