Ich stelle ein einfaches Dateihandhabungsskript zusammen, es funktioniert alles gut, außer am Ende möchte ich, dass es sagt do you want to perform another action
und wenn die Antwort yes
ist dann möchte ich, dass das Skript erneut gestartet wird. Ich weiß, ich brauche hier eine Art Schleife? Folgendes habe ich:
#/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
Akzeptierte Antwort:
vor echo „Wählen Sie eine Aktion …“
answer=yes
while [ "$answer" = yes ]
do
am Ende ersetzen
if [ $answer = yes ]
then "run script again"
exit 0
elif [ $answer = no ]
then echo "Exiting Program"
exit 0
fi
fi
von
if [ "$answer" = yes ]
then "run script again"
fi
done
echo "Exiting Program"
exit 0
Was ich getan habe, ist das Programm in einen while [$condition ] do ; ... done
.
Ich stelle nur sicher, dass die Bedingung in Ordnung war (answer=yes
) in der ersten Schleife.