167 lines
3.1 KiB
Bash
Executable File
167 lines
3.1 KiB
Bash
Executable File
#!/bin/bash
|
|
FILE="./BANCO.txt"
|
|
RE='^[0-9]+$'
|
|
|
|
pause() {
|
|
read -p "Pulse enter para continuar: "
|
|
}
|
|
|
|
list() {
|
|
FILE_CONTENTS="$(cat $FILE | sort -t ';' -k3)"
|
|
if ! checkExists $FILE_CONTENTS; then
|
|
formatear "Tu cuenta no tiene ningún movimiento!"
|
|
return
|
|
fi
|
|
|
|
formatear "$FILE_CONTENTS"
|
|
}
|
|
|
|
formatear() {
|
|
TEXTO="$1"
|
|
|
|
echo -e "\n----------------------------------"
|
|
echo "$TEXTO"
|
|
echo -e "----------------------------------\n"
|
|
}
|
|
|
|
fileCheck() {
|
|
if [[ ! -f "$FILE" ]]; then
|
|
touch ./BANCO.txt
|
|
fi
|
|
}
|
|
|
|
addMove() {
|
|
read -p "Nombre: " NOMBRE
|
|
FECHA=$(date -u +%Y-%m-%d_%H-%M-%S)
|
|
IMPORTE=
|
|
|
|
while [[ ! $IMPORTE =~ $RE ]]; do
|
|
read -p "Importe: " IMPORTE
|
|
done
|
|
|
|
read -p "Descripción: " DESCRIPCION
|
|
|
|
NUM_MOV=$(findFreeID)
|
|
echo "$NUM_MOV;$NOMBRE;$FECHA;$IMPORTE;$DESCRIPCION" >> $FILE
|
|
formatear "Movimiento exitosamente añadido!"
|
|
}
|
|
|
|
checkExists() {
|
|
CHECK_TARGET="$1"
|
|
TARGET_CONTENTS=$(grep "^$1;*" $FILE)
|
|
|
|
if [[ -z $TARGET_CONTENTS ]]; then
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
deleteMove() {
|
|
DELETE_TARGET="$1"
|
|
|
|
if ! checkExists $DELETE_TARGET; then
|
|
formatear "Este movimiento no existe!"
|
|
return
|
|
fi
|
|
|
|
formatear "$TARGET_CONTENTS"
|
|
read -p "Desea borrar este movimiento? [y/N] " ELEC
|
|
if [[ $ELEC != "y" ]]; then
|
|
return
|
|
fi
|
|
|
|
sed -i "/^$DELETE_TARGET;/d" $FILE
|
|
formatear "Movimiento exitosamente borrado!"
|
|
}
|
|
|
|
findFreeID() {
|
|
COUNT=0
|
|
while true; do
|
|
LINE=$(grep "^$COUNT;*" $FILE)
|
|
if [ -z "$LINE" ]; then
|
|
echo $COUNT
|
|
break
|
|
fi
|
|
let COUNT++
|
|
done
|
|
}
|
|
|
|
searchMove() {
|
|
SEARCH_TARGET=$1
|
|
|
|
if ! checkExists $SEARCH_TARGET; then
|
|
formatear "Este movimiento no existe!"
|
|
return
|
|
fi
|
|
|
|
formatear "$(grep "^$SEARCH_TARGET;*" $FILE)"
|
|
}
|
|
|
|
search() {
|
|
read -p "ID de movimiento a buscar: " MOV
|
|
echo $MOV
|
|
}
|
|
|
|
calcTotal() {
|
|
TOTAL=0
|
|
CALC_COL=$(cut -d ";" -f 4 $FILE)
|
|
|
|
while read -r v; do
|
|
TOTAL=$(($TOTAL + $v))
|
|
done<<<$CALC_COL
|
|
|
|
formatear "Tienes un total de $TOTAL€ en movimientos!"
|
|
}
|
|
|
|
status() {
|
|
clear
|
|
cat <<'END_CAT'
|
|
+-----------------------------------------------------------------+
|
|
| MENU DEL BANCO |
|
|
+-----------------------------------------------------------------+
|
|
| a - Añadir un movimiento bancario. |
|
|
| b - Buscar un movimiento bancario por NUM_MOV. |
|
|
| l - Listar todos los movimientos bancarios ordenados por fecha. |
|
|
| t - Calcular el saldo total de la cuenta. |
|
|
| d - Eliminar movimiento bancario. |
|
|
| s - Salir del programa. |
|
|
+-----------------------------------------------------------------+
|
|
END_CAT
|
|
}
|
|
|
|
main() {
|
|
fileCheck
|
|
while true; do
|
|
status
|
|
read -p "Opción: " OPT
|
|
case "$OPT" in
|
|
"a")
|
|
addMove
|
|
pause
|
|
;;
|
|
"b")
|
|
MOV=$(search)
|
|
searchMove "$MOV"
|
|
pause
|
|
;;
|
|
"l")
|
|
list
|
|
pause
|
|
;;
|
|
"t")
|
|
calcTotal
|
|
pause
|
|
;;
|
|
"d")
|
|
MOV=$(search)
|
|
deleteMove "$MOV"
|
|
pause
|
|
;;
|
|
"s")
|
|
exit
|
|
;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
main
|