GNU/Linux >> Linux Esercitazione >  >> Linux

Ottieni lo script da eseguire di nuovo se l'input è Sì?

Sto mettendo insieme un semplice script di gestione dei file, funziona tutto bene tranne che alla fine, voglio che dica do you want to perform another action e se la risposta è yes quindi voglio che lo script ricominci di nuovo. So che ho bisogno di una sorta di loop qui? Ecco cosa ho:

#/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

Risposta accettata:

prima dell'eco "Seleziona un'azione..."

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

alla fine, sostituisci

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

di

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

done

echo "Exiting Program"
exit 0

quello che ho fatto è racchiudere il programma in un while [$condition ] do ; ... done .

Mi assicuro solo che la condizione fosse OK (answer=yes ) nel primo ciclo.


Linux
  1. Come ottenere il nome della distribuzione e il numero di versione in un semplice script di shell?

  2. Cron Job per verificare se lo script PHP è in esecuzione, in caso contrario eseguito?

  3. Esecuzione di script di shell in parallelo

  4. Esegui lo script bash come demone

  5. Esegui lo script bash dopo il login

Come eseguire uno script Python in PHP

Come eseguire uno script Bash

Linux – Come eseguire uno script sul blocco/sblocco dello schermo?

Linux:come eseguire uno script attivato da un input del joystick?

Script all'avvio?

Qual è il modo più veloce per eseguire uno script?