Skip to content

bekkopen/bash-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#!/bin/bash for programmers.

About

This project is used for workshop purposes and contains examples of (hopefully) good practices for bash scripting.

Source: https://github.com/steinim/bash-tutorial

Examples

Debugging

Source: scripts/debugging_easy_example.sh and scripts/debugging_tougher_example.sh

The point: Debugging is hard in bash.

Assignment: Find the bugs.

Syntax (readability)

Source: scripts/syntax.sh

The point: You can do similar things in different ways in bash.

Assignment: Try out the different ifs and explain the differences, when to use, and what you prefer.

Functions and configuration

Source: [include/common_functions.sh include/common_config.sh](bash-tutorial/blob/master/include/common_functions.sh include/common_config.sh)

The point: Reduce complexity, enhance testability, and reusability.

Assignment: Go through the functions and try to understand them. Write your own function. How can you run the functions directly from the command line?

Return values and return statuses

Source: include/common_functions.sh (functions: _is_snapshot, _startServers, _find_version_from_pom and _delete)

The point: Understand the differences between a return value and a return status.

Assignment: Revisit the functions and explain what the different functions return (values and/or statuses)

Pitfall return values

Source: scripts/pitfall_return_values.sh

The point: Understand a common pitfall when using return values.

Assignment: Comment in the echo statement in the function and see what happens. Why?

Pitfall return statuses

Source: scripts/pitfall_return_status.sh

The point: Understand a common pitfall when using return statuses.

Assignment: Why do the two statements return different statuses?

Including common functionality

Source: include/includes.sh

The point: Understand reusability by sourcing other files.

Assignment: Write a function in a file and include and use it in a script.

Debugging revisited

Source: [include/common_functions.sh scripts/debugging.sh](bash-tutorial/blob/master/include/common_functions.sh scripts/debugging.sh)

The point: Understand how you can debug your scripts without executing "dangerous" commands. Understand the -x, the -u, and the set -e set +e constructs.

Assignment: Write debugging for a function or script where it is lacking.

Logging

Source: scripts/logging.sh [scripts/rotating_log.sh include/log.sh](bash-tutorial/blob/master/scripts/rotating_log.sh include/log.sh)

The point: Understand how you can write your own logging framework.

Assignment: Write logging configuration to switch between logging to file and stdout.

Unit testing framework

Source: tests/runAll.sh

The point: Example of how you can syntax check and run all your tests.

Assignment: Make a test fail. Make syntax checking fail.

Unit testing using the && and || operators

Source: tests/testMs.sh

The point: An example of how you can unit test bash functions using the && and || operators.

Assignment: Write a unit test. Is this readable?

Unit testing with asserts

Source: bashUnit/asserts.sh tests/testVersionCheck.sh tests/testCheckHealth.sh

The point: An example of how you can unit test bash functions using asserts.

Assignment: How does assertEquals differ from the assertTrue in e. g. jUnit? Write an assertTrue function and a test that uses it. Is this more readable?

Bonus assignment: Write an assertArrayEquals function and a test that uses it.

Organizing files

Source: The project tree.

The point: Understand convention over configuration and why it is important to put different types of files in different places.

Assignment: How would you organize your files when you have specific scripts for different projects residing in different folders.

Packaging your app

Source: scripts/package.sh

The point: An example of how you can package your app.

Assignment: Package your app. What is the result?

Simple deploy and rollback

Source: scripts/deploy.sh scripts/rollback.sh

The point: These scripts are not very testable.

Assignment: Make the scripts testable.

Rerun

Rerun - a simple command runner that turns loose shell scripts into modular automation.

Rerun_image

Thats all folks!

Contact me:

About

A tutorial for responsible bash scripting

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages