welcome to the bash workshop
play

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


  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

  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

  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

  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

  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

  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

  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 ’ Bash is awesome echo # 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

  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

  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

  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

  11. Overview What is bash? The basics What bash is used for Your time to shine! Strings Meaning of strings Hello World echo ◮ 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

  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

  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

  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

  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]]: not found command ◮ correct: [[ 1 == 3 ]] Bash Workshop 14 Aline Abler

  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

  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

  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

  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 0 Bash Workshop 18 Aline Abler

  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

  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

  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

  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

  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 Unimportant File.odt rm Bash Workshop 23 Aline Abler

  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

  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

  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

  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

  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

  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

  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

  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

  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

Recommend


More recommend