introduction to bash scripting greek

Upload: kostas-katsikogiannis

Post on 19-Feb-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/23/2019 Introduction to Bash Scripting GREEK

    1/63

    Bash Scripting

    ( e p a r a s k e i @ c s d . a u t h . g r )

    ( )

    5

    , 2 0 2 0 1 5

    mailto:[email protected]:[email protected]
  • 7/23/2019 Introduction to Bash Scripting GREEK

    2/63

    / :

    script

    /

    (Functions)

    : If-Else

    :

    : Tips,

    BASH SCRIPT 2

  • 7/23/2019 Introduction to Bash Scripting GREEK

    3/63

    / 1960 (command-line interfaces)

    Unix Bourne Shell(sh)

    o Bourne Shell , scripts

    shells : C shell (csh), Korn shell (ksh), Bourne-Again shell (bash)

    BASH SCRIPT 3

  • 7/23/2019 Introduction to Bash Scripting GREEK

    4/63

    / Bash Bourne Shell, Unix-based

    Linux :

    GUI: , drop-down menus,

    Terminal: (terminal)

    terminal Bash -

    Bash scripting

    .sh

    BASH SCRIPT 4

  • 7/23/2019 Introduction to Bash Scripting GREEK

    5/63

    BASH SCRIPT 5

  • 7/23/2019 Introduction to Bash Scripting GREEK

    6/63

    script terminal - , , :

    BASH SCRIPT 6

  • 7/23/2019 Introduction to Bash Scripting GREEK

    7/63

    script II, / / terminal

    .sh

    BASH SCRIPT 7

  • 7/23/2019 Introduction to Bash Scripting GREEK

    8/63

    script script files bash interpreter

    , /bin/bash

    # , , ,

    interpreter

    : interpreter!

    script,

    terminal

    chmod 700 /path/to/script/file.sh

    BASH SCRIPT 8

  • 7/23/2019 Introduction to Bash Scripting GREEK

    9/63

    script IV

    BASH SCRIPT 9

  • 7/23/2019 Introduction to Bash Scripting GREEK

    10/63

    script V /path/to/script/file.sh

    ./script.sh

    , , script

    , cd (change directory) path (.)

    , cd directory script

    BASH SCRIPT 10

  • 7/23/2019 Introduction to Bash Scripting GREEK

    11/63

    script VI

    BASH SCRIPT 11

  • 7/23/2019 Introduction to Bash Scripting GREEK

    12/63

    / , bash /

    echo,

    , readread name

    echo $name

    ,

    inlineread p Type in your name : name

    echo $name

    BASH SCRIPT 12

  • 7/23/2019 Introduction to Bash Scripting GREEK

    13/63

    bash scripting , ,

    ,

    = whitespace!

    var=FOO

    , $

    echo $var

    / _ !

    BASH SCRIPT 13

  • 7/23/2019 Introduction to Bash Scripting GREEK

    14/63

    ,

    $HOME:

    $BASH: Bash

    $HISTFILE:

    $USER: bash

    BASH SCRIPT 14

  • 7/23/2019 Introduction to Bash Scripting GREEK

    15/63

    bash scripts

    $?: (return value, exit status)

    $#: script

    $0, $1, $2, ...: script $@: script

    $$: pid script

    $SECONDS: script

    BASH SCRIPT 15

  • 7/23/2019 Introduction to Bash Scripting GREEK

    16/63

    - (child process) (parent process)

    C/C++/... main()

    bash-script exit

    0

    lefteris@ubuntu:~/Desktop$ ls

    Project1 script_examples script.sh~ vmware-tools-distrib

    lefteris@ubuntu:~/Desktop$ echo $?

    0

    lefteris@ubuntu:~/Desktop$ ls /test

    ls: cannot access Desktop/test: No such file or directory

    lefteris@ubuntu:~/Desktop$ echo $?

    2

    BASH SCRIPT 16

  • 7/23/2019 Introduction to Bash Scripting GREEK

    17/63

    Script:#!/bin/bash

    echo Number of parameters: $#

    echo First parameter: $1

    echo Third parameter: $3

    echo All parameters: $@

    :

    lefteris@ubuntu:~$ ./test.sh hello world

    Number of parameters: 2First parameter: hello

    Third parameter:

    All parameters: hello world

    BASH SCRIPT 17

  • 7/23/2019 Introduction to Bash Scripting GREEK

    18/63

    C, myfunction(){ ... }

    !

    scripts (non-interactive) (interactive)

    set

    lefteris@ubuntu:~$ testfunction(){

    echo Hello World!

    echo Input arguments: $@ }

    lefteris@ubuntu:~$ testfunction Hello Again!

    Hello World!

    Input arguments: Hello Again!

    BASH SCRIPT 18

  • 7/23/2019 Introduction to Bash Scripting GREEK

    19/63

    return exit code

    Bash

    !

    ( )

    local

    local ,

    BASH SCRIPT 19

  • 7/23/2019 Introduction to Bash Scripting GREEK

    20/63

    script#!/bin/bash

    myfunc1(){echo Start of function 1

    echo Hello World!

    echo End of function 1

    }

    myfunc2(){echo Start of function 2

    myfunc1

    echo End of function 2

    }

    echo Start of script

    myfunc1

    myfunc2

    echo End of script

    BASH SCRIPT 20

  • 7/23/2019 Introduction to Bash Scripting GREEK

    21/63

    lefteris@ubuntu:~$ ./test.shStart of script

    Start of function 1

    Hello World!

    End of function 1

    Start of function 2

    Start of function 1

    Hello World!

    End of function 1End of function 2

    End of script

    BASH SCRIPT 21

  • 7/23/2019 Introduction to Bash Scripting GREEK

    22/63

    BASH SCRIPT 22

  • 7/23/2019 Introduction to Bash Scripting GREEK

    23/63

    If-Else I !

    if [ condition1 ]; thenaction1

    elif [ condition2 ]; thenaction2

    elseaction

    fi

    ; then

    ;

    BASH SCRIPT 23

  • 7/23/2019 Introduction to Bash Scripting GREEK

    24/63

    If-Else IIlefteris@ubuntu:/home$ x=1lefteris@ubuntu:/home$>if [ $x == 1 ]; then> echo x is 1>else> echo x is not 1>fix is 1

    BASH SCRIPT 24

  • 7/23/2019 Introduction to Bash Scripting GREEK

    25/63

    If-Else III (Test

    [

    ) bash [ .

    if bash

    [ 0 then

    [ test

    if

    [

    BASH SCRIPT 25

  • 7/23/2019 Introduction to Bash Scripting GREEK

    26/63

    If-Else IVlefteris@ubuntu:/home$ x=1lefteris@ubuntu:/home$ test $x == 1lefteris@ubuntu:/home$ echo $?0lefteris@Ubuntu:/home$ [ $x == 1 ]

    lefteris@ubuntu:/home$ echo $?0lefteris@ubuntu:/home$ test $x == 2lefteris@ubuntu:/home$ echo $?1lefteris@ubuntu:/home$>if test $x==1; then> echo x is 1>fix is 1

    BASH SCRIPT 26

  • 7/23/2019 Introduction to Bash Scripting GREEK

    27/63

    (while)while [ condition ]do

    actiondone :

    lefteris@ubuntu:/home$ unset INPUTlefteris@ubuntu:/home$ while [ $INPUT != bye ];> do read INPUT; echo $INPUT;> donetesttest

    byebyelefteris@ubuntu:/home$

    BASH SCRIPT 27

  • 7/23/2019 Introduction to Bash Scripting GREEK

    28/63

    (for) foreach for

    for variableName in { list };

    do

    actiondone

    seq built-in range

    for count in {1..10} for count in seq 1 10

    do

    echo $count

    done

    BASH SCRIPT 28

  • 7/23/2019 Introduction to Bash Scripting GREEK

    29/63

    (for)$ seq 1 41234$ for count in seq 1 4; do echo $count; done1234

    $ for count in seq 1 4; do echo $count; doneseq14

    BASH SCRIPT 29

  • 7/23/2019 Introduction to Bash Scripting GREEK

    30/63

    IV (for)lefteris@ubuntu:/home$ for file in ls; do echo $file; donelost+foundlefteris

    ` ( ~ ) (command

    substitution) $(ls)

    .

    lefteris@ubuntu:/home$ lslost+found lefterislefteris@ubuntu:/home$ ls

    lost+found: command not foundlefteris@ubuntu:/home$ echo ls lost+found lefteris

    BASH SCRIPT 30

  • 7/23/2019 Introduction to Bash Scripting GREEK

    31/63

    case $variable in

    pattern-1)commands;;

    pattern-2)

    commands;;

    pattern-3|pattern-4|pattern-5)commands;;

    *)

    commands;;esac *) default case

    BASH SCRIPT 31

  • 7/23/2019 Introduction to Bash Scripting GREEK

    32/63

    echo Which Linux distribution do you know? read DISTRcase $DISTR in

    ubuntu)echo I also know it!;;

    centos|rhel)echo Hey! It is my favorite Server OS!;;

    windows)echo Very funny...;;

    *)

    echo Hmm, seems Ive never used it.;;

    esac

    BASH SCRIPT 32

  • 7/23/2019 Introduction to Bash Scripting GREEK

    33/63

    BASH SCRIPT 33

  • 7/23/2019 Introduction to Bash Scripting GREEK

    34/63

    Bash (Bourne Shell)

    : !

    !

    .

    BASH SCRIPT 34

  • 7/23/2019 Introduction to Bash Scripting GREEK

    35/63

    whatis:

    man: manual (vim-styleshortcuts! :q )

    whereis: path , manual

    which: path

    BASH SCRIPT 35

  • 7/23/2019 Introduction to Bash Scripting GREEK

    36/63

    lefteris@ubuntu:~/Desktop$ whatis lsls (1) - list directory contentslefteris@ubuntu:~/Desktop$ whereis lsls: /bin/ls /usr/share/man/man1/ls.1.gzlefteris@ubuntu:~/Desktop$ which ls/bin/ls

    lefteris@ubuntu:~/Desktop$ whatis timetime (1) - run programs and summarize systemresource usagetime (2) - get time in secondstime (7) - overview of time and timerslefteris@ubuntu:~/Desktop$ whereis timetime: /usr/bin/time /usr/bin/X11/time /usr/include/time.h

    /usr/share/man/man1/time.1.gz /usr/share/man/man7/time.7.gz

    BASH SCRIPT 36

  • 7/23/2019 Introduction to Bash Scripting GREEK

    37/63

    /

    $mv src dest

    ( src) dest

    BASH SCRIPT 37

  • 7/23/2019 Introduction to Bash Scripting GREEK

    38/63

    file file2

    $cp file file2

    file folder file2

    $cp file folder/file2

    fold1 fold2

    $cp fold2/* fold1

    fold1 fold2

    $cp -R fold2/* fold1

    BASH SCRIPT 38

  • 7/23/2019 Introduction to Bash Scripting GREEK

    39/63

    file ( )

    $touch file

    folder

    $mkdir folder

    file

    $rm file

    folder

    $rm -R folder folder

    $rmdir folder

    BASH SCRIPT 39

  • 7/23/2019 Introduction to Bash Scripting GREEK

    40/63

    I / .

    group (. chown chgrp)

    : , group

    : ,

    lefteris@ubuntu:~/Desktop/project1$ ls -l proj* data*

    -rw-rw-r-- 1 lefteris lefteris 12307 Oct 17 13:44 data.out-rwxrwxr-x 1 lefteris lefteris 3789 Oct 17 13:15 project1.sh

    BASH SCRIPT 40

  • 7/23/2019 Introduction to Bash Scripting GREEK

    41/63

    // / file$chmod +x file$chmod +r file$chmod +w file // / file$chmod -x file$chmod -r file$chmod -w file / file / group / other /

    $chmod u+r file$chmod g+r file$chmod o+r file$chmod a+r file

    BASH SCRIPT 41

  • 7/23/2019 Introduction to Bash Scripting GREEK

    42/63

    . chmod 774 file , group,

    BASH SCRIPT 42

    # Permission rwx

    7 read, write and execute 111

    6 read and write 110

    5 read and execute 101

    4 read only 100

    3 write and execute 011

    2 write only 010

    1 execute only 001

    0 none 000

  • 7/23/2019 Introduction to Bash Scripting GREEK

    43/63

    (stdin, cout)

    ls > output

    (stderr, cerr)

    ls > output 2>errors

    ls > output 2&>1

    . (append) >>

    ls >> output

    ls -la >> output

    BASH SCRIPT 43

  • 7/23/2019 Introduction to Bash Scripting GREEK

    44/63

    cat < input

    cat ( )

    lefteris@ubuntu:~/Desktop/project1$ cat

    Hello World!

    Hello World!

    Test

    Test

    ^C

    BASH SCRIPT 44

  • 7/23/2019 Introduction to Bash Scripting GREEK

    45/63

    cat (catenate)

    cat file1 file2

    cat file1 file2 > file3

    , ( less)

    cat file1

    Bonus: sortsort unsorted_file > sorted_file man sort !!!

    BASH SCRIPT 45

  • 7/23/2019 Introduction to Bash Scripting GREEK

    46/63

    Pipelining , pipeline |

    myfile

    cat myfile | sort

    ls | sort

    BASH SCRIPT 46

  • 7/23/2019 Introduction to Bash Scripting GREEK

    47/63

    Io bash

    $ echo $((5 + 1))

    6$a=1;b=3$ result=$((5 + $a * $b))$ echo $result8

    BASH SCRIPT 47

  • 7/23/2019 Introduction to Bash Scripting GREEK

    48/63

    bc (bench calculator)

    $ a=2; b=3.141;

    $(echo $a * $b | bc)$ echo $result6.282$ echo $(echo 5 * $b | bc)15.705

    BASH SCRIPT 48

  • 7/23/2019 Introduction to Bash Scripting GREEK

    49/63

    grep Io grep (Global Regular Expression Print)

    file test

    grep file testcat file | grep test

    cat file | grep -n test

    cat file | grep -v test

    BASH SCRIPT 49

  • 7/23/2019 Introduction to Bash Scripting GREEK

    50/63

    grep II

    cat file | grep -c test

    case-insensitive

    cat file | grep -i test !

    .

    c*

    c\+

    [0 - 9] ( 0 9)

    ^c

    $

    ^

    BASH SCRIPT 50

  • 7/23/2019 Introduction to Bash Scripting GREEK

    51/63

    awk I textprocessing

    .

    (default: /tab)

    awk .

    condition { action }

    BASH SCRIPT 51

  • 7/23/2019 Introduction to Bash Scripting GREEK

    52/63

    awk II

    awk { print } < data

    awk { print $2,$5 } < data

    !

    -Fawk < /etc/passwd -F: { print $6 }

    BASH SCRIPT 52

  • 7/23/2019 Introduction to Bash Scripting GREEK

    53/63

    awk III awk ....

    lefteris@ubuntu:~$ echo 5 4 | awk { print $1 + $2 }

    9

    lefteris@ubuntu:~$ echo 5 4 | awk { print $1 $2 }54

    lefteris@ubuntu:~$ echo 5 4 | awk { print $1, $2 }

    5 4

    0

    awk $1==0 { print $2 } < data

    awk $/regex/ { print $2 } < data

    BASH SCRIPT 53

  • 7/23/2019 Introduction to Bash Scripting GREEK

    54/63

    sleep time: time

    wc file: , (word count)

    wc -w file: (word count)

    head file -n 3:

    tail file -n 3:

    BASH SCRIPT 54

  • 7/23/2019 Introduction to Bash Scripting GREEK

    55/63

    background I ,

    &

    lefteris@ubuntu:~$ gedit

    lefteris@ubuntu:~$ gedit &[1] 6428

    lefteris@ubuntu:~$ jobs

    [1]+ Running gedit &

    lefteris@ubuntu:~$ jobs

    [1]+ Done gedit

    background bash jobnumber pid

    BASH SCRIPT 55

  • 7/23/2019 Introduction to Bash Scripting GREEK

    56/63

    background II wait jobs ( bash)

    lefteris@ubuntu:~$ sleep 10 &

    [1] 6528lefteris@ubuntu:~$ sleep 15 &[2] 6529lefteris@ubuntu:~$ sleep 5 &[3] 6530lefteris@ubuntu:~$ wait

    [1] Done sleep 10[3]+ Done sleep 5[2]+ Done sleep 15

    BASH SCRIPT 56

  • 7/23/2019 Introduction to Bash Scripting GREEK

    57/63

    ()

    :

    SIGINT (interrupt, Ctrl-C): .

    ( Ctrl-C vim).

    SIGTSTP (temporarily stop, Ctrl-Z): (. jobs). jobs .

    SIGTERM (termination): SIGINT.

    SIGKILL (kill): .

    BASH SCRIPT 57

  • 7/23/2019 Introduction to Bash Scripting GREEK

    58/63

    ( );

    kill

    kill pid SIGTERM ( )

    .

    kill -9 pid

    9 SIGKILL ( )

    BONUS: stdin ( sort). EOF (Ctr-D) .

    BASH SCRIPT 58

  • 7/23/2019 Introduction to Bash Scripting GREEK

    59/63

    ps (processes)ps auxps -ef -fps -f -u lefterisps -ef | grep bash

    top

    procfs ( /proc)

    cat /proc/pid/status$ cat /proc/1276/status | grep ThreadsThreads: 3

    BASH SCRIPT 59

  • 7/23/2019 Introduction to Bash Scripting GREEK

    60/63

    Tips

    BASH SCRIPT 60

  • 7/23/2019 Introduction to Bash Scripting GREEK

    61/63

    Tips I () ;

    ls > /dev/null

    ;for word in cat file;do

    echo $worddone

    ;while read line

    do echo $worddone < file

    BASH SCRIPT 61

  • 7/23/2019 Introduction to Bash Scripting GREEK

    62/63

    Tips II ;if [ -z $variable ]

    if [ x$variable == x ]

    ;if [ -n $variable ]

    BASH SCRIPT 62

  • 7/23/2019 Introduction to Bash Scripting GREEK

    63/63

    Advanced Bash-Scripting Guide http://tldp.org/LDP/abs/html/index.html

    Beginning Linux Programming, Neil Matthew, Richard Stones

    http://ubuntuguide.org

    man pages

    http://tldp.org/LDP/abs/html/index.htmlhttp://tldp.org/LDP/abs/html/index.htmlhttp://ubuntuguide.org/http://ubuntuguide.org/http://ubuntuguide.org/http://ubuntuguide.org/http://tldp.org/LDP/abs/html/index.html