From b98a284944b435252acedc7d8e0ede01bc5b406a Mon Sep 17 00:00:00 2001 From: raul Date: Wed, 15 Jan 2025 19:52:08 +0100 Subject: [PATCH] Rearrange repo --- class/scripts2/banco.sh | 166 ++++++++++++++++++ .../scripts2/guess_number.sh | 12 +- script1.sh | 29 +++ 3 files changed, 205 insertions(+), 2 deletions(-) create mode 100755 class/scripts2/banco.sh rename guess_number.sh => class/scripts2/guess_number.sh (89%) create mode 100755 script1.sh diff --git a/class/scripts2/banco.sh b/class/scripts2/banco.sh new file mode 100755 index 0000000..3368d89 --- /dev/null +++ b/class/scripts2/banco.sh @@ -0,0 +1,166 @@ +#!/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 diff --git a/guess_number.sh b/class/scripts2/guess_number.sh similarity index 89% rename from guess_number.sh rename to class/scripts2/guess_number.sh index 4d8e5c9..57d7b2d 100755 --- a/guess_number.sh +++ b/class/scripts2/guess_number.sh @@ -2,6 +2,7 @@ RANDOM_NUM=0 ATTEMPTS_NUM=7 POOL_NUM=100 +RE='^[0-9]+$' usage () { @@ -67,10 +68,17 @@ checkLose() { main() { RANDOM_NUM=$(seq 0 $POOL_NUM | tr " " "\n" | shuf -n1) - while true; do + for (( ; ; )); do + status checkLose - read -p "Número: " num_attempt + + + num_attempt="-1" + while [[ ! $num_attempt =~ $RE || $num_attempt > $POOL_NUM ]]; do + read -p "Número: " num_attempt + done + checkWin $num_attempt read -p "Pulse enter para continuar: " done diff --git a/script1.sh b/script1.sh new file mode 100755 index 0000000..adc9da6 --- /dev/null +++ b/script1.sh @@ -0,0 +1,29 @@ +#!/bin/bash +USUARIO=$1 +TITULOS=( "Nombre" "Contraseña" "UID" "GID" "Comentario" "Directorio Home" "Shell" ) + +# if [[ -z $USUARIO ]]; then +# echo "Usage: ./script1.sh root" +# exit +# fi +# + +getUserInfo() { + USER=$1 + IFS=':' + COUNT=0 + for v in $USER; do + echo "${TITULOS[$COUNT]}: $v" + let COUNT++ + done +} + +main() { + while read -r v; do + # I hate this + getUserInfo "$v" + echo "--------------------" + done