Home  >   Perl Scripts


lenv - List Environment Variables

Description

The script lenv lists the values of ':' delimited environment variables in a way that makes them much easier to read. This partictulary useful when you are trying to debug problems in your $PATH, $LD_LIBRARY_PATH or $CLASSPATH variables.

Usage and Options

List environment variables in 'human readable' format. If the '-e' option is given then the program expects the output of the 'env' command as its input. For example:

env | grep PATH | lenv -e'

Options:

-e Expect 'env' command output as standard input
-h Help


Example Usage

This example shows the values of the $CLASSPATH and $PATH environment variables.

An example of using lenv:

$ lenv CLASSPATH PATH
CLASSPATH = /usr/local/inst/jConnect-5_2/classes/jconn2.jar
          : /usr/local/inst/jdk1.2.2/lib/tools.jar
          : /home/httpd/classes/servlet-2.0.jar
          : /usr/local/inst/ant-1.2-1/share/java/ant.jar
          : /usr/local/inst/ant-1.2-1/share/java/jaxp.jar
          : /usr/local/inst/ant-1.2-1/share/java/parser.jar
          : /home/alan/work/crs/web/src
PATH      = /opt/sybase-11.9.2/bin
          : /usr/local/inst/jdk1.2.2/bin
          : /usr/local/bin
          : /bin
          : /usr/bin
          : /usr/X11R6/bin
          : /usr/local/bin
          : /home/alan/bin
          : /home/alan/local/bin
          : /usr/local/inst/jdk1.2.2/bin
          : /usr/local/inst/ant-1.2-1/bin

Source Code


#!/usr/bin/perl
#
#	11.10.96 ah

require "getopts.pl";

# =============================================================================

sub Usage {
print STDERR <<EndOfUsage;
Usage: $0 [-eh] [variable names..]
        -h for help
EndOfUsage
}
 
sub Help {
print STDERR <<EndOfHelp;
 
Usage: $0 [options] [variable names..] 
 
    Description:
 
        List environment variables in 'human readable' format. If the '-e'
	option is given then the program expects the output of the 'env'
	command as its input (E.g. 'env | grep PATH | lenv -e'). 
 
    Options:
 
        -e		Expect 'env' command output as standard input 
        -h              Help
 
EndOfHelp
        exit (1);
}

# =============================================================================

&Getopts ('eh');

defined ($opt_h) && &Help;

if (!defined ($opt_e)) {
	%VARS = %ENV;
} else {
	# ---- generate the %VARS array from stdin and/or names files

	while (<>) {
		$line = $_;
		chop ($line);

		$var = $line;
		$var =~ s/=.*//;

		$value = substr ($line, length ($var) + 1);

		$VARS { $var } = $value;
	}
}

# ----- Set up an associative array with the requested variable=value pairs
#	if there are left over arguments

if (scalar (@ARGV) > 0) {
	%VARS = ();
	foreach $var (@ARGV) {
		$VARS { $var } = $ENV { $var };
	}
}

# ----- Get the maximum variable length

$mlen = 0;
foreach $key (sort (keys %VARS)) {
	$mlen = length ($key) if (length ($key) > $mlen) 
}

# ----- Output the report

$blank = '                                                                   ';
$pad = substr ($blank, 0, $mlen + 1);

foreach $key (sort (keys %VARS)) {

	$value = $VARS { $key };

	if ($mlen + length ($value) > 80 || $key =~ /PATH$/) {	

		# ----- make paths and long items into vertical list

		$value =~ s/\s+/\n${pad}  /g;	
		$value =~ s/:/\n${pad}: /g;
	}

	printf ("%s%s = %s\n", 
		$key, 
		substr ($blank, 0, $mlen - length($key)),
		$value);
}

# ----- end


Copyright 1996-2000 Alan Hodgkinson. All rights reserved.