Lecture 02: user environment its a series of tubes Hands-On Unix - - PowerPoint PPT Presentation

lecture 02 user environment it s a series of tubes
SMART_READER_LITE
LIVE PREVIEW

Lecture 02: user environment its a series of tubes Hands-On Unix - - PowerPoint PPT Presentation

Lecture 02: user environment its a series of tubes Hands-On Unix System Administration DeCal 2012-09-10 1 / 22 Review Shell interaction Basic commands Shell variables Shell expansion Review Pipes 2 / 22 Shell interaction


slide-1
SLIDE 1

1 / 22

Lecture 02: user environment it’s a series of tubes

Hands-On Unix System Administration DeCal

2012-09-10

slide-2
SLIDE 2

Review

Review ❖ Shell interaction ❖ Basic commands Shell variables Shell expansion Pipes 2 / 22

slide-3
SLIDE 3

Shell interaction

Review ❖ Shell interaction ❖ Basic commands Shell variables Shell expansion Pipes 3 / 22

  • e.g., bash
  • there’s a prompt, type something, read
  • utput, repeat
  • command history
  • tab-completion
slide-4
SLIDE 4

Basic commands

Review ❖ Shell interaction ❖ Basic commands Shell variables Shell expansion Pipes 4 / 22

  • pwd, ls, cd, mkdir, rmdir,

touch, mv, cp, rm, echo, cat, less, grep

  • whoami (who am I)

uname (what I am I running) hostname (where am I) date (when)

slide-5
SLIDE 5

Shell variables

Review Shell variables ❖ Variables ❖ Shell script arguments ❖ Environment variables ❖ Env examples ❖ Quotes Shell expansion Pipes 5 / 22

slide-6
SLIDE 6

Variables

Review Shell variables ❖ Variables ❖ Shell script arguments ❖ Environment variables ❖ Env examples ❖ Quotes Shell expansion Pipes 6 / 22

  • case-sensitive
  • quotes matter (more on this later)
  • $ variable=’value’

$ echo "$variable" value

slide-7
SLIDE 7

Shell script arguments

Review Shell variables ❖ Variables ❖ Shell script arguments ❖ Environment variables ❖ Env examples ❖ Quotes Shell expansion Pipes 7 / 22

  • $1, $2, $3. . . : individual arguments
  • $@, $*: all arguments

(behave differently quoted)

  • $#: number of arguments
  • $0: name of running script
  • $?: exit status of previous command
slide-8
SLIDE 8

Environment variables

Review Shell variables ❖ Variables ❖ Shell script arguments ❖ Environment variables ❖ Env examples ❖ Quotes Shell expansion Pipes 8 / 22

  • created by init scripts (during startup),

by shell, or other parent processes along the way

inherited by child processes

typically names are capitalized

  • display with env or printenv, set

with export (for bash only)

slide-9
SLIDE 9

Env examples

Review Shell variables ❖ Variables ❖ Shell script arguments ❖ Environment variables ❖ Env examples ❖ Quotes Shell expansion Pipes 9 / 22

  • $HOME (home directory): e.g.,

/home/jdoe

  • $PATH (directories to search for

commands): e.g.,

