Finish menu exercise

Can you tell I'm enthusiastic about having to use bash for anything
other than shell automation?
This commit is contained in:
raul 2025-01-15 19:15:18 +01:00
parent 2e92eaa0d1
commit 31813090de
Signed by: raul
GPG Key ID: C1AA797073F17129
1 changed files with 77 additions and 23 deletions

View File

@ -1,13 +1,26 @@
#!/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----------------------------------"
cat $FILE | sort -t ';' -k3
echo "$TEXTO"
echo -e "----------------------------------\n"
}
@ -19,48 +32,84 @@ fileCheck() {
addMove() {
read -p "Nombre: " NOMBRE
#read -p "Fecha: " FECHA
FECHA=$(date -u +%Y-%m-%d_%H-%M-%S)
read -p "Importe: " IMPORTE
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
if [ -z "$LINE" ]; then
echo $COUNT
break
fi
fi
let COUNT++
done
}
searchMove() {
ID=$1
echo -e "\n----------------------------------"
grep "^$ID;*" $FILE
echo -e "----------------------------------\n"
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
searchMove $MOV
}
listMoves() {
command ...
echo $MOV
}
calcTotal() {
command ...
}
TOTAL=0
CALC_COL=$(cut -d ";" -f 4 $FILE)
deleteMove() {
command ...
while read -r v; do
TOTAL=$(($TOTAL + $v))
done<<<$CALC_COL
formatear "Tienes un total de $TOTAL€ en movimientos!"
}
status() {
@ -87,20 +136,25 @@ main() {
case "$OPT" in
"a")
addMove
pause
;;
"b")
search
MOV=$(search)
searchMove "$MOV"
pause
;;
"l")
list
pause
;;
"a")
command ...
"t")
calcTotal
pause
;;
"a")
command ...
"d")
MOV=$(search)
deleteMove "$MOV"
pause
;;
"s")
exit