Skip to content

alextsui05/bash-cli-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

It's hard to test and edit bash scripts. It usually involves simultaneously writing, running, cleaning up errors generated by broken logic, and repeating. It would be nice to be able to do a "dry run" with the flip of a switch and see that every call is working as expected. This project introduces a single file with helper functions to help do this.

Usage

First, you source the file and define the DRY_RUN and VERBOSE booleans:

#/bin/bash
source "bash-cli-utils/cli.h"
VERBOSE=false
DRY_RUN=false
while getopts ":nv" opt; do
    case "${opt}" in
        n)
            DRY_RUN=true
            ;;
        v)
            VERBOSE=true
            ;;
        *)
            echo "Unrecognized option -${OPTARG}"
            exit 1
            ;;
    esac
done
shift $((OPTIND-1))

# Here begins your command-line script logic

This introduces a few wrapper functions that you use throughout your script:

Commands

  1. run command

    This simply runs command. If the DRY_RUN switch is set, then the command simply gets echoed to standard output.

  2. debug message_string

    This echoes the message_string only if the VERBOSE switch is set.

  3. create_empty filename

    This creates an empty file at filename, or truncates it if it exists.

  4. redirect filename command

    This appends the standard output of command to the specified filename. If the DRY_RUN switch is set, then the command just gets echoed to standard output.

See the included example.sh for an example of how this is used.

License

MIT

About

Helper bash functions to ease writing bash command-line tools

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages