#!/usr/bin/perl sub help { local $_= <<"#EOT";s/^# ?//mg; print } # program: random-string # Generate random string, version $VERSION # (c) 2003-8 Vlado Keselj http://www.cs.dal.ca/~vlado # # Usage: random-string [-hv] [length [chars-specification] ] # -h help # -n no new-line at the end # -v version # chars-specifications: default is 0..9, 1..z, A..Z # d is 0..9 # #EOT use strict; use vars qw( $VERSION ); $VERSION = sprintf "%d.%d", q$Revision: 1.2 $ =~ /(\d+)/g; use Getopt::Std; use vars qw($opt_v $opt_h $opt_n); getopts("vhn"); if ($opt_v) { print "$VERSION\n"; exit; } if ($opt_h) { &help(); exit; } if ($#ARGV > 1) { &help; exit 1; } print &random_string(@ARGV).($opt_n ? '' : "\n"); # generate random string of given length sub random_string { my $n = shift; $n = 8 unless $n > 0; my $c = shift; my @chars = (0..9, 'a'..'z', 'A'..'Z'); if ($c eq 'd') { @chars = (0..9) } return join('', map { $chars[rand($#chars+1)] } (1..$n)); }