GNU/Linux >> Znalost Linux >  >> Linux

Znovu spustit skript, pokud je vstup Ano?

Dávám dohromady jednoduchý skript pro manipulaci se soubory, vše funguje dobře, až na konec, chci, aby řekl do you want to perform another action a pokud je odpověď yes pak chci, aby se skript spustil znovu. Vím, že tady potřebuji nějakou smyčku? Tady je to, co mám:

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

Přijatá odpověď:

před echo „Vyberte akci…“

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

nakonec nahradit

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

od

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

done

echo "Exiting Program"
exit 0

to, co jsem udělal, je uzavřít program do while [$condition ] do ; ... done .

Jen jsem se ujistil, že podmínka byla v pořádku (answer=yes ) v první smyčce.


Linux
  1. Jak spustit skript??

  2. Skrytí uživatelského vstupu na terminálu ve skriptu Linux

  3. Negovat podmínku if ve skriptu bash

  1. Po přihlášení spusťte bash skript

  2. Odpovězte ano na ssh-copy-id při prvním spuštění skriptem?

  3. spustit sieve na maildir

  1. Jak získat název distribuce a číslo verze v jednoduchém skriptu Shell?

  2. Linux – Jak spustit skript na obrazovce uzamčení/odemknutí?

  3. Úloha Cron zkontrolovat, zda skript PHP běží, pokud ne, pak spustit?