1 of 21

Shell scripting and using the CLI

2 of 21

Mari Flåm

IT Avdelingen @ UiB��Contact at: mari.flam@nerdschool.no

Erlend Røsjø

Solution architect @ Sparebanken Vest

Contact at:�erlend.rosjo@nerdschool.no

Åse L Mattson

Developer @ Skatteetaten

3 of 21

4 of 21

https://www.meetup.com/bergen-nerdschool/

5 of 21

Workshop prerequisites

  • A UNIX-like console�
  • Linux or Mac? All set.�
  • Windows? Git Bash or WSL

6 of 21

Intentions of this workshop

  • Get a feel of using the terminal�
  • Learn common shell commands�
  • Create shell scripts to make your life easier

7 of 21

Why use the command line?

8 of 21

Why use the command line?

  • Using the command line (or Command Line Interface - CLI) has many advantages and uses:
    • Speed - The CLI is in many cases a lot faster than using a GUI if you know the commands
    • Automation - Repetitive tasks can be automated using scripts
    • Repeatability - Scripting tasks allow you to standardize how a task is performed, so that the result of the task is the same every time
    • Power - The CLI is a very powerful tool that allows you to combine different commands to do a lot of work with little syntax
    • Sometimes, you have no choice - When working on remote servers, you will probably not have access to a graphical user interface

9 of 21

CLI for programming

  • A lot of tools for programming today are only accessible as command line programs, or much more efficient to use as command-line programs
  • Learning to use the CLI will enable you to work more efficiently as a developer

10 of 21

The shell

11 of 21

How does the shell work?

  • Type a command, press enter�
  • Command executes immediately�
  • Result is printed to the screen (standard output/standard out)

12 of 21

What is a command?

  • Small program that you are able to execute from the command line�
  • A command can take zero or many parameters / inputs ��
  • Example:
    • ls
    • pwd
    • cd

13 of 21

General syntax for commands

  • <command>
  • <command> <argument>
  • <command> -<parameter>
  • <command> -<parameter> <argument>

Example:

  • cp -R dir1/ dir2/ - copy all files and subdirectories from dir1 into dir2

14 of 21

Some “common” parameters�

  • -h help
  • -v version or verbose
  • -r or -R recursive
  • -n number
  • -f file or force�
  • Always check if you’re unsure. -h for Help is almost always available.

15 of 21

All PATHs lead to Rome

  • The shell knows about the program (command) because it’s located in a directory the shell scans.�
  • We can add and remove directories the shell should know about by modifying the machine’s PATH variable.

16 of 21

|

  • Pipe�
  • Puts (pipes) commands together�
  • Sends output from standard out (stdout) to standard in (stdin)
    • (Output of one command becomes the input of the next command in the chain)

17 of 21

Assignments

18 of 21

Assignments outline�

  • Exercise 1 - Getting started
  • Exercise 2 - Basics of the shell
  • Exercise 3 - Scripting
  • Exercise 4 - Using pipelines and redirects
  • Exercise 5 - Number guessing game

19 of 21

Assignments�

  • Follow the README: https://github.com/nerdschoolbergen/shellscripting
  • Windows users: Git bash is a bash emulation layer. You may encounter some weirdness with i.e permissions�
  • Use the solutions as hints if you like. You are here to learn, it is not an exam.�
  • Try to type out stuff yourself to get a feel of using the terminal and its shortcuts

20 of 21

Contributing

  • We are open source! �
  • We appreciate any contribution, whether it be typos or suggestions for improvements (Create an issue in the issue tracker)

21 of 21

WIFI

  • SSID: uib-guest
    • Register with your phone number