bash_day1

Embed Size (px)

Citation preview

  • 8/13/2019 bash_day1

    1/31

    Scripting with Bash and Python

    Compact Course @ Max-Planck

    Tobias Neckel

    October 07 - 18, 2013

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 1

  • 8/13/2019 bash_day1

    2/31

    General Information

    Organization

    About 1.5 hours lecture

    Slides and live demonstrations

    About 1.5 hours tutorials

    We suggest tasks to practise You can work on whatever you want Ill try to answer any questions

    Breaks :-)

    Outline

    4 days Bash 5 days Python

    One day off in second week

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 2

  • 8/13/2019 bash_day1

    3/31

    General Information

    About this course

    Scripting in Bash and Python (and beyond)

    Your background?

    Prior knowledge? Feedback!!

    More information

    Slides and tutorials on website:http://www5.in.tum.de/wiki/index.php/Scripting_with_

    Bash_and_Python_-_2013

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 3

  • 8/13/2019 bash_day1

    4/31

    Contents

    Bash (ca. 4 days): Overview, most important basics:

    Files and directories: ls, dir, cd, mkdir, find, touch, . . . Variables, manipulation of variables, arrays, special variables Control structures, functions, parameters

    Shell configuration PS1-PS4 (prompt statement) Aliases and variables Configuration examples for bash (bashrc) Pipes and wildcards

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 4

  • 8/13/2019 bash_day1

    5/31

    Contents (cont.)

    Bash (ca. 4 days): Advanced topics

    Regular expressions String-manipulation, regular expressions: grep, cut, sed,

    head, . . .

    Operating on files Grouping, subshells awk

    Working remote

    ssh, scp, rsync, unison, nx . . .

    Process management Visualization with gnuplot

    Parsing, preprocessing data Interaction with shell tools

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 5

  • 8/13/2019 bash_day1

    6/31

    Contents (cont.)

    Scripting with Python . . . and beyond (ca. 5 days)

    Overview

    Interpreted vs. compiled programming languages Python command shell vs. executable Python programs,

    IPython Where to use it and where not

    Data types and control structures

    Objects, type declarations Numeric types, sequences (list, tuple, dict, strings), . . . Control structures: if, for, break, while, . . . Arithmetic operations

    Functions and parameters

    String handling: strip, trim, split, ...

    File I/O

    Objects, classes, modules, and packages

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 6

  • 8/13/2019 bash_day1

    7/31

    Contents (cont.)

    Scripting with Python . . . and beyond (ca. 5 days)

    Object oriented programming

    A short introduction to OOP OOP in Python

    Common and useful libraries

    os, sys Regular expressions: re Command-line options: optparse math, random GUIs with tkinter ...

    Scientific Computing in Python

    NumPy (vectors, matrices, . . . ) Matplotlib Calling gnuplot

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 7

  • 8/13/2019 bash_day1

    8/31

    Requirements

    Bash

    Bash as shell (yes, tcsh works, too, but slightly different) Editor of your choice (vim, emacs, . . . )

    Collection of standard tools

    gnuplot

    If you are using Windows, we recommend to install cygwinfull/extended

    Python

    Python 2.7.x, as not all packages have been ported to Python3.X

    Packages: Scipy (which should include IPython as interactive Python

    shell; if not, get IPython) PyLab/Matplotlib

    Gnuplot

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 8

  • 8/13/2019 bash_day1

    9/31

    Part I

    Introduction

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 9

  • 8/13/2019 bash_day1

    10/31

    Some Types of Programming Langages

    Declarative programming

    Functional (Lisp, Scheme, make, ...) Logical (Prolog) Other (Sql, ...)

    Imperative programming

    Procedural (C, Fortran, Python, Pascal, ...) Modular (Bash, ...) Object-oriented (C++, Java, Python, ...)

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 10

  • 8/13/2019 bash_day1

    11/31

    Scripting vs. Compiled Languages

    Scripting languages Bash, Python, Perl, postscript, matlab/octave, ...

    Interactive mode

    Few optimisations

    Easy to use

    No binary files (hardly useful for commercial software)

    Compiled languages

    C, C++, Fortran, ...

    Efficient for computational expensive tasks

    Source code is compiled to binary code

    Other

    Java

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 11

  • 8/13/2019 bash_day1

    12/31

    Shell vs. Python

    Shell (Bash)

    Seperate program for each simple task

    Gluing together programs with a script

    Not really a full programming language

    Powerful tools available Suitable for small tools (1-100 lines of code)

    Python

    Full programming language

    Large number of libraries available

    Intuitive naming conventions

    Suitable for almost any task

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 12

  • 8/13/2019 bash_day1

    13/31

    Use of Shell Scripts

    Where to use them:

    System administration

    Automating everyday terminal tasks

    Searching in and manipulating ASCII-Files

    ...

    Where not to use them:

    Lots of mathematical operations

    Computational expensive tasks

    Large programs which need structuring and modularisation

    Arbitrary file access Data structures

    Platform-independent programs

    ...

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 13

  • 8/13/2019 bash_day1

    14/31

    General Programming Rules

    Comments

    Comments

    Comments Problem algorithm program

    Modular programming

    Generic where possible, specific where necessary

    ...

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 14

  • 8/13/2019 bash_day1

    15/31

    Part II

    Bash Basics

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 15

  • 8/13/2019 bash_day1

    16/31

    Some Basic Commands

    The first interactive example:

    cd

    mkdir ba sh_c our se

    cd bash_course

    t ou c h h e ll o . s h

    c hm od u + x h el lo . s h

    < e d it or > h e l lo . s h

    File manipulation

    Files: touch, ls, rm, mv, cp

    Directories: cd, mkdir, rmdir, pwd

    Access with chmod: read, write and execute

    Editors: vi, emacs, gedit, nedit, ...

    Documentation: man, info, apropos, - - help

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 16

  • 8/13/2019 bash_day1

    17/31

    Hello World!

    The first non-interactive example (hello.sh):

    # ! / b i n / b a s h

    echo " H e l l o W o r l d ! "

    Convention: shell script suffix.sh

    #! sha-bang (#: sharp, !: bang)

    Sha-bang is followed by interpreter (try #!/bin/rm)

    echo is a shell builtin

    Hello World! is (as almost everything) a string

    hello.sh has to be executable (chmod)

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 17

  • 8/13/2019 bash_day1

    18/31

    Variables

    # ! / b i n / b a s hSTR= " H e l l o W o r l d ! "

    echo

    STR

    NO WHITESPACE in assignments

    STR: name of the variable used for assignment

    STR: reference to the value

    $STRis a short form of ${STR}

    Quoting

    # ! / b i n / b a s h

    STR= " H e l l o W o r l d ! "

    echo

    STR

    echo "

    S T R "

    echo

    STR

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 18

  • 8/13/2019 bash_day1

    19/31

    Operations

    a=3+5 # does not work a =

    expr 3 + 5

    # does work

    a = expr 3+5 # does not work

    let " a = 3 + 5 " # does work

    let " a = 3 + 5 " # does work

    a =

    ((3+5)) # does work

    a = [3+5] # does work

    No direct mathematical operations (everything is a string)

    command is used to call a program

    expr: evaluate expressions Only integer operations possible

    Operators for expr: comparison (

  • 8/13/2019 bash_day1

    20/31

    Arrays

    # ! / b i n / b a s h

    arr[0]=1

    arr[1]=

    ((

    { a r r [ 0 ] } * 2 ) )

    arr[2]=

    [

    { a r r [ 1 ] } * 2 ]

    arr[5]=32

    echo { a r r [ 0 ] }

    echo

    { a r r [ 1 ] }

    echo

    { a r r [ 2 ] }

    echo

    { a r r [ 3 ] }

    echo

    { a r r [ 4 ] }

    echo

    { a r r [ 5 ] }echo

    { # a r r [ * ] }

    echo { a r r [ * ] }

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 20

  • 8/13/2019 bash_day1

    21/31

    Special Variables

    within a script $? Exit status variable

    $$ process ID

    $0, $1, $2, ... command line parameters

    $* all command line parameters (single string)

    $@ all command line parameters (one string per parameter)

    $# number of command line parameters

    ${#variable}string length of the variable value

    ...

    Environment Variables

    env

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 21

  • 8/13/2019 bash_day1

    22/31

    Control Structures - if

    if [ 1 = 1 ]

    then

    echo " H e l l o W o r l d ! "

    fi

    Usually (most languages), something is done if test is true

    Usually, 0 is false, non-zero values are true

    In bash, test gives a return value

    Return value 0 means no error

    test 1 = 2 # e qu al s [ 1 = 2 ]

    test 1 = 1

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 22

  • 8/13/2019 bash_day1

    23/31

    Control Structures - test

    Usage: test EXPRESSION or [ EXPRESSION ]

    Exits with the status determined by the given expression

    And: EXPRESSION1 -a EXPRESSION2

    Or: EXPRESSION1 -o EXPRESSION2

    Negation: ! EXPRESSION

    String comparison: =, != ( test 1 = 1 is string comparison!) Integer comparison: -eq, -ge, -gt, -le, -lt, -ne

    File comparison: -ef, -nt, -ot

    File test with single operand: -e, -d, ...

    More details: man test

    test 1 - e q 1

    [ 1 - eq 1 ]

    [ [ 1 - e q 2 | | 1 - e q 1 ] ]

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 23

  • 8/13/2019 bash_day1

    24/31

    First Useful Example

    # ! / b i n / b a s h# e nc r yp ti o n p ro gr am

    clear

    which

    EDITOR

    if [

    ? != " 0 " ]

    then

    echo " W h i c h e d i t o r w o u l d y o u l i k e t o u s e ? "read EDITOR

    fi

    E D IT O R p l a in t ex t # g ed it d oe s no t w ork ! d et ac he d f ro m c on so

    g pg - a - -no - o pt io ns - o c ry pt io n . g pg - c p la i nt ex t

    s hr ed - u p la in te xt

    clearcat c r y p t i o n . g p g

    s hr ed - u c ry pt io n . g pg

    exit

    Tobias Neckel: Scripting with Bash and Python

    Compact Course @ Max-Planck, October 07 - 18, 2013 24

  • 8/13/2019 bash_day1

    25/31

    Control Structures - loops

    # ! / b i n / b a s hfor i in < l i s t >

    do

    done

    List can be any possible list of elements

    seq -s produces a list of numbers from 1 to x withseparator s (j o t - 1 xon MAC)

    for i in {1..x}does the same (no variable expansion!)

    break/breakn: stop the loop (n levels of nested loops) continue/continuen: continue with the next iteration

    Other loops: while [ condition ]; do command; done

    Other loops: until [ condition ]; do command; done

    Tobias Neckel: Scripting with Bash and PythonCompact Course @ Max-Planck, October 07 - 18, 2013 25

  • 8/13/2019 bash_day1

    26/31

    Functions

    # ! / b i n / b a s hfunction f u n c ti o n _n a m e {

    command

    }

    f u nc t io n _n a me () {

    command}

    Both syntactic variants do the same

    The round brackets are NOT used for parameters

    Functions have to be defined before they are used

    Functions may not be empty (use :)

    Parameter passing to functions equal to programs

    Tobias Neckel: Scripting with Bash and PythonCompact Course @ Max-Planck, October 07 - 18, 2013 26

  • 8/13/2019 bash_day1

    27/31

    Functions with Parameters

    function min {if [ 1 -lt 2 ]

    then

    return

    1

    else

    return

    2

    fi}

    a =

    m i n 4 6

    echo

    a # does not work

    min 4 6

    a =

    ? # w or ks

    min 500 600

    a =

    ? # does not work ! why ?

    Tobias Neckel: Scripting with Bash and PythonCompact Course @ Max-Planck, October 07 - 18, 2013 27

  • 8/13/2019 bash_day1

    28/31

    Functions with Parameters (2)

    function min {

    if [

    1 -lt

    2 ]

    then

    echo

    1

    else

    echo 2

    fi

    }

    a =

    ( min 4 6) # e qu al s a =

    m i n 4 6

    echo

    a

    Tobias Neckel: Scripting with Bash and PythonCompact Course @ Max-Planck, October 07 - 18, 2013 28

  • 8/13/2019 bash_day1

    29/31

    Command Substitution

    Allows output of a command to replace command itself

    ( command )

    command

    Both perfom expansion by executing command

    Replacing command with standard output of command, trailingnewlines deleted

    Not return code! May be nested: escape inner backquotes with backslashes

    Tobias Neckel: Scripting with Bash and PythonCompact Course @ Max-Planck, October 07 - 18, 2013 29

  • 8/13/2019 bash_day1

    30/31

    Control Structures - if (2)

    # ! / b i n / b a s h

    r a n d =

    [

    R A N D O M % 2 ]

    if [

    # - eq 0 ]

    then

    echo " U s a g e :

    0 < 0 o r 1 > "

    else

    if [[

    1 - ne 0 & &

    1 - ne 1 ] ]

    then

    echo " p a r a m e t e r h a s t o b e 0 o r 1 "

    elif [

    1 - eq

    ran d ]

    then

    echo " w o n "else

    echo " l o s t "

    fi

    fi

    Tobias Neckel: Scripting with Bash and PythonCompact Course @ Max-Planck, October 07 - 18, 2013 30

  • 8/13/2019 bash_day1

    31/31

    Control Structures - case and select

    # ! / b i n / b a s h

    echo " H i t a k e y , t h e n h i t r e t u r n . "

    read Key

    case "

    K e y " in

    [ [: l o we r : ]] ) echo " L o w e r c a s e l e t t e r " ;;

    [ [: u p pe r : ]] ) echo " U p p e r c a s e l e t t e r " ;;

    [0 -9] ) echo " D i g i t " ;;

    * ) echo " s o m e t h i n g e l s e " ;;

    esac

    # ! / b i n / b a s h

    PS3=

    C ho os e y ou r f av or it e l an gu ag e :

    select language in " b as h " " p y th on " " b r a in fu ck " " C ++ "do

    echo " Y o u r f a v o r i t e l a n g u a g e i s

    l a n g u a g e . "

    break

    done

    Tobias Neckel: Scripting with Bash and PythonCompact Course @ Max-Planck, October 07 - 18, 2013 31