?
Solved

How to run shell script from perl

Posted on 2003-03-10
4
Medium Priority
?
509 Views
Last Modified: 2008-02-01
Hi

I want to run shell script from perl and access enviornment set by shell script.

for ex.

if shell script test.sh contains

export DIR=/local1/users/mydir

then

I want to use environment variable DIR in perl program which is set by shell script.

my email is nrusinh@yahoo.com

Thanks,
Saagar
0
Comment
Question by:nrusinh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 8105093
open S,"<test.sh" or die "can't open test.sh $!";
while( <S> ){
    $ENV{$1} = $2 if /export\s+(\w+)=(.*)/;
}
0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 100 total points
ID: 8105329
This is a non-trivial problem unless you can guarantee which shell and style a script was written for.  Ozo's answer works well for one style of ksh programming.  This is more complete.

open SH, "<test.sh" or die "can't open tesh.sh: $!";
while (<SH>) {
    chomp;
    # export VAR=VALUE
    $ENV{$1} = $2 if /\bexport\s+(\w+)\s*=\s*([^;]+)/;
    # VAR=VALUE; export VAR
    # regex should make sure the exported var is the same one that was set but I can't remember how to construct that
    # does NOT handle variables set on one line and exported on a different one
    $ENV{$1} = $2 if /(\w+)\s*=\s*([^;]+);\s+export\s+/;
    # set VAR=VALUE
    $ENV{$1} = $2 if /\bset\s+(\w+)\s*=\s*([^;]+)/;
    # setenv VAR=VALUE
    $ENV{$1} = $2 if /\bsetenv\s+(\w+)\s+([^;]+)/;
}
close SH;
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9697821
Nothing has happened on this question in over 7 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points [grade B] between ozo and wilcoxon. [Also, edit out asker's email addr.]

Please post any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question