Code Monkey home page Code Monkey logo

bash-boilerplate's Introduction

Boilerplate BASH operations

My toolbox of Bash boilerplate code.

Just copy boilerplate.sh to your script directory, include it:

source boilerplate.sh

and get going.

In short

assert_number_of_arguments EXPECTED_NUMBER
exit_on_error "COMMAND"
to_lower "String in quotes" RESULT_VARIABLE_NAME
to_upper "String in quotes" RESULT_VARIABLE_NAME
decho "message"

Functions

assert_num_args(NUM)

Asserts the right number of command line arguments, or exit with error.

Example (test1.sh):

#!/bin/bash
source boilerplate.sh
assert_num_args 1

Output:

$ ./test1.sh
Usage: test1.sh
Expected 1 arguments (got 0)

exit_on_error(COMMAND)

Runs COMMAND. If the return value is different than 0, exits with that return value.

Example (test2.sh):

#!/bin/bash
source ../boilerplate.sh
exit_on_error 'echo "1+2" | bc'
exit_on_error 'asdasdsa asa sgfh jyh' 2>&1
echo "You should never see this line"

Output:

$ ./test2.sh
3
../boilerplate.sh: line 33: asdasdsa: command not found

Verify the return value of test2.sh:

$ echo $?
127

to_lower("String in quotes", RESULT_VARIABLE_NAME)

Converts the string to lowercase, and stores it in the new variable name.

Example (from test3.sh):

#!/bin/bash
source ../boilerplate.sh
s="And Now for Something Completely Different"
to_lower "$s" l
echo $l

Output:

AND NOW FOR SOMETHING COMPLETELY DIFFERENT

to_upper("String in quotes", RESULT_VARIABLE_NAME)

Converts the string to UPPERCASE, and stores it in the new variable name.

Example (from test3.sh):

#!/bin/bash
source ../boilerplate.sh
s="And Now for Something Completely Different"
to_lower "$s" l
echo $l

Output:

and now for something completely different

decho("MESSAGE")

decho stands for debug echo: Prints and echos MESSAGE according to debug flags.

  • echos $MESSAGE if $DEBUG_SHELL is either true, yes, on or 1.
  • appends $MESSAGE to the filename $DEBUG_LOG_FILE, if $DEBUG_LOG_FILE is not empty.

Example (from test4.sh):

#!/bin/bash
# Write to stdout and log file.
DEBUG_LOG_FILE="test_log_file"
DEBUG_SHELL="Yes"
decho "Screen and file."

# Write to stdout only.
DEBUG_LOG_FILE=""
DEBUG_SHELL="1"
decho "Screen only."

Output - stdout:

Screen only.

Output - test_log_file:

$ echo test_log_file
File only.

TODO

  • Better test suite:
  • Automatic (Runs with a single commmand)
  • Readable.
  • Add MIN, MAX optional arguments to assert_number_of_arguments
  • Support echo arguments (e.g. -n) in decho

bash-boilerplate's People

Contributors

adamatan avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.