Skip to content

Commit 958be03

Browse files
authored
Merge pull request mouredev#6260 from h4ckxel/h4ckxel
#11 - Bash
2 parents aa5f5b0 + 491b629 commit 958be03

File tree

3 files changed

+657
-0
lines changed

3 files changed

+657
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/bin/bash
2+
3+
# Crear archivo con información personal
4+
function crear_archivo() {
5+
file="h4ckxel.txt"
6+
f=("Me llamo H4ckxel" "Mi edad es de 23 años" "Mi lenguaje de programacion favorito es Python/Bash")
7+
printf "%s\n" "${f[@]}" > "$file"
8+
echo -e "\nEl archivo h4ckxel.txt ha sido creado\n"
9+
}
10+
11+
# Leer el contenido del archivo
12+
function leer_archivo() {
13+
cat "h4ckxel.txt"
14+
}
15+
16+
# Borrar archivo
17+
function borrar_archivo() {
18+
if [ -e "h4ckxel.txt" ]; then
19+
rm "h4ckxel.txt"
20+
echo -e "\nEl archivo 'h4ckxel.txt' ha sido eliminado\n"
21+
else
22+
echo -e "\nEl archivo no existe\n"
23+
fi
24+
}
25+
26+
crear_archivo
27+
leer_archivo
28+
borrar_archivo
29+
30+
# Parte extra: Gestión de ventas
31+
store="sales.txt"
32+
33+
# Añadir producto
34+
function add_product() {
35+
read -p "Nombre: " name
36+
read -p "Cantidad vendida: " quantity
37+
read -p "Precio: " price
38+
echo "$name, $quantity, $price" >> "$store"
39+
}
40+
41+
# Consultar producto
42+
function consult_product() {
43+
read -p "Nombre: " name
44+
grep -i "^$name," "$store" || echo "Producto no encontrado"
45+
}
46+
47+
# Eliminar producto
48+
function delete_product() {
49+
read -p "Nombre: " name
50+
sed -i "/^$name,/d" "$store"
51+
}
52+
53+
# Actualizar producto
54+
function update_product() {
55+
delete_product
56+
add_product
57+
}
58+
59+
# Leer archivo completo
60+
function read_file() {
61+
cat "$store"
62+
}
63+
64+
# Calcular ventas por producto
65+
function product_sales() {
66+
total=0
67+
read -p "Nombre: " name
68+
while IFS=, read -r prod quantity price; do
69+
if [[ "$prod" == "$name" ]]; then
70+
total=$(echo "$quantity * $price" | bc)
71+
echo -e "\nEl total de las ventas de $name es: $total\n"
72+
return
73+
fi
74+
done < "$store"
75+
echo "Producto no encontrado"
76+
}
77+
78+
# Calcular ventas totales
79+
function total_sales() {
80+
total=0
81+
while IFS=, read -r _ quantity price; do
82+
total=$(echo "$total + $quantity * $price" | bc)
83+
done < "$store"
84+
echo -e "\nEl total de las ventas es: $total\n"
85+
}
86+
87+
# Salir y borrar archivo de ventas
88+
function exit_program() {
89+
rm -f "$store"
90+
echo -e "[!] Archivo 'sales.txt' eliminado. Saliendo del programa...\n"
91+
}
92+
93+
# Menú de opciones
94+
while true; do
95+
echo -e "\n--- Menú de gestión de ventas ---"
96+
echo "1) Añadir producto"
97+
echo "2) Consultar producto"
98+
echo "3) Eliminar producto"
99+
echo "4) Actualizar producto"
100+
echo "5) Consultar archivo completo"
101+
echo "6) Calcular ventas por producto"
102+
echo "7) Calcular ventas totales"
103+
echo "8) Salir"
104+
105+
read -p "Elige una opción: " option
106+
107+
case $option in
108+
1) add_product ;;
109+
2) consult_product ;;
110+
3) delete_product ;;
111+
4) update_product ;;
112+
5) read_file ;;
113+
6) product_sales ;;
114+
7) total_sales ;;
115+
8) exit_program; break ;;
116+
*) echo "Opción no válida, intenta de nuevo" ;;
117+
esac
118+
done

0 commit comments

Comments
 (0)