• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Write a simple shell script

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.
1 Solution

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


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";
#! /bin/bash
echo "type Y or N"
while read -n 1 ans
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
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now