Last active
February 19, 2017 05:10
-
-
Save ericychoi/b84d0b5bce54a5bcb8d4541b8a886d28 to your computer and use it in GitHub Desktop.
Perl bin script template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use strict; | |
| use warnings; | |
| use Getopt::Long; | |
| use Term::ANSIColor; | |
| # usage: | |
| # | |
| my $SCRIPT = 'foo.pl' | |
| my ($dryrun, $verbose, $foo); | |
| GetOptions( | |
| 'dryrun!' => \$dryrun, | |
| 'verbose!' => \$verbose, | |
| ) or die "usage: $SCRIPT (--dryrun)\n"; | |
| my @commands = @ARGV; | |
| if ($dryrun) { | |
| print "dryrun mode:\n"; | |
| } | |
| my $cmd .= "ls -al"; | |
| $cmd .= " $foo " if $foo; | |
| do_cmd($cmd); | |
| exit 0 if !$more_cmds; | |
| sub do_cmd { | |
| my ($cmd) = @_; | |
| if ($dryrun) { | |
| print "$cmd\n"; | |
| return; | |
| } | |
| open CMD, '-|', $cmd or die $@; | |
| my $line; | |
| while (defined($line=<CMD>)) { | |
| # print colored("hello world\n", 'green'); | |
| print $line; | |
| } | |
| close CMD; | |
| die "command: '$cmd' returned non-zero status\n" if $?; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment