-
Notifications
You must be signed in to change notification settings - Fork 0
/
Template.sh
182 lines (156 loc) · 4.58 KB
/
Template.sh
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/sh
#===============================================================================
# CREATION
# Date de creation : 08/09/87
# Auteur : jdesesquelles
# Version : V1.0
#===============================================================================
#
# Commentaire complet du script.
#
#===============================================================================
# ------------------------------------------------------------------------------
# VARIABLES: globales
# ------------------------------------------------------------------------------
DATE=`date +"%Y%m%d"`
HOME_PATH=${HOME}
LOG_FILE="${HOME_PATH}/template-$DATE.log"
BIN_PATH="/usr/bin"
FILE_1="/tmp/test1.txt"
FILE_2="/tmp/test2.txt"
ARG_NBR="$#"
ARG_1="$1"
ARG_2="$2"
# ------------------------------------------------------------------------------
# FONCTION: Message et log
# ------------------------------------------------------------------------------
displayMessage() {
echo "$*"
}
displayError() {
echo -e "\r\e[0;31m* $* *\e[0m"
# exit 1
}
displayTitle() {
displayMessage "------------------------------------------------------------------------------"
displayMessage "$*"
displayMessage "------------------------------------------------------------------------------"
}
# Parametres: MESSAGE COMMAND
displayAndExec() {
local message=$1
echo -n "[En cours] $message"
shift
echo ">>> $*" >> $LOG_FILE 2>&1
sh -c "$*" >> $LOG_FILE 2>&1
local ret=$?
if [ $ret -ne 0 ]; then
echo -e "\r\e[0;31m [ ERROR ]\e[0m $message"
else
echo -e "\r\e[0;32m [ OK ]\e[0m $message"
fi
return $ret
}
# ------------------------------------------------------------------------------
# FUNCTION: test root
# ------------------------------------------------------------------------------
function testRoot() {
#if [ "$(id -u)" != "0" ]; then
if [ "$(id -u)" = "0" ]; then
# displayError "Vous n'etes pas root."
displayError "Merci de ne pas utiliser de droit root."
exit 1
fi
}
# ------------------------------------------------------------------------------
# FUNCTION: Test bin
# ------------------------------------------------------------------------------
function testBin() {
if [ ! -d "$BIN_PATH" ];then
displayError "Repertoire: \"$BIN_PATH\" introuvable."
exit 1
fi
}
# ------------------------------------------------------------------------------
# FUNCTION: Test file + source
# ------------------------------------------------------------------------------
function testFile() {
if [[ ! -e "$1" ]]; then
displayError "Fichier: \"$(basename $1)\" absent."
exit 1
fi
}
function sourceFile() {
testFile $1
source $1
}
# ------------------------------------------------------------------------------
# FUNCTION: Usage
# ------------------------------------------------------------------------------
function usage() {
#if [ -z "$ARG_1" ] || [ -z "$ARG_2" ]; then
if [ "$ARG_NBR" -ne 2 ]; then
echo -e "\r\e[0;31m* Argument(s) manquant *\e[0m"
echo
echo "Info:"
echo -e "\tScript pour bla bla bla..."
echo
echo "Usage:"
echo -e "\t$(basename $0) -p <product_id> -g <g0r0c0>"
echo
exit 1
fi
}
# ------------------------------------------------------------------------------
# FUNCTION: Choix (ALL)
# ------------------------------------------------------------------------------
function allProduct() {
displayAndExec "pause 5s" "sleep 5"
displayAndExec "pause 4s" "sleep 4"
displayAndExec "pause 3s" "sleep 3"
displayAndExec "pause 2s" "sleep 2"
displayAndExec "pause 1s" "sleep 1"
}
# ------------------------------------------------------------------------------
# FUNCTION: Choix (LIST)
# ------------------------------------------------------------------------------
function productChoice() {
sleep 1
}
# ------------------------------------------------------------------------------
# FUNCTION: Menu
# ------------------------------------------------------------------------------
main() {
#clear
echo
displayTitle "Ceci est un Titre"
echo -e "Selection de parmi le menu suivant:"
echo -e "\tALL : choix1"
echo -e "\tLIST : choix2"
echo
echo -e -n "Entrez votre choix [ ALL | LIST ] : "
read METHOD
case $METHOD in
ALL|all )
allProduct
;;
LIST|list )
productChoice
;;
* )
displayError "reponse incorrect"
# main
;;
esac
}
# ------------------------------------------------------------------------------
# START
# ------------------------------------------------------------------------------
#---
usage
testRoot
testBin
testFile $FILE_1
#---
main
exit 0