Unix shell talk - RIT SSE

Post on 15-Jan-2015

107 views 1 download

Tags:

description

Tech Talk given at the Rochester Institute of Technology's Society of Software Engineers about basic unix shell usage and productivity tips.

transcript

Text shellsMatt Mokary

and important productivity tips

TERMINAL-ogy● Terminal window terminal window

TERMINAL-ogy● Terminal window

● Terminal emulatorterminal emulator

terminal window

TERMINAL-ogy● Terminal window

● Terminal emulatorterminal emulator

terminal window

TERMINAL-ogy● Terminal window

● Terminal emulator

● Shell

terminal emulator

shell

terminal window

Modern Shells● C Shell (csh)

● Bourne Again Shell (bash)

● Korn Shell (ksh)

● Z Shell (zsh)

Modern Shells● C Shell (csh)

● Bourne Again Shell (bash)

● Korn Shell (ksh)

● Z Shell (zsh)

Basic usage<program name> <program flags> [ arg1, arg2, … ]

$ ls

Basic usage<program name> <program flags> [ arg1, arg2, … ]

$ ls

Applications Documents Library

Desktop Downloads Music

$

Basic usage<program name> <program flags> [ arg1, arg2, … ]

$ ls -a

<program name> <program flags> [ arg1, arg2, … ]

$ ls -adrwxr-xr-x 3 mattmokary staff 102 Feb 24 12:56 Applications

drwx------+ 5 mattmokary staff 170 Mar 20 17:06 Desktop

drwx------+ 11 mattmokary staff 374 Mar 20 12:03 Documents

drwx------+ 56 mattmokary staff 1904 Mar 20 14:54 Downloads

drwx------@ 49 mattmokary staff 1666 Feb 12 15:42 Library

drwx------+ 5 mattmokary staff 170 Feb 20 00:12 Music

$

Basic usage

<program name> <program flags> [ arg1, arg2, … ]

$ grep -n TODO MyProject.java

Basic usage

Basic usage<program name> <program flags> [ arg1, arg2, … ]

$ grep -n TODO MyProject.java

23: // TODO is there an off-by-one error here?

131: // TODO document this function

340: // TODO make this more efficient

$

<program name> <program flags> [ arg1, arg2, … ]

$ gcc -o HelloWorld hello_world.c

Basic usage

<program name> <program flags> [ arg1, arg2, … ]

$ gcc -o HelloWorld hello_world.c

$

Basic usage

<program name> <program flags> [ arg1, arg2, … ]

$ gcc -o HelloWorld hello_world.c

$ ls

HelloWorld hello_world.c hello_world.o

$

Basic usage

Streams

Streams● Standard Input (stdin)

Streams● Standard Input (stdin)

● Standard Output (stdout)

Streams● Standard Input (stdin)

● Standard Output (stdout)

● Standard Error (stderr)

Streams$ cat test.txt

Streams$ cat test.txtThis is a test file.It has several lines,none of which are very helpful.This is the last line of the file.$

Stream Redirection● >

Stream Redirection● > $ grep TODO MyProject.java

Stream Redirection● > $ grep TODO MyProject.java

// TODO make this better

// TODO delete these

$

Stream Redirection● > $ grep TODO MyProject.java > todo.txt

Stream Redirection● > $ grep TODO MyProject.java > todo.txt

$

Stream Redirection● > $ grep TODO MyProject.java > todo.txt

$ ls

MyProject.java todo.txt

$

Stream Redirection● > $ grep TODO MyProject.java > todo.txt

$ ls

MyProject.java todo.txt

$ cat todo.txt

// TODO make this better

// TODO delete these

$

Stream Redirection● >

>>

$ grep TODO MyProject.java > todo.txt

$ ls

MyProject.java todo.txt

$ cat todo.txt

// TODO make this better

// TODO delete these

$

Stream Redirection● >

>>

● <

Stream Redirection● >

>>

● <

$ grep TODO MyProject.java

// TODO make this better

// TODO delete these

$

Stream Redirection● >

>>

● <

$ grep TODO < MyProject.java

// TODO make this better

// TODO delete these

$

Stream Redirection● >

>>

● <

● |

Stream Redirection● >

>>

● <

● |

$ grep TODO MyProject.java > todo.txt

$ wc -l todo.txt

2

$ rm todo.txt

Stream Redirection● >

>>

● <

● |

$ grep TODO MyProject.java | wc -l

Stream Redirection● >

>>

● <

● |

$ grep TODO MyProject.java | wc -l

2

Stream Redirection● >

>>

● <

● |

$ grep TODO MyProject.java | wc -l

2

$ ruby get_hockey_tweets.rb \

| grep -in sedin | grep -i henrik \

| wc -l

173

$

Stream Redirection● > redirect stdout to file stream

>> append stdout to file stream

● < redirect file stream to stdin

● | redirect stdout and stderr to stdin

Stream Redirection● stdin (channel 0)

● stdout (channel 1)

● stderr (channel 2)

Stream Redirection$ ./sqrt -1 2> errors.txt

No real solution.

$

Stream Redirection$ ./sqrt -1 2> errors.txt

No real solution.

$ cat errors.txt

Argument is less than zero.

$

Stream Redirection$ ./sqrt -1 2> errors.txt 1> /dev/null

$ cat errors.txt

Argument is less than zero.

$

