Skip navigation

Here’s a way to use variables in both bash and perl.

From Perl

  1. Start with a config file, we’ll call it ‘conf.ph’:
    #!/usr/bin/perl
    %vars = (
    HOME => '/home/gba',
    NAME => 'greg a' );
    
  2. from there we can load ‘conf.ph’ into any of our perl scripts
    #!/usr/bin/perl
    require 'conf.ph';
    
  3. load its variables
    #!/usr/bin/perl
    require 'conf.ph';
    print "my name is: " , $vars{'NAME'} , "\n";
    

from bash

  1. using the same conf.ph we create a primer script to load the perl variables into the environment:
    #!/usr/bin/perl
    include "conf.ph";
    while ( my ($key, $value) = each(%vars) )
    {
    $ENV{$key} = $value;
    }
    
  2. we can then execute our shell script from with-in our primer script
    #!/usr/bin/perl
    include "conf.ph";
    while ( my ($key, $value) = each(%vars) )
    {
    $ENV{$key} = $value;
    }
    system("/bin/myname.sh");
    
  3. /bin/myname.sh:
    #!/bin/sh
    echo "my name is $NAME"
    
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.