#!/usr/bin/env perl # #$Info: env2 - Converts environment variables to various script languages. $ ############################################################################}}} ## BEGIN DOCUMENTATION #####################################################{{{ ############################################################################### =pod =head1 NAME B - Converts environment variables to various script languages. =head1 SYNOPSIS B -from I -to I [-o I] I B --save [FILE] B # displays help =head1 DESCRIPTION Do you prefer running bash while the rest of the team runs tcsh? Or perhaps you learned csh and the system administrators only know ksh. What happens when they (or you) supply an initialization script to source? If any of these situations sounds familiar, then this may be the script for you. B takes shell scripts of one flavor in and spits out scripts effectively equivalent in another dialect. We say "effectively equivalent" because it does not translate syntax such as B/B statements. Instead, the original (source) script is evaluated to determine what environment variables it modifies, and the effective values of those variables are simply expressed in the syntax of the destination script's dialect. Typically, this is all you really need for scripts that modify the environment. NOTE: If you need to the conditionals and for-loops to be used for different situations (e.g. different host architectures), then simply use this script repeatedly as needed. Future extensions may include B or . Supported languages currently include: B, B, B, B, B, B, B, B, B, B, B, and B. NOTE: The file version is identified by an internally computed SHA1 hash similar to the way B does versioning. If you get a warning message about inconsistent hash, it means that somebody modified the file without updating the $SHA variable. =head1 OPTIONS =over =item B<-all> Include all variables rather than just those that changed. See B<-diff>. =item B<-clear> Resets the ignore list to almost completely empty except for the generally dangerous environment list. See B<-ignore>, B<-reset>, and B<-unsafe_clear>. =item B<-diff> Only include those variables whose values are different as a result of sourcing the specified input script. This is the default action. =item B<-from> DIALECT The dialect to translate from. If not present, then the input file is examined for a #! line. If that fails, the B environment variable is examined. If that fails, we default to B (Hey, I had to choose something, and B is the default Linux shell of choice). =item B<-help> This built-in documentation. Written in POD so that you can also have it in HTML, PDF, RTF, plain text or as a man page. =item B<-ignore> VARLIST Comma separated list of variables to ignore. By default, B starts with the list set to _,ENV,ENV2,OLDPWD,SHLVL See also B<-clear>, B<-reset> and B<-unsafe_clear>. =item B<-ignored> Lists variables that will be ignored. Useful if you are uncertain and want to clarify things before proceeding. =item B<-o> [I] Specifies a I to save the results in. By default results are sent to STDOUT. If you leave off the I, the filename will be CB<$SHELL>. =item B<-profile> [I] Specifies a file that contains the starting environment. Defaults to C<.env2profile>. Searches for the file in the current directory or $HOME if path not specified. =item B<-reset> Resets the ignore list to a minimum set. See also B<-clear>, B<-ignore> and B<-unsafe_clear>. =item B<-save> [FILE] Saves the environment in a perl format. By default saves to env2.pl. Internally, this option is invoked as part of the conversion process and saves an intermediate file to /tmp/env2.$$.pl. =item B<-sha1> Displays the SHA1 version identifier. Use this to see if script has been modified since last updated. =item B<-to> DIALECT The dialect to translate to. If not supplied, the B environment variable is examined. If that fails, we default to B (Hey, I had to choose something, and B is the default Linux shell of choice). =item B<-uniq> I<[PATHLIST]> Ensure that each path variable specified in I contains a unique colon separated list. Default is to apply this to common path variables: MANPATH PATH LD_LIBRARY_PATH DYLD_LIBRARY_PATH PERL5LIB =item B<-unsafe_clear> Resets the ignore list to completely empty. This is potentially dangerous because allows inclusion of dangerous environment variables that the user should not touch. These dangerous variables can have an adverse affect on operation of UNIX. See also B<-clear>, B<-ignore> and B<-reset>. =item B<--version> Display version of script. =back =head1 ENVIRONMENT ENV2 environment variable contains default command-line arguments if defined. =head1 DEPENDENCIES To support YAML, this script uses the CPAN YAML module. =head1 EXAMPLES # Simple conversion env2 -from ksh -to csh -o setup.csh setup.ksh # Create a modules cmd for a new xyz tool version 1.0 TOOL_SETUP=$TOOLS/vendor_dir/xyz_tool/xyz-1.0/setup.script MDLDIR=$TOOLS/modules/tools/xyz env2 -from sh -to modulecmd -o $MDLDIR/1.0 $TOOL_SETUP # Dynamically use a script for another shell to set environment eval `env2 -from ksh -to $SHELL -o setup.csh setup.ksh` =head1 COPYRIGHT/LICENSE env2 is copyright (C) 2003-2008 David C Black. All rights reserved. This code may is hereby made available under Apache 2.0 licensing. =head1 AUTHOR David C. Black =cut ############################################################################### ## END DOCUMENTATION ########################################################## ############################################################################}}} require v5.6; use strict; use warnings; use English; use FindBin qw($RealBin $RealScript); use Data::Dumper; use FileHandle; use Cwd qw(getcwd abs_path); BEGIN { eval 'use Digest::SHA qw(sha1)'; } our $use_sha1=defined &sha1; BEGIN { eval 'use YAML'; } our $use_yaml=defined &YAML::LoadFile; STDOUT->autoflush(1); STDERR->autoflush(1); our $start_dir = &getcwd(); our $LINE = '?'; use vars qw{$SHA1}; our $verbose = 0; our (@DANGER_LIST,@USER_PREFS,@DEFAULT_IGNORE,@IGNORE,@ONLY); our $VERSION = '1.1.0'; ########################## <<< UPDATE THIS LINE WHEN FIXING BUGS OR ADDING FEATURES >>> ### # Compute SHA for the script if ($use_sha1) { our $SHA1 = '18017c6e340372ef014013675b6bb799e60cc971'; #### <<< UPDATE THIS LINE WHEN COMMITTING CHANGES >>> ### my $sha = Digest::SHA->new(1); open SCRIPT,"<$RealBin/$RealScript"; while (