Prakee has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to execute the shell script inside perl script. I am successfully able to execute the shell script if I manually assign the variable named ApID. But I would like to call variable ApID=APXD_IERF outside the script which is in configuration file named LED.cfg
>my $confdir = $ENV{$CFG_DIR}; >my $conffile = 'LED.cfg'; >my $LEDcfg = "$confdir/$conffile"; >cat /abcd/efgh/LED.cfg ApID=APXD_IERF user=MATT JOBCD=1357 Script coding as below: >my $ApID = $LEDcfg->param(".ApID") ## passing the parameter ApID from + configuration file >my $pwd = `(ApID=$ApID $path/CA.sh)`; ## running shell script by ApID + parameterized => Failed >my $pwd = `(ApID=APXD_IERF $path/CA.sh)`; ## running shell script by +ApID hardcoded => Succeeded
  • Comment on How to call particular parameter from another configuration file in perl
  • Download Code

Replies are listed 'Best First'.
Re: How to call particular parameter from another configuration file in perl
by Fletch (Bishop) on Jun 03, 2021 at 16:58 UTC

    Your "example code" doesn't make sense. You show $LEDcfg being a string, then you call a method on it expecting to extract a particular value out of it. Something's missing. Perhaps whatever configuration module you're using doesn't work the way you're attempting to use it.

    Absent the actual code you're using no one's really going to be able to do more than vaguely guess what's actually going wrong. Step back, fire up the debugger (run with perl -d YOURPROGRAM; see perldebug) and/or embed some debugging prints and make sure that $ApID is what you expect (and/or that the putative $LEDcfg instance works the way you think).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: How to call particular parameter from another configuration file in perl
by hippo (Archbishop) on Jun 03, 2021 at 13:54 UTC

    You could load the configuration file with Config::General:

    use Config::General; my %CONF = Config::General->new ('path/to/LED.cfg')->getall (); my $pwd = `(ApID=$CONF{ApID} $path/CA.sh)`;

    Why are you using a subshell? Would it not be more efficient without?


    🦛

      I did not succeed in provided suggestion.

      I tried <<<my $pwd = qx(ApID=$CONF{ApID} $path/CA.sh);>>> as well. But no luck. This CA.sh is calling the password from CyberArk vault based on ApID value.

      CA.sh connects to cyberark vault through ApID and retrieves the password after successful attempt. Once the password is authenticated, ETL job is executed, That is the idea of the script. But unfortunately, i am failing to retrieve the password from cyberark vault for user APX_... mentioned. This ApID is present inside the configuration file named as LED.cfg which is in different location.

Re: How to call particular parameter from another configuration file in perl
by kcott (Archbishop) on Jun 05, 2021 at 05:49 UTC

    G'day Prakee,

    Welcome to the Monastery.

    As I understand it, you have a shell script (CA.sh) that uses a variable (ApID) to return a password. Here's an absolutely minimal script to simulate that:

    $ cat ./pm_11133472.sh echo -n "pw$ApID"

    Here's a couple of runs. In the first, the script does not have access to the ApID variable and returns a bogus password; in reality, that might be an error message. In the second, the script does have access to the ApID variable and returns a genuine password.

    $ ./pm_11133472.sh pw $ ApID=APXD_IERF ./pm_11133472.sh pwAPXD_IERF

    Perl has a special variable, %ENV. You can use this to effect what I showed above.

    $ perl -e ' for ("", "APXD_IERF") { $ENV{ApID} = $_; my $pwd = `./pm_11133472.sh`; print "$pwd\n"; } ' pw pwAPXD_IERF

    How you actually get the value for ApID from the config file is another matter. Others have already commented on that and I really have nothing further to add at this point.

    — Ken

Re: How to call particular parameter from another configuration file in perl
by Anonymous Monk on Jun 04, 2021 at 18:21 UTC

    I think there are two questions you should ask yourself:

    1. Does $ApID contain what you think it does? and
    2. Is the shell script actually receiving the value Perl is trying to send it?

    The first is easy enough: insert print "\$ApID='$ApID'\n" before you spawn the command. This assumes $ApID contains all printable characters. If not, you may need to enlist the aid of Data::Dumper, Data::Dump, YAML, JSON, or whatever your favorite dumper/serializer is.

    The second is harder, because it may not be obvious what the shell is doing with what you have given it. If you can modify $path/CA.sh, have it echo $ApID and see what you get.

      Yayyyy. I am not a perl expert and I am in beginner level. But I finally did it after 2 days of struggle. Below is the syntax that i have used and fetching ApId.
      use warnings; use Config::Simple; my $confdir = $ENV{CFG_DIR}; $conffile = 'LED.cfg'; my $LEDcfg = "$confdir/$conffile"; print `cat $LEDcfg\n`; Config::Simple->import_from('LED.cfg', \%Config); $LEDcfg = new Config::Simple('LED.cfg'); $ApID = $LEDcfg->param('ApID'); %Config = $LEDcfg->vars(); print "$ApID\n";

      Now i am able to read the ApID from LED.cfg file. And the next thing is that, i would like to run the perl script and pass the output to another variable $pwd inside main perl script . For ex

      <main Script 1> my $pwd = `($path/CA.pl)`; ## CA.pl is <script 2> chomp ${pwd}; print "$pwd"; ## print the output
      If i run the CA.pl manually, i am able to retrieve the desired password. I am able to connect to database server If i run as below.
      $./$path/CA.pl password1234 $pwd=password1234 mainscript1.pl Connected to Oracle database $
      But i could not capture the output of $path/CA.pl and parse the output to $pwd inside mainscript1. I tried below ones but no luck.
      my $pwd = qx($path/CA.pl); my $pwd = qx("$path/CA.pl"); my $pwd = system($path/CA.pl); my $pwd = system('$path/CA.pl'); my $pwd = system('usr/local/bin/perl -w $path/CA.pl');
      Can you help me run script 2 named $path/CA.pl inside mainscript1 and parse the output to $pwd variable inside main script. I expect the output of $pwd=password1234. Thanks for your guidance as always
        > my $pwd = qx($path/CA.pl);

        This should work. Maybe check $? on the next line, it should be zero if the script executed successfully. system returns an exit code, you can't capture the output with it.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]