Shell Programming (Part 1) Devin J. Pohly - - PowerPoint PPT Presentation

shell programming part 1
SMART_READER_LITE
LIVE PREVIEW

Shell Programming (Part 1) Devin J. Pohly - - PowerPoint PPT Presentation

Systems and Internet i Infrastructure Security i Institute for Networking and Security Research Department of Computer Science and Engineering Pennsylvania State University, University Park, PA Shell Programming (Part 1) Devin J. Pohly


slide-1
SLIDE 1

CMPSC 311: Introduction to Systems Programming Page 1

Institute for Networking and Security Research Department of Computer Science and Engineering Pennsylvania State University, University Park, PA

Systems and Internet Infrastructure Security

i i

Shell Programming (Part 1)

Devin J. Pohly <djpohly@cse.psu.edu>

slide-2
SLIDE 2

Page 2 CMPSC 311: Introduction to Systems Programming

Vim + Make

  • Vim integrates with make
  • Type :make target (or

just :make) to build

  • Vim will read the output

and jump to each error or warning

  • Next error: :cn
  • Previous error: :cp
  • Show current error

again: :cc

slide-3
SLIDE 3

Page 3 CMPSC 311: Introduction to Systems Programming

Shell programming

  • aka “shell scripting,”

“Bash scripting”

  • What is it?
  • Series of commands
  • Programming with

programs

  • What for
  • Automating
  • System administration
  • Prototyping
slide-4
SLIDE 4

Page 4 CMPSC 311: Introduction to Systems Programming

A sample script: shello

  • First line: interpreter
  • The #! is important!
  • Comment: # to end-of-line
  • Give the file execute

permission

  • chmod +x shello
  • Not determined by file

extension

  • Typical: .sh or none
  • Run it
  • ./shello

#! /bin/bash # Greetings! echo Shello world # Use a variable echo Shello "$USER" # Set a variable greetz=Shellutations echo "$greetz world"

slide-5
SLIDE 5

Page 5 CMPSC 311: Introduction to Systems Programming

Shell variables

  • Setting/unsetting
  • export

var=value

  • No spaces!
  • unset var
  • Using the value
  • $var
  • Untyped by default
  • Behave like strings
  • (See help declare for

more)

slide-6
SLIDE 6

Page 6 CMPSC 311: Introduction to Systems Programming

Special variables

  • Change shell behavior or

give you information

  • PWD: current directory
  • USER: name of the current

user

  • HOME: the current user’s

home directory

◾ Can usually be abbreviated as a

tilde (~)

  • PATH: where to search for

executables

  • PS1: Bash prompt (will see

later)

slide-7
SLIDE 7

Page 7 CMPSC 311: Introduction to Systems Programming

Exercise

  • Make a directory ~/bin
  • Move shello script

there

  • Prepend the directory to

your $PATH

  • PATH=~/bin:$PATH
  • Change to home dir
  • Run by typing shello
slide-8
SLIDE 8

Page 8 CMPSC 311: Introduction to Systems Programming

Shell initialization

  • Set custom variables
  • At startup, bash runs

shell commands from

~/.bashrc

  • Just a shell script
  • This script can do

whatever you want

slide-9
SLIDE 9

Page 9 CMPSC 311: Introduction to Systems Programming

Fun with prompts

  • Main prompt: $PS1
  • Special values
  • \u: username
  • \h: hostname
  • \w: working dir
  • \e: escape (for colors)
  • many others
  • This is something you

can set in your .bashrc

Very detailed treatment: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/

slide-10
SLIDE 10

Page 10 CMPSC 311: Introduction to Systems Programming

Aside: Vim initialization

  • Vim reads ‘:’ commands

from ~/.vimrc

  • Color scheme
  • Indenting preferences
  • Backup files
  • Appearance preferences

◾ Line numbers ◾ Highlighting matched

parentheses, braces, etc.

◾ Titlebar in terminal

  • Remapping keys

colorscheme koehler set autoindent set autowrite set backup set number set showmatch set title filetype plugin indent on syntax on

slide-11
SLIDE 11

Page 11 CMPSC 311: Introduction to Systems Programming

Special characters

  • echo Penn State is #1
  • echo Micro$oft Windows
  • echo Steins;Gate
  • What happened?
slide-12
SLIDE 12

Page 12 CMPSC 311: Introduction to Systems Programming

Special characters

  • echo Penn State is #1
  • echo Micro$oft Windows
  • echo Steins;Gate
  • What happened?
  • Many special characters
  • Whitespace
  • #$*&^?!~'`"\{}[]<>()|;
  • What if we want these

characters?

slide-13
SLIDE 13

Page 13 CMPSC 311: Introduction to Systems Programming

Quoting

  • Removes specialness
  • Hard quotes: '…'
  • Quote everything except

closing '

  • Soft quotes: "…"
  • Allow variables (and some
  • ther things)
  • Good practice: "$var"
  • Backslash (escaping)
  • Quotes next character
slide-14
SLIDE 14

Page 14 CMPSC 311: Introduction to Systems Programming

Arguments

  • In C: argc and argv[]
  • Split at whitespace
  • How can we override

this?

  • Arguments to a script
  • ./script foo bar
  • $# is the same as argc
  • "$@": all args
  • "$1" to "$9": individual
slide-15
SLIDE 15

Page 15 CMPSC 311: Introduction to Systems Programming

Debug mode

  • Shows each command
  • Variables expanded
  • Arguments quoted
  • Run with bash -x
  • Temporary – just for that

run

  • bash -x shello
  • Use -xv for even more

info

slide-16
SLIDE 16

Page 16 CMPSC 311: Introduction to Systems Programming

Exercises

  • Make a script that:
  • Prints its first argument

doubled

./script1 foo foofoo

  • Prints its first four args in

brackets, one per line

./script2 "foo bar" baz [foo bar] [baz] [] []

slide-17
SLIDE 17

Page 17 CMPSC 311: Introduction to Systems Programming

Redirecting input/output

  • Assigns stdin and/or

stdout to a file

  • echo hello > world
  • echo hello >> world
  • tr h j < world
slide-18
SLIDE 18

Page 18 CMPSC 311: Introduction to Systems Programming

Pipelines

  • Connect stdout of one

command to stdin of the next

  • echo hello | tr h j
  • … | rev
  • … | hexdump -C
  • … | grep 06
  • UNIX philosophy at

work!