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 #!/bin/bash
FILE="./BANCO.txt" FILE="./BANCO.txt"
RE='^[0-9]+$'
pause() { pause() {
read -p "Pulse enter para continuar: " read -p "Pulse enter para continuar: "
} }
list() { 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 -e "\n----------------------------------"
cat $FILE | sort -t ';' -k3 echo "$TEXTO"
echo -e "----------------------------------\n" echo -e "----------------------------------\n"
} }
@ -19,19 +32,52 @@ fileCheck() {
addMove() { addMove() {
read -p "Nombre: " NOMBRE read -p "Nombre: " NOMBRE
#read -p "Fecha: " FECHA
FECHA=$(date -u +%Y-%m-%d_%H-%M-%S) FECHA=$(date -u +%Y-%m-%d_%H-%M-%S)
IMPORTE=
while [[ ! $IMPORTE =~ $RE ]]; do
read -p "Importe: " IMPORTE read -p "Importe: " IMPORTE
done
read -p "Descripción: " DESCRIPCION read -p "Descripción: " DESCRIPCION
NUM_MOV=$(findFreeID) NUM_MOV=$(findFreeID)
echo "$NUM_MOV;$NOMBRE;$FECHA;$IMPORTE;$DESCRIPCION" >> $FILE 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() { findFreeID() {
COUNT=0 COUNT=0
while true; do while true; do
LINE=$(grep "^$COUNT;*" $FILE) LINE=$(grep "^$COUNT;*" $FILE)
if [ -z $LINE ]; then if [ -z "$LINE" ]; then
echo $COUNT echo $COUNT
break break
fi fi
@ -40,27 +86,30 @@ findFreeID() {
} }
searchMove() { searchMove() {
ID=$1 SEARCH_TARGET=$1
echo -e "\n----------------------------------"
grep "^$ID;*" $FILE if ! checkExists $SEARCH_TARGET; then
echo -e "----------------------------------\n" formatear "Este movimiento no existe!"
return
fi
formatear "$(grep "^$SEARCH_TARGET;*" $FILE)"
} }
search() { search() {
read -p "ID de movimiento a buscar: " MOV read -p "ID de movimiento a buscar: " MOV
searchMove $MOV echo $MOV
}
listMoves() {
command ...
} }
calcTotal() { calcTotal() {
command ... TOTAL=0
} CALC_COL=$(cut -d ";" -f 4 $FILE)
deleteMove() { while read -r v; do
command ... TOTAL=$(($TOTAL + $v))
done<<<$CALC_COL
formatear "Tienes un total de $TOTAL€ en movimientos!"
} }
status() { status() {
@ -87,20 +136,25 @@ main() {
case "$OPT" in case "$OPT" in
"a") "a")
addMove addMove
pause
;; ;;
"b") "b")
search MOV=$(search)
searchMove "$MOV"
pause pause
;; ;;
"l") "l")
list list
pause pause
;; ;;
"a") "t")
command ... calcTotal
pause
;; ;;
"a") "d")
command ... MOV=$(search)
deleteMove "$MOV"
pause
;; ;;
"s") "s")
exit exit