#!/bin/bash
# Title:
# Version: 0.0 a
# Author: Frédéric CHEVALIER <fcheval@txbiomed.org>
# Created in: 20xx-
# Modified in:
# Licence : GPL v3
#======#
# Aims #
#======#
aim=""
#==========#
# Versions #
#==========#
# v0.0 - 20xx-xx-xx: creation
version=$(grep -i -m 1 "version" "$0" | cut -d ":" -f 2 | sed "s/^ *//g")
#===========#
# Functions #
#===========#
# Usage message
function usage {
echo -e "
\e[32m ${0##*/} \e[00m -i|--in file -o|--out value -v|--val value -s|--switch -h|--help
Aim: $aim
Version: $version
Options:
-i, --in input file
-o, --out name of output file
-v, --val value
-s, --switch switch
-h, --help this message
"
}
# Info message
function info {
if [[ -t 1 ]]
then
echo -e "\e[32mInfo:\e[00m $1"
else
echo -e "Info: $1"
fi
}
# Warning message
function warning {
if [[ -t 1 ]]
then
echo -e "\e[33mWarning:\e[00m $1"
else
echo -e "Warning: $1"
fi
}
# Error message
## usage: error "message" exit_code
## exit code optional (no exit allowing downstream steps)
function error {
if [[ -t 1 ]]
then
echo -e "\e[31mError:\e[00m $1"
else
echo -e "Error: $1"
fi
if [[ -n $2 ]]
then
exit $2
fi
}
# Dependency test
function test_dep {
which $1 &> /dev/null
if [[ $? != 0 ]]
then
error "Package $1 is needed. Exiting..." 1
fi
}
# Progress bar
## Usage: ProgressBar $mystep $myend
function ProgressBar {
if [[ -t 1 ]]
then
# Process data
let _progress=(${1}*100/${2}*100)/100
let _done=(${_progress}*4)/10
let _left=40-$_done
# Build progressbar string lengths
_fill=$(printf "%${_done}s")
_empty=$(printf "%${_left}s")
# Build progressbar strings and print the ProgressBar line
# Output example:
# Progress : [########################################] 100%
#printf "\rProgress : [${_fill// /=}${_empty// / }] ${_progress}%%"
printf "\r\e[32mProgress:\e[00m [${_fill// /=}${_empty// / }] ${_progress}%%"
[[ ${_progress} == 100 ]] && echo ""
fi
}
# Clean up function for trap command
## Usage: clean_up file1 file2 ...
function clean_up {
rm -rf $@
exit 1
}
#==============#
# Dependencies #
#==============#
test_dep
#===========#
# Variables #
#===========#
# Options
while [[ $# -gt 0 ]]
do
case $1 in
-i|--in ) input="$2" ; shift 2
while [[ ! -z "$1" && $(echo "$1"\ | grep -qv "^-" ; echo $?) == 0 ]]
do
input=$(echo "$input" "$1")
shift
done ;;
-o|--out ) output="$2" ; shift 2 ;;
-v|--val ) v="$2" ; shift 2 ;;
-s|--switch ) switch=1 ; shift ;;
-h|--help ) usage ; exit 0 ;;
* ) error "Invalid option: $1\n$(usage)" 1 ;;
esac
done
# Check the existence of obligatory options
if [[ -z "$input" ]]
then
error "The option input is required. Exiting...\n$(usage)" 1
fi
if [[ -z "$output" ]]
then
output=
fi
#============#
# Processing #
#============#
#-------------#
# Sub-section #
#-------------#
# Progress bar
for i in myvar
do
ProgressBar $mystep $myend
done
# Trap function
##
for i in {1..3} ; do
cmd &
mypid="$mypid $!"
done
# OR
cmd &
mypid=$!
##
trap "kill $mypid ; clean_up $tmp1 $tmp2" SIGINT SIGTERM # Clean_up function to remove tmp files
wait
exit 0