#!/usr/local/bin/perl my $windows = $ENV{'windir'} || $ENV{'WINDIR'}; my $listsep = $ENV{'LISTSEP'} || ($windows? ';' : ':'); my $dirsep = $ENV{'DIRSEP'} || ($windows? '\\' : '/'); my $fancy = $windows? "\333" : '@'; my $joiner = $windows? '' : "\\\n"; # "\\\n\t"; $USAGE=" USAGE: $0 OPTIONS REGEXP SYNOPSIS: Print/verify ENV key/values that match REGEXP AUTHOR: GPL(C) Mohsin Ahmed, http://www.cs.albany.edu/~mosh NOTE: Like set | grep REGEXP, -dir checks dirs are readable. Cut/paste output into .bashrc and autoexec.bat. OPTIONS: P Prints key or value matching regexp P. -key=P Prints key = P. -key P Prints keys =~ P. -Key P Prints keys !~ P. -val P Prints values =~ P. -dir Check dirs exists in value, eg. LIB, PATH, INCLUDE. -v verbose EXAMPLE: setck LOGNAME=mosh HOME=/home/faculty/mosh ... PATH=/etc:/usr/local/bin:/var setck '^PATH$' Prints \$PATH setck 'M.*PATH' Prints \$MANPATH setck -key=LIB LIB=/sas/lib:/usr/lib setck -key HOME HOME=/home/faculty/mosh setck -val bin PATH=/etc:/usr/local/bin:/var setck -dir > settenv.bat && vi setenv.bat && settenv.bat "; while( $_ = $ARGV[0], /^-/ ){ shift; m/^--$/ && last; if( m/^-[?h]/i ){ print $USAGE; exit; } elsif( m/^-key=(.+)$/ ){ $mkey = "\^$1\$"; undef $fancy; } elsif( m/^-key$/ ){ $mkey = shift || die "usage: -key PAT to pick\n"; } elsif( m/^-Key$/ ){ $notmkey = shift || die "usage: -Key PAT to skip\n"; } elsif( m/^-val$/i ){ $mval = shift || die "usage: -val PAT to pick\n"; } elsif( m/^-dir$/ ){ $dirchk=1; undef $fancy; } elsif( m/^-v$/ ){ $verbose=1; } else{ die "Invalid option '$_', see -? for help.\n"; } } if( $pat = shift ){ # $pat = sh2pat( $pat ); warn "# grep( m/$pat/i, \%ENV )\n"; } KEY: foreach $key (sort keys %ENV){ my $val = $ENV{ $key }; # pick the keys. next KEY # Don't want to see the functions. if $val =~ m/}\s*\Z/; next KEY if # User wants some keys? $mkey && $key !~ s/$mkey/$fancy$&$fancy/ig; next KEY if # User doesn't want some keys? $notmkey && $key =~ m/$notmkey/i ; next KEY if # User wants some values? $mval && $val =~ m/$mval/; next KEY # Default is user wants anything matching his pat. if $pat && ($key !~ m/$pat/i ) && ( $val !~ m/$pat/i ); next KEY # User is doing -dir check and we don't care for rest. if $dirchk && $val !~ m,[/\\],; if( $dirchk ){ my( $notfirstdir, %val ); my @val = split( $listsep, $val ); if( $windir ){ @val = map( lc($_), @val ); print "set $key=\n"; }else{ print "$key=\\\n"; } foreach $dir (@val) { my $pd = ''; if( $windir ){ $pd .= "set $key=\%$key\%;$dir"; }else{ $pd .= ":$dir\\"; } my $err; if( $dirchk ){ $err .= " ** duplicate " if $val{ $dir }++ ; $err .= " ** trailing/ " if $dir =~ s,[/\\]+$,,g; $err .= " ** unreadable " unless (-r $dir) or (not $dir) or ($dir =~ m/\%/); if( -f $dir ){ $err .= " ** a file " }elsif(( ! -d $dir) and (-r $dir)){ $err .= " ** not a dir " } } printf "%-50s %s\n", $pd, $err; $notfirstdir = 1; } print "\n"; }else{ $val =~ s/$listsep/$joiner$&/g; # print "\n", $key,'=',$joiner,$val,"\n"; print $key,'=',$val,"\n"; } } # EOF