Jean-Francois T. Jean-Francois T. - 5 months ago 26
Bash Question

Set case sensitive Windows environment variables in Perl

I am trying to set some Windows environment variables in Perl with case sensitivity in order to use them in a shell script.

However, I noticed that the environment variables are all in uppercase when I try to use them in lowercase in a shell script called from Perl with

system
command and run with Msys.

For example, the following script:

#!perl

system "echo echo TOTO=\$TOTO > toto.sh";
system "echo echo Titi=\$Titi >> toto.sh";
system "echo echo TITI=\$TITI >> toto.sh";
$ENV{'TOTO'}="0+0";
$ENV{'Titi'}="Not toto!";
system("sh toto.sh");


returns (while run in Msys) the following output:

TOTO=0+0
Titi=
TITI=Not toto!


Does anybody know whether this is possible in Windows (i.e. how to set the environment variable
Titi
and not
TITI
)?

Thanks

Answer

It appeared to be that it is not possible to do: Windows will only use the uppercase variables.

The way I did was to temporarily transform the script by replacing variables with their upper-case equivalent and switch them back to the previous state after using them.

In my case, I had to deal with the variables exported from the shell script Run_Session.sh. I have created a Perl script to perform the uppercase replacement and this is the result.

###############################
## SUBROUTINE uc_variables   ##
#-----------------------------#
# This subroutine convert the environment variables exported 
# by the top level script to upper case, and can revert the operation.
#
# Input:
# -----
# $mode: "ON" (default) or "OFF"
#
# Output:
# ------
# N/A
#-----------------------#
#########################
sub uc_variables {
    my $mode=shift;
    chdir $Bin;
    if ($mode eq "OFF") {
        foreach my $bak (glob "{*,*/*}.sh.bak") {
            (my $orig = $bak)=~s/\.bak//;
            move($bak,$orig);
        }
    } else {
        #Check if back-up already exists
        uc_variables("OFF") if (-e "Run_Session.sh.bak");
        #List variables to update
        my %uc_var;
        open RUN_SESSION,"<Run_Session.sh" or die "Error while reading Run_Session.sh ($!)\n";
        map { /export\s+(\w*[a-z]\w*)/ and ($uc_var{$1}=uc($1))=~s/Directory/DIR/i } <RUN_SESSION>;
        close RUN_SESSION;
        #Replace variables
        foreach my $shell (glob "{*,*/*}.sh") {
            #Back-up
            copy($shell,"$shell.bak");
            #Read data
            open SHELL,"<$shell" or die "Error while reading $shell ($!)\n";
            my $SHELL = join("", <SHELL>);
            close SHELL;
            #Replace
            map { $SHELL=~s/$_/$uc_var{$_}/g } keys %uc_var;
            #Print
            open SHELL,">$shell" or die "Error while writing in $shell ($!)\n";
            print SHELL $SHELL;
            close SHELL;
        }
    }
} #end of uc_variables