#!/usr/bin/env perl ## envstore - save and load environment variables ## Copyright © 2009 by Daniel Friesel ## License: WTFPL use strict; use warnings; use Pod::Usage; use Simplestore; my $store_file = "/tmp/envstore-$>"; my %store; my $action = shift; my $arg = shift; my $arg2 = shift; my ($key, $value); sub usage { pod2usage( -message => 'Syntax error', -exitval => 1, -verbose => 99, -sections => 'SYNOPSIS|DESCRIPTION', -output => \*STDERR, ); } sub check_store { my ($mode, $uid); unless (-e $store_file) { return(0); } ($mode, $uid) = (stat($store_file))[2,4]; if ($uid != $<) { print STDERR "envstore: store file not owned by us\n"; exit(1); } $mode &= 0x00077; if ($mode > 0) { print STDERR "envstore: store file writable by group/others\n"; exit(1); } return(1); } sub load_store { return unless check_store; %store = %{load($store_file) || {}}; } sub save_store { umask(0077); save($store_file, \%store); } sub get_keyvalue { my ($key, $value) = @_; unless (defined($value)) { if (exists($ENV{$key})) { $value = $ENV{$key}; } else { print STDERR "No such parameter: $key\n"; exit(1); } } return($key, $value); } usage unless defined($action); load_store; if ($action eq 'save') { usage unless defined($arg); ($key, $value) = get_keyvalue($arg, $arg2); $store{$key} = $value; save_store; } elsif ($action eq 'eval') { while (($key, $value) = each(%store)) { $value =~ s/'/'"'"'/g; print "export $key='$value'\n"; } } elsif ($action eq 'show' or $action eq 'list') { while (($key, $value) = each(%store)) { printf("%-15s = %s\n", $key, $value); } } elsif ($action eq 'rm') { usage unless defined($arg); delete($store{$arg}); save_store; } elsif ($action eq 'clear') { unlink($store_file); } else { usage; } __END__ =head1 NAME envstore - save and restore environment variables =head1 SYNOPSIS B I [ I ] =head1 DESCRIPTION envstore can safe and restore environment variables, thus transferring them between different shells. I must be one of =over =item B Forget all stored variables =item B Produce shell code for evaluation, restoring all saved variables =item B I [I] Save I either with its current shell value or with I =item B List saved variables in better readable format =item B I Remove I from store =back =head1 AUTHOR B was written by Daniel Friesel Ederf@derf.homelinux.orgE Original idea and script by Maximilian Gass Emxey@ghosthacking.netE