Solved

Write a  simple shell script

Posted on 2004-10-06
2
257 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

759 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

21 Experts available now in Live!

Get 1:1 Help Now