Welcome to the Bash Workshop! If you prefer to work on your own, - - PowerPoint PPT Presentation

welcome to the bash workshop
SMART_READER_LITE
LIVE PREVIEW

Welcome to the Bash Workshop! If you prefer to work on your own, - - PowerPoint PPT Presentation

Welcome to the Bash Workshop! If you prefer to work on your own, already know programming or are confident in your abilities, please sit in the back . If you prefer guided exercises, are completely new to programming, or want to have your


slide-1
SLIDE 1

Welcome to the Bash Workshop!

◮ If you prefer to work on your own, already know programming or are confident in your abilities, please sit in the back. ◮ If you prefer guided exercises, are completely new to programming, or want to have your hands held, please sit in the front.

1

slide-2
SLIDE 2

Overview What is bash? The basics What bash is used for Your time to shine!

Bash Workshop

Aline Abler

Bash Workshop 1 Aline Abler

slide-3
SLIDE 3

Overview What is bash? The basics What bash is used for Your time to shine! Table of Contents

What will we do today?

◮ Theory

◮ What is bash? ◮ The basics

◮ Exercises

Bash Workshop 2 Aline Abler

slide-4
SLIDE 4

Overview What is bash? The basics What bash is used for Your time to shine! Shells

Bash is a not a programming language.

◮ It is a shell ◮ It is an interface between you and your computer ◮ It allows you to access the operating system’s services (i.e. run programs) ◮ It is not designed as a programming language, but can be used as such

Bash Workshop 3 Aline Abler

slide-5
SLIDE 5

Overview What is bash? The basics What bash is used for Your time to shine! Shells

Bash is not the only shell there is.

◮ sh, ksh, zsh, fish, dash. . . ◮ Most commands work in any shell, but some don’t ◮ Usually this doesn’t matter too much

Bash Workshop 4 Aline Abler

slide-6
SLIDE 6

Overview What is bash? The basics What bash is used for Your time to shine! Shells

Bash scripts are not programming

◮ Glue existing programs together to create new ones ◮ It’s possible to write entire programs, but please don’t

Bash Workshop 5 Aline Abler

slide-7
SLIDE 7

Overview What is bash? The basics What bash is used for Your time to shine! How to bash

Look, a bash script

# !/ bin/bash echo ’Hello World ’ echo Bash is awesome # Now some fun stuff: sudo zypper update notify -send ’Update complete ’ feh

  • -bg -fill ’pictures/ fancy_wallpaper .jpg ’

youtube -dl -o ’Video .%( ext)s’ ’https :// www.youtube.com/watch?v= lAIGb1lfpBw ’

Bash Workshop 6 Aline Abler

slide-8
SLIDE 8

Overview What is bash? The basics What bash is used for Your time to shine! How to bash

What bash can do

◮ Automate anything you can do from a console ◮ Let several separate programs work together

Bash Workshop 7 Aline Abler

slide-9
SLIDE 9

Overview What is bash? The basics What bash is used for Your time to shine! Strings

All about strings

Everything is a string

Bash Workshop 8 Aline Abler

slide-10
SLIDE 10

Overview What is bash? The basics What bash is used for Your time to shine! Strings

Strings and word splitting

◮ A string is a sequence of characters that is treated as a unit ◮ Commands are strings, too ◮ Strings are split at every space, tab, or newline unless they’re in quotes

Bash Workshop 9 Aline Abler

slide-11
SLIDE 11

Overview What is bash? The basics What bash is used for Your time to shine! Strings

Meaning of strings

echo Hello World

◮ echo, Hello and World are single strings ◮ The first string becomes the command, all following become arguments

echo ’Hello World ’

◮ Here, Hello World is just one string

Bash Workshop 10 Aline Abler

slide-12
SLIDE 12

Overview What is bash? The basics What bash is used for Your time to shine! Strings

Repeat after me

Every word is a single argument unless you use quotes.

Bash Workshop 11 Aline Abler

slide-13
SLIDE 13

Overview What is bash? The basics What bash is used for Your time to shine! Commands

All about commands

Everything that does something is a command

Bash Workshop 12 Aline Abler

slide-14
SLIDE 14

Overview What is bash? The basics What bash is used for Your time to shine! Commands

Why is this so important?

