Skip to content

Instantly share code, notes, and snippets.

@hvmonteiro
Created April 19, 2016 13:59
Show Gist options
  • Save hvmonteiro/4c4df4afd0f4d5908d5be890071b74c1 to your computer and use it in GitHub Desktop.
Save hvmonteiro/4c4df4afd0f4d5908d5be890071b74c1 to your computer and use it in GitHub Desktop.
Atom Editor snippets for Bash Shell Scripting
#
# Author: Hugo Monteiro
# Description: Atom Editor snippets for Bash Shell Scripting
# URL : https://gist.github.com/hvmonteiro/4c4df4afd0f4d5908d5be890071b74c1
# Last Changed: 19-04-2016
# Installation: File to be copy/pasted into Atom Editor snippets configuration file
'.source.shell':
# function to print a message and exit with an error
'function exit_with_error':
'prefix': 'exit_w'
'body': """
function exit_with_error() {
# \\$1 = Error message
# \\$2 = Exit code
[ \\"\\$1\\" == "" ] && ERROR_MSG=\\"An error as ocurred.\\" \\|\\| ERROR_MSG=\\"\\$1\\"
[ \\"\\$2\\" == "" ] && ERROR_CODE=1 || ERROR_CODE=\\$2
echo -e \\"$ERROR_MSG\\\\n\\" 1>&2
exit $ERROR_CODE
\\}
${1}
"""
# if...then...else...fi
'if...the...else...fi':
'prefix': 'if'
'body': """if [ condition ]; then
${1}
#statements
else
${2}
#statements
fi
"""
# echo to print to stderr
'function echo_error':
'prefix': 'echo_'
'body': """
function errecho() {
echo \\"$@\\" 1>&2 # Prints message to stderr instead of stdout
\\}
${1}
"""
# myexit function
'function myexit':
'prefix': 'myexi'
'body': """
function myexit() {
[ \\"\\$1\\" == \\"\\" ] && EXIT_CODE=0 || EXIT_CODE=\\$1
#statements
exit $EXIT_CODE
\\}
${1}
"""
# init script parameters case
'init script case':
'prefix': 'initcase'
'body': """
case \\$1 in:
stop)
;;
start)
;;
status)
;;
restart)
;;
reload)
;;
# other statements
*)
## If no parameters are given, print which are available.
echo \\"Usage: \\${0##*/} {stop|start|status|restart|reload}\\"
exit 2
;;
esac
${1}
"""
# function to print script usage
'function print_usage':
'prefix': 'printus'
'body': """
function print_usage() {
## If no parameters are given, print which are available.
echo \\"Usage: \\${0##*/} [option 1] <argument> [option 2] <argument>...\\" 1>&2
${1}#statements
echo \\""\\""
echo \\" -option description\\"
exit 2
\\}
"""
# bash shell script header
'bash header':
'prefix': 'bashheader'
'body': """
#!/bin/bash
#
# name: ${1}
# Description: This script ...
# Created by: <name>@<domain>
# Last Changed: <date> - <comment>
#
#
"""
# bash shell script command line parameters handler
'command line parameters snippet':
'prefix': 'scriptparameters'
'body': """
function print_usage () {
echo \\"Usage: \\${0##*/} [option1] <argument> [option2] <argument>...\\" 1>&2
exit 2
\\}
OPTIONS=\\"\\"
while getopts \\"a:w:n:b:r:sthv\\" c
do
case \\$c in
a) OPTIONS="$OPTIONS -a \\$OPTARG ";;
w) OPTIONS="$OPTIONS -w \\$OPTARG";;
n) OPTIONS="$OPTIONS -n \\$OPTARG";;
b) OPTIONS="$OPTIONS -b \\$OPTARG";;
r) OPTIONS="$OPTIONS -r \\$OPTARG";;
s) OPTIONS="$OPTIONS -s";;
t) OPTIONS="$OPTIONS -t";;
h) OPTIONS="$OPTIONS -h";;
v) OPTIONS="$OPTIONS -v";;
\\\\\?) print_usage;;
esac
done
# shift all \\"collected\\" parameters
shift \\$((OPTIND - 1))
# no command line argument supplied, print usage
[ \\$# -eq 0 ] && print_usage
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment