Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sleep: fail for extra args #430

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 28 additions & 34 deletions bin/sleep
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,36 @@ License: perl

use strict;

my ($VERSION) = '1.202';
use File::Basename qw(basename);
use Getopt::Std qw(getopts);

use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;

my $Program = basename($0);
my ($VERSION) = '1.203';

getopts('') or usage();
my $seconds = shift;
unless (defined $seconds) {
warn "$Program: missing operand\n";
usage();
}
if (@ARGV) {
if ($ARGV [0] eq '-?') {
$0 =~ s{.*/}{};
print <<EOF;
Usage: $0 seconds

Sleep for the given number of seconds.

Options:
-?: Print usage, then exit.
EOF
} elsif ($ARGV[0] =~ /^\d+$/) {
sleep $ARGV[0];
} else {
warn "$0: invalid time interval\n";
exit 1;
}
} else {
warn "$0: missing operand\n";
exit 1;
warn "$Program: extra operand `$ARGV[0]'\n";
usage();
}
if ($seconds !~ m/\A[0-9]+\z/) {
warn "$Program: invalid time interval `$seconds'\n";
exit EX_FAILURE;
}
sleep $seconds;
exit EX_SUCCESS;

exit;
sub usage {
warn "usage: $Program SECONDS\n";
exit EX_FAILURE;
}

__END__

Expand All @@ -53,19 +58,8 @@ sleep I<seconds>

=head1 DESCRIPTION

I<true> exits succesfully. I<false> exits unsuccesfully.

=head2 OPTIONS

I<sleep> accepts the following options:

=over 4

=item -?

Print out a short help message, then exit.

=back
I<sleep> waits for a number of seconds, then exits successfully.
The argument is taken as a decimal number with no fractional part.

=head1 ENVIRONMENT

Expand Down
Loading