Created
August 9, 2017 18:27
-
-
Save odurc/5c70d3063e3e1154bf14f1820ba1c90b to your computer and use it in GitHub Desktop.
useful functions for bash scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# print green | |
function info { | |
echo -e "\e[0;32m"$@"\e[0m" | |
} | |
# print yellow | |
function warn { | |
echo -e "\e[0;33m"$@"\e[0m" | |
} | |
# print red | |
function error { | |
echo -e "\e[0;31m"$@"\e[0m" | |
} | |
# command line parser | |
function parse_cmd_line { | |
# first argument of parse_cmd_line function must be a string space-separated | |
# containing the valid positional arguments | |
# second argument must be a string space-separated containing the valid | |
# optional arguments | |
# third argument must be "$@" | |
# | |
# Example: | |
# parse_cmd_line "<arg1> <arg2> [opt1]" "--flag1 --flag2=10" bla ble bli --flag1 | |
# | |
# after executing this call the following variables are created? | |
# arg1=bla | |
# arg2=ble | |
# opt1=bli | |
# flag1=1 | |
# flag2=10 | |
# get arguments and convert them to array | |
local pos_args=($1) | |
shift | |
local opt_args=($1) | |
shift | |
# make variables local | |
local arg="" | |
local opt="" | |
local var="" | |
local val="" | |
# create variables of optional arguments with default values | |
for opt in ${opt_args[@]}; do | |
if [[ "$opt" == *"="* ]]; then | |
# extract variable name and its value | |
var=`echo ${opt:2} | cut -d'=' -f1 | sed 's/-/_/g'` | |
val=`echo ${opt:2} | cut -d'=' -f2` | |
# create variable | |
eval `echo $var=$val` | |
fi | |
done | |
# loop all arguments | |
local recv_count=0 | |
for arg in $@; do | |
# check if it's an "OPTIONS" argument | |
if [[ "${arg::2}" == "--" ]]; then | |
# check if received option is valid | |
for opt in ${opt_args[@]}; do | |
# get variable name replacing dash by underscore | |
var=`echo ${arg:2} | cut -d'=' -f1 | sed 's/-/_/g'` | |
if [[ `echo $opt | cut -d'=' -f1` == \ | |
`echo $arg | cut -d'=' -f1` ]]; then | |
# check if a value has been provided | |
if [[ "$arg" == *"="* ]]; then | |
val=`echo ${arg:2} | cut -d'=' -f2` | |
# force variable to have a value | |
else | |
val="1" | |
fi | |
# create variable | |
eval `echo $var=$val` | |
break | |
fi | |
done | |
else | |
# create variables of the positional arguments | |
var=`echo ${pos_args[$recv_count]} | sed 's/[]<>[]//g'` | |
eval `echo $var=$arg` | |
recv_count=$((recv_count+1)) | |
fi | |
done | |
# count required arguments | |
local req_count=0 | |
for arg in ${pos_args[@]}; do | |
if [[ "${arg::1}" == "<" && "${arg: -1}" == ">" ]]; then | |
req_count=$((req_count+1)) | |
fi | |
done | |
# check if all required arguments were received | |
if [[ "$recv_count" < "$req_count" ]]; then | |
ret=1 | |
else | |
ret=0 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment