API
assertNot
Check if last command result (passed in input). If result is equal to res_value
param print message to stdout and exit with value 1.
Parameters:
$1: result value of the command
$2: check if result value is equal to this value. (numeric value)
$3: message to print if result is not equal to zero
(Show/Hide)assertNot () {
local result=$1
local inv_res=$2
if [ $result -eq $inv_res ] ; then
echo -en "$3\n"
exit 1
fi
}
error_handled
Check if last command result. If result is not equal to 0 then
print a message to stdout and exit with value 1.
Parameters:
$1: message to print if result is not equal to zero.
(Show/Hide)error_handled () {
local result=$?
if [ $result -ne 0 ] ; then
echo -en "$1\n"
exit 1
fi
}
error_generate
Print input message and exit with value 1 if second parameter is not equal to 0.
Parameters:
$1: message to print on stdout.
$2: optional field to avoid call to exit program if value is equal to 0.
(Show/Hide)error_handled () {
local result=$?
if [ $result -ne 0 ] ; then
echo -en "$1\n"
exit 1
fi
}
check_var
Check if variable with name in input contains a string or not.
Parameters:
$1: name of the variable to check.
Returns:
1: length of the variable is zero
0: length of the variable is not zero
(Show/Hide)check_var () {
local var=$1
eval v=\$$var
if [ -z $v ] ; then
return 1
fi
return 0
}
escape_var
Escape content of the variable with input name.
Parameters:
$1: name of the variable to check.
Returns:
(Show/Hide)escape_var () {
local name="$1"
eval v=\$$name
v="$(echo "$v" | sed -e 's/\//\\\//g' -e 's:\\:\\\\:g' -e 's/\&/\\\&/g' -e 's:`:\\\`:g' )"
eval "$name=\$v"
return 0
}
escape2oct_var
Escape content of the variable with input name like escape_var with octets ascii code.
Parameters:
$1: name of the variable to check.
Returns:
(Show/Hide)escape_var () {
local name="$1"
eval v=\$$name
v="$(echo "$v" | sed -e 's/\//\\\//g' -e 's:\\:\\\\:g' -e 's/\&/\\\&/g' -e 's:`:\\\`:g' )"
eval "$name=\$v"
return 0
}
confirmation_question
Use to generate an input question and manage response.
Parameters:
$1: message with question for user.
Returns:
0: if user answer is yes.
1: if if user answer is no.
2: if user answer empty.
(Show/Hide)escape2oct_var () {
local name="$1"
eval v=\$$name
# TODO: Handle replace through input params selection
v="$(echo "$v" | sed -e 's:\/:\\057:g' -e 's:`:\\0140:g' )"
#v="$(echo "$v" | sed -e 's:\\n:\\012:g' -e 's:\/:\\057:g' -e 's:\\:\\0134:g' -e 's:`:\\0140:g' )"
#v="$(echo "$v" | sed -e 's:\\n:\\012:g' -e 's:\/:\\057:g' -e 's:\\:\\0134:g' -e 's:`:\\0140:g' )"
eval "$name=\$v"
return 0
}
push_spaces
Push spaces to stdout.
Parameters:
$1: Number of spaces to write.
Returns:
(Show/Hide)push_spaces () {
local n_spaces=$1
if [ $n_spaces -gt 0 ] ; then
for ((i=0; i<$n_spaces; i++))
do
echo -en " "
done
fi
return 0
}
get_space_str
Create a string with input "str" param at begin and N spaces
where N is equal to max_chars - ${#str}
Parameters:
$1: (var_name) Name of variable where save string with spaces.
$2: (max_chars) Number of max chars of the string with spaces.
$3: (str) String to insert at begin of save string.
$4: (pre_spaces) Number of spaces to add before str. (Optional. default 0).
Returns:
0: on success
1: on error
(Show/Hide)get_space_str () {
local var_name="$1"
local max_chars="$2"
local str="$3"
local pre_spaces="$4"
[[ -z "$max_chars" || -z "$var_name" ]] && return 1
local n_spaces="$((${max_chars} - ${#str}))"
if [ -n "${pre_spaces}" ] ; then
for ((i=0; i<${pre_spaces}; i++)) ; do
str=" ${str}"
done
fi
if [ $n_spaces -gt 0 ] ; then
for ((i=0; i<$n_spaces; i++)) ; do
str="${str} "
done
fi
eval "$var_name=\$str"
return 0
}
commons_exists_prog
Check if a program is available on current PATH.
Parameters:
$1: (program) Name of the program.
$2: (options) Override default (-v) option on check presence. Optional.
Returns:
0: if program exists
1: if program doesn't exists or invalid input params.
(Show/Hide)commons_exists_prog () {
local prog="$1"
local opts="$2"
local check_opts="-v"
local ans=0
[ -z "${prog}" ] && return 1
if [ -n "${opts}" ] ; then
check_opts="${opts}"
fi
${prog} ${check_opts} > /dev/null 2>&1
if [ $? -ne 0 ] ; then
ans=1
fi
return $ans
}