◮ if and while are commands ◮ [[ is a command

Bash Workshop 13 Aline Abler

slide-15
SLIDE 15

Overview What is bash? The basics What bash is used for Your time to shine! Commands

Example

◮ wrong:

[[1==3]]

◮ Bash’s answer:

bash: [[1==3]]: command not found

◮ correct:

[[ 1 == 3 ]]

Bash Workshop 14 Aline Abler

slide-16
SLIDE 16

Overview What is bash? The basics What bash is used for Your time to shine! Commands

Repeat after me

If there’s brackets, you probably need spaces.

Bash Workshop 15 Aline Abler

slide-17
SLIDE 17

Overview What is bash? The basics What bash is used for Your time to shine! All about return values

All about return values

Every command returns a value

Bash Workshop 16 Aline Abler

slide-18
SLIDE 18

Overview What is bash? The basics What bash is used for Your time to shine! All about return values

Return values

◮ Every command returns a number, its return value ◮ 0 means success Everything else means there was an error ◮ Some commands also print to stdout - that’s what you see.

Bash Workshop 17 Aline Abler

slide-19
SLIDE 19

Overview What is bash? The basics What bash is used for Your time to shine! All about return values

Return values

◮ What you run

echo ’Hello World ’

◮ What you see

Hello World

◮ What bash sees

Bash Workshop 18 Aline Abler

slide-20
SLIDE 20

Overview What is bash? The basics What bash is used for Your time to shine! All about return values

Why is that important?

◮ &&, ||, if and while all act based on the return value of something ◮ They disregard that command’s actual output

Bash Workshop 19 Aline Abler

slide-21
SLIDE 21

Overview What is bash? The basics What bash is used for Your time to shine! All about return values

Example

if ls -l foo then echo ’File foo exists ’ else echo ’File foo does not exist ’ fi

Bash Workshop 20 Aline Abler

slide-22
SLIDE 22

Overview What is bash? The basics What bash is used for Your time to shine! Expansion

Bash doesn’t only run commands

◮ Tilde expansion

~/files becomes /home/alinea/files

◮ Variable expansion

$BROWSER becomes Firefox

◮ Arithmetic expansion

$(( 1 + 4 )) becomes 5

◮ Command substitution

$( pwd ) becomes /home/alinea/scripts

◮ Pathname expansion (or globbing)

files/qui* becomes files/quicknotes files/quiz

Bash Workshop 21 Aline Abler

slide-23
SLIDE 23

Overview What is bash? The basics What bash is used for Your time to shine! Expansion

Bash doesn’t only run commands

◮ Expansion happens before any command is run ◮ Double quotes (") don’t prevent expansion, but single quotes (’) do.

$ echo "$HOME" ’$HOME ’ /home/alinea $HOME

◮ Expansion happens before word splitting

Bash Workshop 22 Aline Abler

slide-24
SLIDE 24

Overview What is bash? The basics What bash is used for Your time to shine! Expansion

The issue with word splitting

var=’Unimportant File.odt ’ rm $var # Variable expansion

becomes

rm Unimportant File.odt

Bash Workshop 23 Aline Abler

slide-25
SLIDE 25

Overview What is bash? The basics What bash is used for Your time to shine! Expansion

The issue with word splitting

◮ The correct way

rm "$var"

Bash Workshop 24 Aline Abler

slide-26
SLIDE 26

Overview What is bash? The basics What bash is used for Your time to shine! Expansion

Repeat after me:

If there’s a dollar, you probably need quotes!

Bash Workshop 25 Aline Abler

slide-27
SLIDE 27

Overview What is bash? The basics What bash is used for Your time to shine! The whole point of bash

Why is bash awesome?

◮ Bash can easily invoke any programs and connect them ◮ Bash is like glue

Bash Workshop 26 Aline Abler

slide-28
SLIDE 28

Overview What is bash? The basics What bash is used for Your time to shine! How to write a bash script

How to write a bash script

◮ I want a script that searches youtube and downloads the first video it finds

Bash Workshop 27 Aline Abler

slide-29
SLIDE 29

Overview What is bash? The basics What bash is used for Your time to shine! How to write a bash script

Splitting it up

◮ Search youtube ◮ Download video

Bash Workshop 28 Aline Abler

slide-30
SLIDE 30

Overview What is bash? The basics What bash is used for Your time to shine! How to write a bash script

Google for a program that already does what you need

◮ youtube-dl can download a video from youtube

youtube -dl -o ’Video .%( ext)s’ ’https :// www.youtube.com/watch?v= lAIGb1lfpBw ’

Bash Workshop 29 Aline Abler

slide-31
SLIDE 31

Overview What is bash? The basics What bash is used for Your time to shine! How to write a bash script

Splitting it up even further

◮ Search youtube

◮ Download youtube results website ◮ Search website text ◮ Find first youtube video link

◮ Download video

Bash Workshop 30 Aline Abler

slide-32
SLIDE 32

Overview What is bash? The basics What bash is used for Your time to shine! What happens now?

Hands on!

Self-driven exercises

◮ Self study using a guide ◮ Try some of our exercises ◮ Choose the exercises you find interesting! No need to go in order.

Guided exercises

◮ Solve easy exercises in plenum ◮ Tailored to complete beginners ◮ Please sit in the front

Bash Workshop 31 Aline Abler

slide-33
SLIDE 33

Overview What is bash? The basics What bash is used for Your time to shine! What happens now?

Course material

◮ These slides, exercise sheet and bash guide: http://thealternative.ch/index.php?view=knowhow ◮ Please leave some feedback! http://feedback.thealternative.ch ◮ Theme by Christian Horea, CC BY

Bash Workshop 32 Aline Abler