Little Girl's Mostly Linux Blog

YesNo

Yes No

This page was last updated on January 22, 2017.

These scripts will ask you for one of two replies and will not continue until you give one or the other.

Method 1

This script uses an if statement:
#!/bin/bash

JOB () {
	echo -n "Do you wish to take that action? (y/n)";
	read -n1 response
	echo;
	if
		[ "$response" = "y" ];
	then
		echo "You chose yes.";
	elif
		[ "$response" = "n" ];
	then
		echo "You chose no.";
	else
		JOB;
	fi;
	}
JOB;
This script uses a case statement:
#!/bin/bash
JOB () {
	echo -n "Do you wish to take that action? (y/n)";
	read -n1 response
	echo;
	case "$response" in
		y) echo "You chose yes."
		;;
		n) echo "You chose no."
		;;
		*) JOB;
		;;
	esac;
	}
JOB;

Method 2

This script uses an if statement:
#!/bin/bash

JOB () {
	read -s -n1 -p "Do you wish to do that? (y/n)" response;
	echo;
	if
		[ "$response" = "y" ];
	then
		echo "You chose yes.";
	elif
		[ "$response" = "n" ];
	then
		echo "You chose no.";
	else
		JOB;
	fi;
	}
JOB;
This script uses a case statement:
#!/bin/bash
JOB () {
	read -s -n1 -p "Do you wish to do that? (y/n)" response;
	echo;
	case "$response" in
		y) echo "You chose yes."
		;;
		n) echo "You chose no."
		;;
		*) JOB;
		;;
	esac;
	}
JOB;

Obligatory Happy Ending

And they all lived happily ever after. The end.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: