Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to run shell script from perl

Posted on 2003-03-10
4
Medium Priority
?
513 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
3 Comments
 
LVL 85

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 27

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

Independent Software Vendors: 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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

572 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