Using dot commands in a script

Discussion in 'Perl Misc' started by danfperl@yahoo.com, Mar 9, 2005.

  1. Guest

    I'm trying to load an environment file in a script and I think it may
    be easiest to show it before explaining it. I've got 2 files,
    /tmp/test.pl and /tmp/test.env. Here are the contents.

    test.env:
    #!/usr/bin/ksh
    export BLAH=TEST

    test.pl:
    #!/usr/local/bin/perl
    system('. /tmp/test.env');
    print "\$BLAH is ${BLAH}" ;

    When I run it I get:
    sh: BLAH=TEST: is not an identifier
    $BLAH is

    How can I get Perl to load the file properly? It does so from the
    command line but throws the error when I run the script. For reasons I
    can't get into I can't change the env file - the perl script needs to
    be modified. Any help is greatly appreciated!!

    Dan
    , Mar 9, 2005
    #1
    1. Advertising

  2. wrote :
    > I'm trying to load an environment file in a script and I think it may
    > be easiest to show it before explaining it. I've got 2 files,
    > /tmp/test.pl and /tmp/test.env. Here are the contents.
    >
    > test.env:
    > #!/usr/bin/ksh
    > export BLAH=TEST
    >
    > test.pl:
    > #!/usr/local/bin/perl

    use strict;
    use warnings
    > system('. /tmp/test.env');


    Why not simply say:
    $ENV{BLAH}="TEST";
    to set the environmet variable
    or even
    $BLAH="TEST";
    to use a perl variable?

    > print "\$BLAH is ${BLAH}" ;

    Here you probably want:
    print "Shell-\$BLAH is $ENV{BLAH}\n";
    if you really want the exported shell variable.

    ${BLAH} is the same as $BLAH, which has not been assigned yet in your
    script.

    >
    > When I run it I get:
    > sh: BLAH=TEST: is not an identifier


    This comes from the shell; I have not worked with the ksh, but it looks
    like a syntax error in the variable assignment.

    > $BLAH is


    This is, because $BLAH is empty.
    If you had used strict and warnings, perl had told you.

    HTH
    Best regards
    Martin

    --
    perl -e '$|=1;&p(7.74.117.115.116.32);&s();&p(97.110.111);&p(116.104.101
    ..114);&s;&p(32.112.101.114.108);&s();&p(32.104.97.99.107.101.114.10);sub
    s{sleep 1};sub p(){print "@_"}'
    Martin Kissner, Mar 9, 2005
    #2
    1. Advertising

  3. Dan Guest

    Thanks for the quick reply Martin.

    > Why not simply say...to set the environmet variable or even...to use

    a perl variable?

    It's not just one variable - that was an example. The actual
    environment file I'm working with has dozens of lines. We have lots of
    scripts running here and decided to have a single file with all
    settings that anyone could reference. The more we set environment
    variables outside it the tougher it is to manage, so there's basically
    a hard line on that one: Source from the environment file or not at
    all.

    I know the error is shell related and I know why it's throwing it. For
    some reason the dot command in the script defaults to sh instead of ksh
    or bash and the environment file was designed for ksh/bash. When it
    runs via sh it throws the error. (It uses "export BLAH=TEST" syntax
    which isn't supported in sh.)

    Dan
    Dan, Mar 9, 2005
    #3
  4. wrote :
    > I'm trying to load an environment file in a script and I think it may
    > be easiest to show it before explaining it. I've got 2 files,
    > /tmp/test.pl and /tmp/test.env. Here are the contents.
    >
    > test.env:
    > #!/usr/bin/ksh
    > export BLAH=TEST
    >
    > test.pl:
    > #!/usr/local/bin/perl
    > system('. /tmp/test.env');
    > print "\$BLAH is ${BLAH}" ;
    >
    > When I run it I get:
    > sh: BLAH=TEST: is not an identifier
    > $BLAH is
    >
    > How can I get Perl to load the file properly? It does so from the
    > command line but throws the error when I run the script. For reasons I
    > can't get into I can't change the env file - the perl script needs to
    > be modified. Any help is greatly appreciated!!


    Well, in that case I would try
    system('/usr/bin/ksh /tmp/test.env')
    I suppose even
    system('/tmp/test.env')
    would work if /tmp/test.env is executable, since it has a proper shebang
    line.

    But I guess I'd prefere a file wich is formatet like this
    ----- variables.txt ----
    name1:value1
    name2:value2
    name3:value3
    ----
    and then use a hash like this in the scripts
    --- snip ----
    my %hash;
    open FH,"<","variables" or die "error\n";
    while (<FH>) {
    /(.*?):(.*)/;
    $hash{$1}=$2;
    }
    print $hash{'name1'},"\n";
    --- snap ---

    The last line prints "value1".
    I do not really see a need to use the 'system' function and shell
    variables here.

    HTH
    Martin

    --
    perl -e '$|=1;&p(7.74.117.115.116.32);&s();&p(97.110.111);&p(116.104.101
    ..114);&s;&p(32.112.101.114.108);&s();&p(32.104.97.99.107.101.114.10);sub
    s{sleep 1};sub p(){print "@_"}'
    Martin Kissner, Mar 9, 2005
    #4
  5. Tony Curtis Guest

    >> On 9 Mar 2005 12:10:55 -0800,
    >> said:


    > I'm trying to load an environment file in a script and I
    > think it may be easiest to show it before explaining it.
    > I've got 2 files, /tmp/test.pl and /tmp/test.env. Here are
    > the contents.


    > test.env:
    > #!/usr/bin/ksh
    > export BLAH=TEST


    > test.pl:
    > #!/usr/local/bin/perl


    warnings & strict?

    > system('. /tmp/test.env');


    system() doesn't use ksh (hence the error message), and those
    settings will only exist within that transient child process.

    > print "\$BLAH is ${BLAH}" ;


    Now back to the parent, child's environment is gone! Also the
    child process would not have been able to set a variable
    inside the perl program.

    > When I run it I get: sh: BLAH=TEST: is not an identifier
    > $BLAH is


    You could write some perl to parse the export settings in the
    ksh script, and then set $ENV{} within the perl.

    Or wrap the perl up inside a script that does the source
    first.

    Is this a FAQ? I couldn't find anything obvious with -q.

    hth
    t
    Tony Curtis, Mar 9, 2005
    #5
  6. robin Guest

    try running in linux, and if that doesn't work, run it in a command
    prompt with the perl -M compiler, not that I know how to do that now;
    if that still doesn't work, try the exec command or use backtix.
    -robin
    robin, Mar 10, 2005
    #6
  7. robin wrote:

    > try running in linux, and if that doesn't work, run it in a command
    > prompt with the perl -M compiler, not that I know how to do that now;
    > if that still doesn't work, try the exec command or use backtix.


    Good God, where to start...

    He *is* running it in *nix, where do you think he's got ksh installed, and
    Perl in /usr/local/bin?

    The -M switch tells perl to load a module. It's got nothing at all to do
    with the Perl compiler.

    And last (but certainly not least) his use of system() vs. exec() or
    backticks has *nothing* to do with the problem.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 10, 2005
    #7
  8. wrote:
    > I'm trying to load an environment file in a script and I think it may
    > be easiest to show it before explaining it. I've got 2 files,
    > /tmp/test.pl and /tmp/test.env. Here are the contents.
    >
    > test.env:
    > #!/usr/bin/ksh
    > export BLAH=TEST
    >
    > test.pl:
    > #!/usr/local/bin/perl
    > system('. /tmp/test.env');
    > print "\$BLAH is ${BLAH}" ;
    >
    > When I run it I get:
    > sh: BLAH=TEST: is not an identifier
    > $BLAH is
    >
    > How can I get Perl to load the file properly? It does so from the
    > command line but throws the error when I run the script. For reasons I
    > can't get into I can't change the env file - the perl script needs to
    > be modified. Any help is greatly appreciated!!


    Using system() will spawn a new shell which will modify its own
    environment and pass any modifications on to its children. Nothing is
    exported upwards or sidewarts the process hierarchy. It's pretty much
    like trying to "cd" in a shell script and find that you stayed where you
    are in your current shell.

    There is, as usual, more than one way to do it:

    1. you could do the definition in a perl-file and "require" this file:
    test.env:
    our $BLAH = 'TEST';

    test.pl:
    #! /usr/bin/perl

    use warnings;
    use strict;
    our $BLAH;

    require "test.env";

    print "BLAH is $BLAH\n";

    2. you could read and parse the shell script:
    test.env:
    #!/usr/bin/ksh
    export BLAH=TEST

    test.pl:
    #! /usr/bin/perl

    use warnings;
    use strict;

    if (open ENV, '<', 'path/to/test.env') {
    while (<ENV>) {
    next if /^\s*#/;
    chomp;
    if (/^\s*(export)?\s+([^=]+)=(.*)/) {
    my $var = $2;
    my $value = $3;
    $value =~ s/^"(.*)"$/$1/;
    $ENV{$var} = $value;
    }
    }
    close ENV;
    }
    print $ENV{BLAH}, "\n";

    (Note that there are several traps lurking, e.g. the following would be
    accepted by this script but not by ksh:
    export BLAH=This is a test
    )

    HTH,

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Mar 10, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuël van Laere

    To dot or not to dot?

    Samuël van Laere, Oct 16, 2003, in forum: HTML
    Replies:
    8
    Views:
    417
    Samuël van Laere
    Oct 16, 2003
  2. Christopher M. Lusardi

    volatile struct in dot h vs dot c

    Christopher M. Lusardi, May 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    467
    Peter Shaggy Haywood
    May 15, 2004
  3. Nathan Sokalski
    Replies:
    11
    Views:
    691
    AAaron123
    Aug 14, 2009
  4. krishnan

    Dot Net Project Execution without Dot Net and Framework....

    krishnan, Jan 7, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    176
    krishnan
    Jan 7, 2006
  5. Replies:
    6
    Views:
    232
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
Loading...

Share This Page