/usr/local/bin:/usr/bin:/bin

  • $PS1 (prompt display, depends on

shell): e.g., \n\[\e[0;31m\]\u\[\e[m\]

@\[\e[1;34m\]\w \[\e[2;90m\]\@ \n\[\e[m\]\[\e[0;35m\]\h\[\e[m\]\[\e[0;31m\]\$ \[\e[m\]\[\e[0;32m\]

slide-10
SLIDE 10

Quotes

Review Shell variables ❖ Variables ❖ Shell script arguments ❖ Environment variables ❖ Env examples ❖ Quotes Shell expansion Pipes 10 / 22

  • no quotes: spaces and “special”

characters ($, *, ?, !, ‘, \, ’, ", etc.) have special meanings

  • double quotes: double quote and

($, ‘, \) have special meanings

  • single quotes: only single quote (’)

has special meaning

slide-11
SLIDE 11

Shell expansion

Review Shell variables Shell expansion ❖ Aliases ❖ Globbing ❖ Substitution Pipes 11 / 22

slide-12
SLIDE 12

Aliases

Review Shell variables Shell expansion ❖ Aliases ❖ Globbing ❖ Substitution Pipes 12 / 22

  • replacement of word by another string
  • e.g., in bash, alias dir=’ls’
  • view current aliases with alias
  • alternatives: functions or

scripts/symlinks in PATH

slide-13
SLIDE 13

Globbing

Review Shell variables Shell expansion ❖ Aliases ❖ Globbing ❖ Substitution Pipes 13 / 22

  • parameter expansion using wildcard

character (* and ?)

  • prevent with escape characters or

quotes

slide-14
SLIDE 14

Substitution

Review Shell variables Shell expansion ❖ Aliases ❖ Globbing ❖ Substitution Pipes 14 / 22

  • utput (stdout) of command in

backticks (‘) is substituted in command line

  • useful to assign variables, e.g.,

USER=‘whoami‘

  • bash alternative $(...)
slide-15
SLIDE 15

Pipes

Review Shell variables Shell expansion Pipes ❖ Real pipes ❖ Silly words ❖ Wise words ❖ Unix pipes ❖ Standard streams ❖ Named pipes (FIFO) ❖ Network pipes (netcat) 15 / 22

slide-16
SLIDE 16

President Obama at TransCanada Stillwater Pipe Yard (March 22 photo by AP via bloomberg.com)

slide-17
SLIDE 17

Silly words

Review Shell variables Shell expansion Pipes ❖ Real pipes ❖ Silly words ❖ Wise words ❖ Unix pipes ❖ Standard streams ❖ Named pipes (FIFO) ❖ Network pipes (netcat) 17 / 22

“And again, the Internet is not something that you just dump something on. It’s not a big truck. It’s a series of tubes.”

(Sen Ted Stevens, R-AK)

slide-18
SLIDE 18

Wise words

Review Shell variables Shell expansion Pipes ❖ Real pipes ❖ Silly words ❖ Wise words ❖ Unix pipes ❖ Standard streams ❖ Named pipes (FIFO) ❖ Network pipes (netcat) 18 / 22

“This is the Unix philosophy. Write programs that do one thing and do it well. Write programs to work

  • together. Write programs to

handle text streams, because that is a universal interface.”

(Doug McIlroy, inventor of Unix pipes)

slide-19
SLIDE 19

Unix pipes

Review Shell variables Shell expansion Pipes ❖ Real pipes ❖ Silly words ❖ Wise words ❖ Unix pipes ❖ Standard streams ❖ Named pipes (FIFO) ❖ Network pipes (netcat) 19 / 22

http://en.wikipedia.org/ wiki/File:Pipeline.svg

slide-20
SLIDE 20

Standard streams

Review Shell variables Shell expansion Pipes ❖ Real pipes ❖ Silly words ❖ Wise words ❖ Unix pipes ❖ Standard streams ❖ Named pipes (FIFO) ❖ Network pipes (netcat) 20 / 22

  • standard input (stdin): text going into

program (read), typically TTY unless redirected

  • standard output (stdout): text going
  • ut of program (write), typically TTY

unless redirected

  • standard error (stderr):

error/diagnostic text output, not redirected

slide-21
SLIDE 21

Named pipes (FIFO)

Review Shell variables Shell expansion Pipes ❖ Real pipes ❖ Silly words ❖ Wise words ❖ Unix pipes ❖ Standard streams ❖ Named pipes (FIFO) ❖ Network pipes (netcat) 21 / 22

  • named in contrast to anonymous ones

created by shell

  • special file created using mkfifo
  • can be used as a temporary file
slide-22
SLIDE 22

Network pipes (netcat)

Review Shell variables Shell expansion Pipes ❖ Real pipes ❖ Silly words ❖ Wise words ❖ Unix pipes ❖ Standard streams ❖ Named pipes (FIFO) ❖ Network pipes (netcat) 22 / 22

Imagine the possibilities:

  • dd
  • gzip