Solved

Write a  simple shell script

Posted on 2004-10-06
2
259 Views
Last Modified: 2010-04-20
I need help writing a simple shell script that solicits a (Y)es or (N)o response from the user. If the response is 'Y' display a message on the screen that thanks the user for the positive response. If the response is 'N' display a message that thanks the user for the negative response. If the answer is anything else, redisplay the question.
0
Comment
Question by:plate55
2 Comments
 
LVL 10

Expert Comment

by:Luxana
ID: 12245105
Hi

you can try something like this:
first do
# which perl
and then just change first line of cript according you output

---------------------------------------
#!/usr/bin/perl

while ($input ne 'y'){
print "\(Y/N\)\n";
chomp($input = <STDIN>);
if ($input eq 'y') {
print "answer yes\n";
} elsif ($input eq 'n') {
print "answer no\n";
$input='y';
}
}
0
 
LVL 4

Accepted Solution

by:
bytta earned 500 total points
ID: 12245131
#! /bin/bash
echo "type Y or N"
while read -n 1 ans
do
case $ans in  #checks if the char is Y,y,N or n
        [Yy]*) echo -e "\rThanks for Yes";break;; # Y or y
        [Nn]*) echo -e "\rThanks for No";break;; # N or n
        *) echo -en "\r";;                                   # anything else
esac
done # were done now - comments follow
#-------------------------------
infinite while loop that reads one char (-n 1) and stores it in ans. breaks when

the echo -en "\r" command (carriage return) is just for looks, so that your screen doesn't get full of characters, even if you type with your forehead.
There's probably a neater way to do this (similar to getc() in C/C++), but this way you can see the last typed char (unless you press <enter>, which starts a new line)

Fixing that would be more than the 10 lines of code here
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now