GNU/Linux >> Tutoriales Linux >  >> Linux

¿Obtener la secuencia de comandos para que se ejecute de nuevo si la entrada es Sí?

Estoy armando un script simple de manejo de archivos, todo funciona bien excepto al final, quiero que diga do you want to perform another action y si la respuesta es yes entonces quiero que el script comience de nuevo. Sé que necesito algún tipo de bucle aquí? Esto es lo que tengo:

#/bin/bash


echo "Select an option from copy , remove , rename , linking"
#read in user input into the action variable

read action

# if action is copy then continue proceed with the following
if [ $action = "copy" ]
then
echo "Please enter the filename you wish to copy"
read filename
# check if filename exists, if it doesn't then exit program
    if [ ! -e  $filename ] 
    then
    echo "$filename does not exist"
    exit 1
    fi
echo "Please enter the new filename"
read filenamenew
cp $filename $filenamenew
echo "$filename has been copied to $filenamenew"

# if action is remove then continue proceed with the following
elif [ $action = "remove" ]
then
echo "Please enter the filename you wish to remove"
read filename
# check if filename exists, if it doesn't then exit program
    if [ ! -e  $filename ] 
    then
    echo "$filename does not exist"
    exit 1
    fi
rm -rf $filename
echo "$filename has been deleted"

# if action is rename then continue proceed with the following
elif [ $action = "rename" ]
then
echo "Please enter the filename you wish to rename"
read filename
# check if filename exists, if it doesn't then exit program
    if [ ! -e  $filename ] 
    then
    echo "$filename does not exist"
    exit 1
    fi
echo "Please enter the new filename"
read filenamenew
mv $filename $filenamenew
echo "$filename has been renamed to $filenamenew"
fi

echo "Do you want to perform another file operation (yes/no) ?"
read answer

if [ $answer = yes ]
then "run script again"
exit 0
    elif [ $answer = no ]
    then echo "Exiting Program"
    exit 0
    fi
fi

Respuesta aceptada:

antes de echo “Seleccione una acción…”

answer=yes
while [ "$answer" = yes ]
do

al final, reemplaza

if [ $answer = yes ]
then "run script again"
exit 0
    elif [ $answer = no ]
    then echo "Exiting Program"
    exit 0
    fi
fi

por

if [ "$answer" = yes ]
then "run script again"
fi

done

echo "Exiting Program"
exit 0

lo que hice fue encerrar el programa en un while [$condition ] do ; ... done .

Solo me aseguro de que la condición fuera correcta (answer=yes ) en el primer ciclo.


Linux
  1. ¿Cómo obtener el nombre de distribución y el número de versión en un script de Shell simple?

  2. ¿Trabajo cron para verificar si el script Php se está ejecutando, si no, entonces ejecutar?

  3. ¿Cómo solicito la entrada Sí/No/Cancelar en un script de shell de Linux?

  4. Ocultar la entrada del usuario en la terminal en el script de Linux

  5. Ejecutar script bash como demonio

Cómo ejecutar un script de Python en PHP

Cómo ejecutar un script Bash

Linux:¿cómo ejecutar un script en el bloqueo/desbloqueo de la pantalla?

Linux:¿cómo ejecutar un script activado por una entrada de joystick?

¿Script al inicio?

¿Cuál es la forma más rápida de ejecutar un script?