I'm trying to create a bash script that asks a series of questions posed in sequence, all of which need to be answered sequentially with the word "yes" before proceeding to the next question. After the final question is asked, a piece of data is solicited and that is read into a variable before two final commands are executed.
The script is complex enough that my rudimentary bash skills and a bit of internet sleuthing have so far not resulted in a solution. So I want to ask for some help here.
Below is dummy sample script containing a bit of pseudo-code mixed in with some valid commands that is meant to better convey what I'm aiming to do with this script.
#!/bin/sh
echo "This script will create a file for you and perhaps rename it. Press any key to continue." read anykey
logic here to wait intil a key is pressed, then move on to display the first question
echo "Would you like to create a file?" read ans1
if [ $ans1 == yes ]; then
echo "Going to next question." #Logic here to proceed to next
question
if [ $ans1 != yes ] echo "This question requires a 'yes' answer. Returning to question"
else logic here to re-pose question
fi
echo "Would you really like to create the file?" read ans2
if [ $ans2 == yes ]; then
echo "Going to next question." #Logic here to proceed to next question
if [ $ans2 != yes ] echo "This question requires a 'yes' answer. Returning to question"
else logic here to re-pose question
fi
echo "Are you absolutely certain you'd like to create the file?" read ans3
if [ $ans3 == yes ]; then
echo "Going to next question." #Logic here to proceed to next question
if [ $ans3 != yes ] echo "This question requires a 'yes' answer. Returning to question"
else logic here to re-pose question
fi
echo "Do you take seriously the responsibilities of creating this file?" read ans4
if [ $ans4 == yes ]; then
echo "Going to next question." #Logic here to proceed to next question
if [ $ans4 != yes ] echo "This question requires a 'yes' answer. Returning to question"
else logic here to re-pose question
fi
echo "Do you make a firm commitment to not frivolously deleting this file?" read ans5
if [ $ans5 == yes ]; then
echo "Going to next item." #Logic here to proceed to next item
if [ $ans5 != yes ] echo "This question requires a 'yes' answer. Returning to question"
else logic here to re-pose question
fi
echo "Ok, if you insist on doing this please enter a name for the file:" read filename
echo "Creating file $filename"
touch $filename
echo "Your choice of file names is judged inadequate. Renaming file."
mv $filename adequate-filename
echo "Congratulations, you have successfully created a file. Exiting"
done
I can't determine whether, for example, while loops or until loops might be applicable here. Input on this task will be appreciated. Thanks
LATER EDIT: Here's what I came up with using a function (as was suggested by Marcus)--lots of credit to AI:
#!/bin/bash
read -n1 -s -r -p "This script will create a file for you and perhaps rename it. Press any key to continue."
echo
echo
# Function to ask yes-only questions
ask_question()
{
while true;
do
read -p "$1 (yes only): " answer
if [[ "$answer" == "yes" ]]; then break
else
echo "Please answer yes."
fi
done
}
# Ask 5 yes-only questions
ask_question "Would you like to create a file?"
ask_question "Would you really like to create the file?"
ask_question "Are you absolutely certain you'd like to create the file?"
ask_question "Do you take seriously the responsibilities of creating this file?"
ask_question "Do you make a firm commitment to not frivolously deleting this file?"
echo
read -p "Ok, if you insist on doing this please enter a name for the file: " filename
sleep 1 && touch $filename
sleep 2
echo "Your choice of file names is judged inadequate. Renaming file."
mv $filename $(date +%m-%d-%y)-$filename
echo "Congratulations, you have successfully created a file."
# Final question
read -p "Are you ready to exit? (yes/no): " ready_answer
# Check if the user is ready
if [[ "$ready_answer" == "yes" ]]; then
echo "Your new file has been saved as $(ls $(date +%m-%d-%y)-$filename)"
else
echo "You are not ready."
fi
Any improvements/corrections needed? This is a while loop running inside a function, right?
cmd > fileor thatmv foo filewould happily do). Instead, you might want to just consider saying what you're going to do, e.g. "Creating file 'foo.txt', ok? (Y/n)". And creating the file with means that don't clobber an existing file, unless you specifically asked about that, too.