forked from evertramos/basescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header
79 lines (69 loc) · 2.96 KB
/
header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#-----------------------------------------------------------------------
#
# Basescript function
#
# The basescript functions were designed to work as abstract function,
# so it could be used in many different contexts executing specific job
# always remembering Unix concept DOTADIW - "Do One Thing And Do It Well"
#
# Developed by
# Evert Ramos <[email protected]>
#
# Copyright Evert Ramos
#
#-----------------------------------------------------------------------
#
# Be careful when editing this file, it is part of a bigger script!
#
# Basescript - https://github.com/evertramos/basescript
#
#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
# This function has one main objective:
# 1.
#
# You must/might inform the argument(s) below:
# 1.
# 2. [optional] (default: )
# d1. [optional] (default: true) Stop execution on error
# d2. [optional] (default: true) Allow run with 'sudo'
#
# [CAUTION] The optional argument(s) replaces previously parameter as of:
# d1. STOP_EXECUTION_ON_ERROR
# d2. ALLOW_RUN_WITH_SUDO
#
#-----------------------------------------------------------------------
function_name()
{
# Local variables
local LOCAL_ LOCAL_STOP_EXECUTION_ON_ERROR LOCAL_ALLOW_RUN_WITH_SUDO LOCAL_RUN_WITH_SUDO
# Required arguments
LOCAL_=${1:-null}
# Optional arguments
LOCAL_STOP_EXECUTION_ON_ERROR="${d1:-null}" && [[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == null ]] && LOCAL_STOP_EXECUTION_ON_ERROR=${STOP_EXECUTION_ON_ERROR:-true}
# LOCAL_ALLOW_RUN_WITH_SUDO="${d1:-null}" && [[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == null ]] && LOCAL_ALLOW_RUN_WITH_SUDO=${ALLOW_RUN_WITH_SUDO:-true}
# Validate required parameters
if [[ $LOCAL_ == "" || $LOCAL_ == null ]]; then
if [[ ! $(declare -F echoerror) == "" ]]; then
echoerror "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'" ${LOCAL_STOP_EXECUTION_ON_ERROR}
else
echo "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'"
[[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == true ]] && exit 1
fi
fi
# Show debug message
if [[ "$DEBUG" == true ]]; then
if [[ ! $(declare -F echowarning) == "" ]]; then
echowarning "You are running..... xyz - [function: ${FUNCNAME[0]}]"
else
echo "You are running..... xyz - [function: ${FUNCNAME[0]}]"
fi
fi
# [EXAMPLE] Run a function with 'sudo'
# Allows 'sudo' to run in this function with a condition
# [[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == true ]] && LOCAL_RUN_WITH_SUDO=sudo
# $LOCAL_RUN_WITH_SUDO mkdir -p
# [CONDITION] You may add a condition to check if it requires to run a command with 'sudo'
# [[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == true ]] && ! system_check_user_folder_owner ${LOCAL_FOLDER%/*} && LOCAL_RUN_WITH_SUDO=sudo
# $LOCAL_RUN_WITH_SUDO mkdir -p $LOCAL_FOLDER > /dev/null 2>&1
}