Solved

Write a  simple shell script

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
lunix and unix command 21 120
What is linux 4.8.22? Is it the same as ubuntu 16.10 ?? 2 77
LogmeIn using Linux Ubuntu 16.04 6 117
RHEL 6.7 Gnome Desktop on VMware 6 VM 9 115
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

730 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