Quick Question with Scripting

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Cmiller21

Quick Question with Scripting

Post by Cmiller21 »

Hi,

So I've recently been experimenting with the case command. I know how to use it, but I'd like some help figuring out something. I wrote this script shown below

Code: Select all

#!/bin/bash

echo "What is your name?"
read myname
echo "Hi $myname"

echo "Would you like to continue running this script? Yes or no?"

read answer

case $answer in
	yes) echo "Okay, let's continue.";;
	no) echo "Okay, terminating script now."
		exit 0;;
	*) echo "The answer you typed is not valid, please type yes or no.";;
esac
So the script would either continue or terminate depending on whether the user entered yes or no. But if user doesn't enter yes or no I would like the script restart the case command until the user types in either yes or no. The problem is I do not know what command to use for that to happen. Could someone help me?

~Cmiller21
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Habitual

Re: Quick Question with Scripting

Post by Habitual »

Cmiller21 wrote:But if user doesn't enter yes or no I would like the script restart the case command until the user types in either yes or no.
Why restart anything? It will just sit there waiting. No need to restart.

http://mywiki.wooledge.org/BashGuide/Te ... nditionals
for some tips.
Cmiller21

Re: Quick Question with Scripting

Post by Cmiller21 »

Habitual,

thanks for the link you gave me. Based on what I read there, I changed my script, and it works! If anyone is curious this is my new script:

Code: Select all

#!/bin/bash

echo "What is your name?"
read myname
echo "Hi $myname"

echo "Would you like to continue running this script?"

select choice in yes no; 
do
if [[ $choice = yes ]]
	then echo "Okay, let's continue."; break;
elif [[ $choice = no ]]
	then echo "Okay, terminating script now."
	exit 0;
fi
done
I didn't get a chance to read all of the article, but I'll make sure to study on it soon. Thanks again!

~Cmiller
Habitual

Re: Quick Question with Scripting

Post by Habitual »

Cmiller21

Re: Quick Question with Scripting

Post by Cmiller21 »

WOW! Thanks! Appreciate it big time, man. And I really mean it :D . I'll be bookmarking these shortly.

~Cmiller
Habitual

Re: Quick Question with Scripting

Post by Habitual »

You're very welcome.

Here's a custom Google search engine I put together to search 44 Linux-specific sites for coding goodness...
Locked

Return to “Scripts & Bash”