Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

We do tedious repetitive tasks all the time

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

But there is a solution!

Slide 5

Slide 5 text

Bash.

Slide 6

Slide 6 text

Bash scripting – making your life easier Preslav Mihaylov 16.10.2016, Java2Days Sofia, Bulgaria

Slide 7

Slide 7 text

/PreslavMihaylov /PreslavMihaylov Software Engineer Technical Trainer /PreslavMihaylov

Slide 8

Slide 8 text

 What is a shell  How do we interact with the shell  Introduction to Bash scripting  Exploring Bash script syntax  Real world issues  When not to use Bash  Exploring all the different commands  In-depth coverage of bash features Agenda

Slide 9

Slide 9 text

What is a shell

Slide 10

Slide 10 text

OS SHELL USER

Slide 11

Slide 11 text

How do we interact with the shell Live Demo

Slide 12

Slide 12 text

OK. So we can type in commands in the interactive shell

Slide 13

Slide 13 text

But how do we automate them?

Slide 14

Slide 14 text

Introduction to Bash scripting

Slide 15

Slide 15 text

Moving from the command prompt To a text file Live Demo

Slide 16

Slide 16 text

The shebang... #!/bin/bash  … defines the program which will interpret the script   … is stationed on the first line of the script 

Slide 17

Slide 17 text

Redirecting I/O  One of the most powerful features   Allows us to combine programs together

Slide 18

Slide 18 text

Keyboard echo Screen echo “Hello world”

Slide 19

Slide 19 text

Keyboard echo Screen echo “Hello world” > log.txt log.txt

Slide 20

Slide 20 text

Keyboard echo Screen grep “hello”

Slide 21

Slide 21 text

echo Screen echo “Hello world” | grep “Hello” Screen grep Screen

Slide 22

Slide 22 text

Live Demo

Slide 23

Slide 23 text

Exploring Bash script syntax

Slide 24

Slide 24 text

Variables  How to declare them:  STR=Hello   How to use them:  echo $STR

Slide 25

Slide 25 text

How NOT to declare them  STR =Hello STR= Hello STR = Hello STR = Hello

Slide 26

Slide 26 text

How NOT to use them  echo $STR_pesho   Correct way:  echo ${STR}_pesho

Slide 27

Slide 27 text

Working with text  Parses text as is:  echo ’Hello $STR’   Interpolates text:  echo “Hello $STR”   Gets the output of the command:  echo “Hello $(cat name.txt)”

Slide 28

Slide 28 text

Live Demo

Slide 29

Slide 29 text

Making decisions  If ; then  ...  elif ; then  …  else  …  fi  Syntax:

Slide 30

Slide 30 text

Making decisions  The condition format depends on the brackets used   The brackets can be of many kinds. Depending on them you can do arithmetic expressions, regex, etc...   A short tutorial:  Bash scripting if statements

Slide 31

Slide 31 text

Live Demo

Slide 32

Slide 32 text

Repetition structures  for var in list; do …  done   while ; do …  done  Syntax:

Slide 33

Slide 33 text

Repetition structures  The list is a string with whitespace (depends on IFS) separated values   The var is a shell variable which acts as an iterator over the list   The condition works the same way it works with Decision structures

Slide 34

Slide 34 text

Live Demo

Slide 35

Slide 35 text

Real world issues

Slide 36

Slide 36 text

Three points of view  The User   The Developer   The Sysadmin

Slide 37

Slide 37 text

The User wants to...  … avoid tedious tasks.  … automate his daily tasks.  … customize his shell the way he wants.

Slide 38

Slide 38 text

The Developer wants to...  … do his work in peace.  … build his project appropriately.  … tune up his OS a bit.

Slide 39

Slide 39 text

The Sysadmin wants to...  … automate his work in order to play Counter Strike.  … maintain a stable system.  … avoid dealing with Users.

Slide 40

Slide 40 text

Live Demo

Slide 41

Slide 41 text

When not to use Bash

Slide 42

Slide 42 text

Avoid using Bash...  … on Heavy tasks.  … when the script gets too big.

Slide 43

Slide 43 text

Resources  TLDP - Intro Bash Scripting  TLDP - Advanced Bash Scripting  From Bash to Z Shell 

Slide 44

Slide 44 text

The GUI makes the simple tasks - even simpler. The Shell makes the impossible tasks - possible

Slide 45

Slide 45 text

May the Bash be with you /PreslavMihaylov /PreslavMihaylov /PreslavMihaylov 16.10.2016, Java2Days Sofia, Bulgaria