0

The question is simple, but I haven't found the answer on Google.

How do I run in perl something like this?

[$] VARIABLE=foo ./script.sh

I have a perl script (Nvidia Cuda installer, for example) with hardcoded variables in the script, but I want to set that variables when I run the script instead of editing the script.

In bash it's like posted above, but I don't know how make this work on perl.

Is it possible to do this?

Greetings.

EDIT: For clarification: this is a sniplet of the install-linux.pl script:

sub can_add_for_all_users()
{
    my $filename = "/usr/share/applications/.nvidiatoolkitinstall";
    system("touch $filename 2>/dev/null");
    if (-f $filename)
    {
        system("rm $filename");
        return 1;
    } else {
        return 0;
    }
}

I want to run something like this:

[$] filename="/tmp/.nvidiatoolkitinstall" perl install-linux.pl -destdir="${pkgdir}" -prefix="/opt/cuda" -noprompt -nosymlink

But this not works

I can do edit the script with sed:

[$] sed 's|/usr/share/applications/.nvidiatoolkitinstall|/tmp/.nvidiatoolkitinstall|g' -i install-linux.pl

but i want parser the variable to perl instead, like bash can do

sL1pKn07
  • 1
  • 1

2 Answers2

1

You are looking for "how to read environment variables with perl". Perl have %ENV hash, which keeps all these variables. You can simply access them with $ENV{'VARIABLENAME'}. Simple example:

#!/usr/bin/perl
print $ENV{'VARIABLE'};

and than VARIABLE=blahblah script.pl returns blahblah.

stderr
  • 141
  • 5
0
local $ENV{VARIABLE} = 'foo';
system('./script.sh');
ikegami
  • 367,544
  • 15
  • 269
  • 518