#!/usr/local/bin/perl my $windows = $ENV{'windir'} || $ENV{'WINDIR'}; my $listsep = $ENV{'LISTSEP'} || ($windows? ';' : ':'); my $dirsep = $ENV{'DIRSEP'} || ($windows? '\\' : '/'); my $fancy = ''; my $fance = ''; if( ($ENV{'TERM'} eq "xterm") ){ $fancy = "\033[31m"; $fance = "\033[39m"; } # AUTHOR: GPL(C) http://www.cs.albany.edu/~mosh $USAGE="USAGE: setm OPTION STRING SYNOPSIS: print \$ENV\{STRING\} or matching keys. Options: -w Word delimit STRING to \\bSTRING\\b. -setenv 'setenv x y' csh style output. -set 'set x=y' csh/dos style output. 'x=y' sh style output is default. -v verbose -n No shell functions. Example: sett path PATH=.... sett -/macs EMACS_DIR=... EMACS_LOADPATH=... "; while( $_ = $ARGV[0], m/^-/ ){ shift; last if /^--$/; $verbose++, next if m/^-v/; $skipfun++, next if m/^-n/; $setenv++, next if m/^-setenv/; $set++, next if m/^-set/; $worddelimit, next if m,^-w,; die $USAGE if m/^-?/; } unless( $pat ){ $pat = shift || die $USAGE; } $pat = "\\b$pat\\b" if $worddelimit; warn "% grep( m/$pat/i, \%ENV )\n"; foreach (sort keys %ENV) { if( $skipfun && ( $ENV{$_} =~ m,^\(\), ) ){ warn "Skipping $_\n" if $verbose; next; } if( $setenv ){ $kv = sprintf("setenv %-10s %s", $_, $ENV{$_} ); }elsif( $set ){ $kv = sprintf("set %s=%s", $_, $ENV{$_} ); }else{ $kv = sprintf("%s=%s", $_, $ENV{$_} ); $kv =~ s/[\n\r]/\\n/g; # bash functions in ENV. if( $windir ){ $kv =~ s/$listsep$//g; $kv =~ s/$listsep/\n\t$&/g; }else{ # x:y is printed as x\\\n:y $kv =~ s/$listsep/\\\n$&/g; } } if( $kv =~ s/$pat/$fancy$&$fance/gio ){ print $kv,"\n" } }