Stream Redirection$ ./postfix-solver 3 4 + 2>&1 > /dev/null \

| grep syntax > syntax-errors.txt

Stream Redirection$ ./postfix-solver 3 4 + 2>&1 > /dev/null \

| grep syntax > syntax-errors.txt

Stream Redirection$ ./postfix-solver 3 4 + 2>&1 > /dev/null \

| grep syntax > syntax-errors.txt

Stream Redirection$ ./postfix-solver 3 4 + 2>&1 > /dev/null \

| grep syntax > syntax-errors.txt

Stream Redirection$ ./postfix-solver 3 4 + 2>&1 > /dev/null \

| grep syntax > syntax-errors.txt

Stream Redirection$ ./postfix-solver 3 4 + 2>&1 > /dev/null \

| grep syntax > syntax-errors.txt

Stream Redirection$ ./postfix-solver 3 4 + 2>&1 > /dev/null \

| grep syntax > syntax-errors.txt

$

Stream Redirection$ ./postfix-solver 3 4 + 2>&1 > /dev/null \

| grep syntax > syntax-errors.txt

$ ls

postfix-solver syntax-errors.txt

$

Stream Redirection$ ./postfix-solver 3 4 + 2>&1 > /dev/null \

| grep syntax > syntax-errors.txt

$ ls

postfix-solver syntax-errors.txt

$ cat syntax-errors.txt

$

Stream Redirection● stdin 0● stdout 1● stderr 2● 2> redirect stderr to file stream● 2>&1 redirect stderr to current stdout

etc.

Every SE Ever:

Every SE Ever:“I have no idea why this isn’t working.”

Every SE Ever:“I have no idea why this isn’t working.”

“I have no idea why this is working.”

Every SE Ever:“I have no idea why this isn’t working.”

“I have no idea why this is working.”

“How do I get out of vim?”

vim Modes● Normal

vim Modes● Normal

o insert a new line beneath the cursor, move to it,and enter insert mode

$ move to the end of the line and remain innormal mode

i enter insert mode at the cursor’s currentposition

I move to the beginning of the line and enterinsert mode

dd cut the current line

vim Modes● Normal

● Insert

vim Modes● Normal

● Insert

● Command

vim Modes● Normal

● Insert

● Command

:w write the buffer to the disk

:q quit vim; will stop you if the file has changedsince the last write

:q! quit vim and discard changes since last write

:wq write the buffer and quit vim

:x same as :wq

:<some number>move the cursor to line <some number>

Typical vim Workflow

Typical vim Workflow$ vim some-file.txt

Typical vim Workflow$ vim some-file.txt

until you’ve made all the desired changes:navigate through the file in normal mode

Typical vim Workflow$ vim some-file.txt

until you’ve made all the desired changes:navigate through the file in normal modebounce between normal and insert until part is fixed

Typical vim Workflow$ vim some-file.txt

until you’ve made all the desired changes:navigate through the file in normal modebounce between normal and insert until part is fixedenter normal mode and write the file with “:w”

Typical vim Workflow$ vim some-file.txt

until you’ve made all the desired changes:navigate through the file in normal modebounce between normal and insert until part is fixedenter normal mode and write the file with “:w”

enter normal mode

Typical vim Workflow$ vim some-file.txt

until you’ve made all the desired changes:navigate through the file in normal modebounce between normal and insert until part is fixedenter normal mode and write the file with “:w”

enter normal modewrite the file and quit vim with “:wq” or “:x”

vim Visual Mode

vim Visual Modex cut the highlighted text and save it to vim’s

clipboard

y copy the highlighted text and save it to vim’sclipboard

p (in normal mode)paste the text in vim’s clipboard into the bufferafter the cursor

P (in normal mode)paste the text in vim’s clipboard into the bufferbefore the cursor

tmux

tmux$ Shell

tmux$ tmux new -s Crypto

$ Shell

tmux session“Crypto”

tmux$ tmux new -s Crypto

$ tmux ls

Crypto: 1 windows … (attached)

$

Shell

tmux session“Crypto”

tmux$ tmux new -s Crypto

$ tmux ls

Crypto: 1 windows … (attached)

$ tmux detach

$

Shell

tmux session“Crypto”

tmux$ tmux new -s Crypto

$ tmux ls

Crypto: 1 windows … (attached)

$ tmux detach

$ tmux new -s Grading

$

Shell

tmux session“Crypto”

tmux session“Grading”

tmux$ tmux new -s Crypto

$ tmux ls

Crypto: 1 windows … (attached)

$ tmux detach

$ tmux new -s Grading

$ tmux kill-session -t Crypto

$

Shell

tmux session“Grading”

tmux$ tmux new -s Crypto

$ tmux ls

Crypto: 1 windows … (attached)

$ tmux detach

$ tmux new -s Grading

$ tmux kill-session -t Crypto

$ tmux detach

$

Shell

tmux session“Grading”

tmux Session

tmux SessionCTRL+b % vertical split

tmux SessionCTRL+b % vertical split

CTRL+b “ horizontalsplit

tmux SessionCTRL+b % vertical split

CTRL+b “ horizontalsplit

CTRL+b x y kill pane

tmux SessionCTRL+b % vertical split

CTRL+b “ horizontalsplit

CTRL+b x y kill pane

CTRL+b arrows

navigate panes

My Personal Setup

Thanks!