- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Built-in function to accept "yes" "no" respons...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2005 05:23 AM
07-12-2005 05:23 AM
Built-in function to accept "yes" "no" response?
My if statements are growing because of all the y/n logic that needs to be done.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2005 05:38 AM
07-12-2005 05:38 AM
Re: Built-in function to accept "yes" "no" response?
sub confirm()
{
$UserAnswer = <>; # Read in the user's answer.
chomp $UserAnswer;
if ((not($UserAnswer eq "n")) and # If it's not a yes or a no,
(not($UserAnswer eq "y")) and
(not($UserAnswer eq "N")) and
(not($UserAnswer eq "Y")))
{
while ((not($UserAnswer eq "n")) and # Keep going until we get a yes
(not($UserAnswer eq "y")) and # or a no.
(not($UserAnswer eq "N")) and
(not($UserAnswer eq "Y")))
{
print "Please answer \"y\", for yes, or \"n\", for no.\n";
$UserAnswer = <>;
chomp $UserAnswer;
}
}
# If we've gotten here, the user has given a yes or a no answer.
return (($UserAnswer eq "Y") or ($UserAnswer eq "y"));
}
then using it
print "Do you wish to continue (y/n)? ";
if (&confirm())
{
print "Continuing...\n";
}
else
{
print "Bye!\n";
exit 0;
}
I saved it for future use in my useful stuff folder but never tested to see if it works. Hence, proceed with caution ...
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2005 05:43 AM
07-12-2005 05:43 AM
Re: Built-in function to accept "yes" "no" response?
Basically, what I wanted was a way to prevent any other input, when asking for a response without having to build in the extra "else" statement to handle all other keystrokes.
If there isn't an easy way, then I'll resort to the extra 'else'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2005 06:10 AM
07-12-2005 06:10 AM
Re: Built-in function to accept "yes" "no" response?
In a shell script you could do something like:
case ANSWER in
Y|y|yes|YES|Yes|YEs|yES|YEs|yeS) do something ;;
N|n|NO|No|nO) do something else ;;
*) error condition for all other input ;;
esac
Just a thought!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2005 06:24 AM
07-12-2005 06:24 AM
Re: Built-in function to accept "yes" "no" response?
Another shell option that is similar is;
$ cat ddd.sh
#!/usr/bin/ksh
echo "Do you know what your doing?"
select ENTRY in yes no
do
echo "You selected: $ENTRY"
break
done
$ ./ddd.sh
Do you know what your doing?
1) yes
2) no
#? 1
You selected: yes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2005 07:01 AM
07-12-2005 07:01 AM
Re: Built-in function to accept "yes" "no" response?
You write subroutines to handle basic tasks like this:
my $resp = ask_yn("Are you a whackball?");
ask_yn() returns 1 if yes/y/YeS/Y/ ... is pressed; 0 if no/NO/n/nO/N ... is pressed; and the function returns -1 if SIGINT is received. It loops until a valid response is typed (or a SIGINT is received).
It's really time for you to go out and buy the O'Reilly Perl (Camel) books. The good news is that once you start writing Perl, you really don't need shell, awk, sed, grep, et al.
The attached ask_yn.pl should make a good subroutine for you. Note: "die" inside an eval does not exit the program just exits the enclosing eval {} and that's a sneaky way to implement signal handlers in Perl.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2005 08:36 AM
07-12-2005 08:36 AM
Re: Built-in function to accept "yes" "no" response?
if ($q =~ /^(yes|no|y|n)$/)
becomes
if ($q =~ /^\s*(yes|no|y|n)\s*$/)
\s* means zero or more whitespace characters
$rslt = (substr($q,0,1) eq "y");
becomes
$rslt = (substr($1,0,1) eq "y");
Regular expressions inside ()'s become $1, $2, ... for each set of parens.
The change is up to you but it does make things easier for the user at the slight expense of greater Perl novice confusion.