-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
executable file
·141 lines (115 loc) · 4.25 KB
/
utils.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
#!/bin/bash
#aqui colocaremos las funciones recurrentes , para llamarlas directamente desde este archivo
#++++++++++++++++++++++++++++++++++++++++++
# funcion que verifica si somos root
#++++++++++++++++++++++++++++++++++++++++++
function es_root(){
if [ `id -u` -ne 0 ];then
return 1
elif [ `id -u` -eq 0 ];then
return 0
else
return 2 #un error inesperado
fi
}
msj_es_root(){
#funcion que trae a es root y muestra msj acorde a lo q suceda ya que muchas veces solo necesitamos el msj y el retorno y otras solo el retorno
es_root
r1=$?
if [ $r1 -eq 0 ];then
echo "+-------------------+"
echo "| usted es root |"
echo "+-------------------+"
return 0
elif [ $r1 == 1 ];then
echo "+----------------------+"
echo "| usted no es root |"
echo "+----------------------+"
return 1
else
echo "+----------------------+"
echo "| error inesperado |"
echo "+----------------------+"
return 2
fi
}
function estaInstalado(){
value=$1
dpkg -s $value &>/dev/null
if [ $? -eq 0 ];then
return 0
#el programa se encuentra instalado
else
return 1
#sino se encuentra instalado devolveraqa 1
fi
}
function instalacion(){
value=$1
estaInstalado $value
estaInstalado=$?
if [ $estaInstalado -eq 1 ] ;then
read -p "desea instalar el programa?? 'y'/'n' " respuesta
if [[ $respuesta == "y" ]];then
apt-get update -y
apt-get install $value -y
if [ $? -eq 0 ];then
echo "+----------------------------------------+"
echo "| $value instalado correctamente |"
echo "+----------------------------------------+"
return 0
else
echo "+---------------------------------------------------+"
echo "| ocurrio un error a la hora de la instalacion |"
echo "+---------------------------------------------------+"
return 3
fi
elif [[ $respuesta == "n" ]];then
echo "+----------------------------------------------+"
echo "| ha seleccionado no instalar $value |"
echo "+----------------------------------------------+"
return 2
fi
elif [ $estaInstalado -eq 0 ];then
echo "+----------------------------------------+"
echo "| $value ya esta en su sistema |"
echo "+----------------------------------------+"
return 0
else
echo "+---------------------------------------------------+"
echo "| ocurrio un error a la hora de la instalacion |"
echo "+---------------------------------------------------+"
return 3
fi
}
function warning() {
START='\033[0;31m' #color rojo para el msj incorrecto
END='\033[0;00m' # verde para la confirmacion de 1 a 5
MESSAGE=${@:-""}
echo -e "${START}${MESSAGE}${END}"
sleep 3
}
#++++++++++++++++++++++++++++++++++++++++++++++
# funcion que verifica si una ip es correcta
#+++++++++++++++++++++++++++++++++++++++++++++
function valid_ip(){
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
#En ~, es parte del operador =~ que realiza una coincidencia de expresión regular de la cadena a su izquierda con la expresión regular extendida a su derecha.
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
export -f warning
export -f instalacion
export -f estaInstalado
export -f es_root
export -f msj_es_root
export -f valid_ip