Sponsored Content
Full Discussion: How to add a Y/N option?
Top Forums Shell Programming and Scripting How to add a Y/N option? Post 302970618 by kshji on Saturday 9th of April 2016 03:54:24 AM
Old 04-09-2016
Here is two examples. Also
Code:
read -p

is usable.

Using read -n 1.
Code:
#!/bin/ksh
# bash/ksh93/zsh/... but not dash (=posix)

# if read key ENTER, bash return newline and ksh return carriage return
cr=$'\r'  # ENTER key ksh
while printf "Continue:Y\b"
      read -n 1 response
      printf "\r                  \n"
      case "$response" in
                $cr|Y|y|"") response="Y" ;; # default = ENTER
                n|N) reponse="N" ;;
                *) response="N" ;;
      esac
      [ "$response" = "Y" ]
do
                echo "Do ..."
                date
                echo "..."
done

echo 'Goodbye.'

Using dd = take dump from stdin, 1 char.
Code:
# keypressed, read 1 char from stdin using dd
# works using any sh-shell
readkbd() {
  stty -icanon -echo
  dd bs=1 count=1 2>/dev/null
  stty icanon echo
}

while printf "Continue:Y\b"
      response=$(readkbd)
      printf "\r                  \n"
      case "$response" in
                Y|y|"") response="Y" ;; # default = ENTER
                n|N) reponse="N" ;;
                *) response="N" ;;
      esac
      [ "$response" = "Y" ]
do
                echo "Do ..."
                date
                echo "..."
done

echo 'Goodbye.'

 

9 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

How to add Win2000, Win Me option to Lilo menu?

Hi All, I have installed RH Linux 7.1, windows 2000 and windows ME in the same machine. There are two booting menu: Lilo: Linux Dos Dos : Windows2000 WindowsME If I want to use windowME, I need to boot lilo and choose dos and final choose windows ME. Have any... (3 Replies)
Discussion started by: wilsonchan1000
3 Replies

2. Shell Programming and Scripting

trying to add an option to view before executing?

I am very new to scripting. I have the following script: for i in `ls -1 | grep $1 | grep -v $2` do x=`echo $i | sed 's/\.New/\.Old/g'` echo mv $i DONE/$x done This is taking files from the directory I am in and changing the name from .New to .Old and then echoing the command to move... (2 Replies)
Discussion started by: llsmr777
2 Replies

3. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

4. UNIX for Dummies Questions & Answers

How to add the landscape option...

to an existing print que? (1 Reply)
Discussion started by: NycUnxer
1 Replies

5. Shell Programming and Scripting

How do I add the option to change the path in a menu?

How do I add the option to change the path in a menu? I have this script. The user chooses a number and had the option of doing something, looking for log files etc. There is a possibility they might want to look at a different path other than what I have given them such as... (2 Replies)
Discussion started by: taekwondo
2 Replies

6. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

7. Shell Programming and Scripting

Interactive script to give user option of what text to add

Hello all, I have created a script that will remove the first two lines of one text file (body.txt) and then add the text from a different text file (header.txt) directly to the beginning of the freshly modified body.txt file. It is as follows: #!/bin/bash ## This script will add a header... (2 Replies)
Discussion started by: marcozd
2 Replies

8. Ubuntu

Add Option To Right Menu

hey all, I already installed nautilus-actions now , I want to add "print path" script(option) to the right context menu!.. I did : http://img853.imageshack.us/img853/6973/59818245.png http://img847.imageshack.us/img847/8758/37217230.png the script print located in... (2 Replies)
Discussion started by: eawedat
2 Replies

9. Shell Programming and Scripting

Add option to fstab

I need as script (awk/sed?) to add noatime option to fstab. It should append ,noatime to whatever is in column 4 if noatime isn't already there, leaving comments alone. input: # /etc/fstab # Created by anaconda on Mon Oct 31 20:44:41 2011 # # Accessible filesystems, by reference, are... (5 Replies)
Discussion started by: pblumenthal
5 Replies
rpmatch(3)						     Library Functions Manual							rpmatch(3)

NAME
rpmatch - Determines whether a response is affirmative or negative LIBRARY
Standard C Library (libc.a) SYNOPSIS
#include <stdlib.h> int rpmatch( const char *response); PARAMETERS
User input entered in response to a question that requires an affirmative or negative answer. DESCRIPTION
The rpmatch() function determines whether the string value of the response parameter matches the affirmative or negative response expres- sion as specified by the LC_MESSAGES category in the program's current locale. Both response expressions, defined in the locale, may be extended regular expressions. A possible value of the affirmative expression, yesexpr, for a English-language locale is "^([yY]|[yY][eE][sS])". This expression will match any value of the response parameter that has consists of the letter Y (in uppercase or lowercase) or the letters YES (in any mixture of uppercase and lowercase letters). EXAMPLES
The following example requests a response from the user and uses the rpmatch() function to determine if the response is affirmative or neg- ative. #include <stdlib.h> #include <stdio.h> #include <locale.h> #include <string.h> #define SLENGTH 80 main() { char str[SLENGTH], *eol; int ans; (void)setlocale(LC_ALL, ""); printf("Do you want to perform this operation: "); fgets(str, SLENGTH, stdin); if ((eol = strchr(str, ' ')) != NULL) *eol = ''; /* Replace newline with null */ else return; /* Line entered too long */ ans = rpmatch(str); if (ans == 1) printf("You responded affirmatively "); else if (ans == 0) printf("You responded negatively "); else printf("Your answer did not match "); } RETURN VALUES
A value of 1 is returned if the string value of the response parameter is matched by the affirmative expression; a value of 0 (zero) is returned if the string value of the response parameter is matched by the negative expression. If neither expression matches the string value of the response parameter, a value of -1 is returned. RELATED INFORMATION
Commands: grep(1). Functions: regcomp(3), regexec(3), setlocale(3). Files: locale(4). delim off rpmatch(3)
All times are GMT -4. The time now is 08:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy