Solved

Write a  simple shell script

Posted on 2004-10-06
2
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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