469,328 Members | 1,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Sourcing a Shell Script

Hi,

I am learning perl to do some test automation. In my perl scripts I need to source shell script. Can someone tell me whats wrong with the code below.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. system "source source.csh";
  4.  
  5. $rajat = $ENV{'RAJAT'};
  6.  
  7. if (defined ($rajat)){
  8.     print "RAJAT defined\n";
  9. }
  10. else{
  11.     print "RAJAT not defined\n";
  12. }
  13.  
*****************************************
cat source.csh
setenv RAJAT 1

Is this the correct way to source a shell script from perl or is there any other way.
Mar 21 '07 #1
7 21395
@rajataggarwal

Yes, in principle, shell scripts can be sourced with "source <script>" (you also could use the dot-form ". <script>").

But, since system() starts a subshell, your environment variables do not get passed to the shell your perl script is running in. Even though your shell script exports variables, it will export them to the subshell, not to your actual shell.

What exactly are you trying to do within the shell script? Maybe there is another way to solve it.

Greetz, Doc
Mar 21 '07 #2
miller
1,089 Expert 1GB
Hello Rajat,

Doc is 100 percent correct as for my knowledge covers what you're asking. This is specifically an issue with unix. You can read about it breifly from perldoc:

perdoc perlfaq8 I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?

Another user also had a related issue a month ago:
TSDN Perl Forum - Adding ENV variables in linux

Anyway, as doc said, explain what you're ultimately trying to accomplish. It is likely there is a way around this specific requirement, but we won't be able to suggest anything unless you tell us more.

- Miller
Mar 22 '07 #3
Hi Doc, Miller,

Thanks for the information. I am working on automating build, release, download and regressions for a product. Though its not compulsory for me to use Perl but I thought I will get to learn perl so started writing perl scripts.

During the automation process, I need to sources lot of scripts from various vendors, These scripts are setting or appending lot of environment variables. So sourcing the script seemed the easiest way out to me. But if there is not way to source the csh script properly, then I might go back to either shell script or set the enviornment variables the csh script is setting in my perl script. Though I would not like to go for the later option. I get the scripts to be sourced from other vendors and if I set the environment variables, then everytime I get new scripts, I will have to go through them and correct my perl scripts.

If you have any way around please let me know.

Thanks,
Rajat
Mar 22 '07 #4
bidoun
1
I already had this issue with Perl three years ago. You can solve it by sourcing your script in a system command and then calling env to display environment variables you can then parse the output stream of the system command and extract the variable name and value with a regular expression then set inside the Perl code.

It can seem quiet complicated but you can do it in a couple of hours.

BR.
May 9 '07 #5
prn
254 Expert 100+
I thought I'd have a quick go at this just for laughs.

I created a shell file called "setone":
Expand|Select|Wrap|Line Numbers
  1. #! /bin/bash
  2. export RAJAT="found"
  3.  
Then a quick perl file:
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. use strict;
  3.  
  4. my $rajat = $ENV{'RAJAT'};
  5. print "rajat = $rajat\n";
  6.  
  7. my ($envar, $enval);
  8. open IN, ". ./setone; env |" 
  9.   or die "Could not run shell: $!\n";
  10. while ( <IN> ) {
  11.   chomp;
  12.   ($envar,$enval) = split /=/,$_,2;
  13.   $ENV{$envar} = $enval;
  14. }
  15. close IN;
  16.  
  17. $rajat = $ENV{'RAJAT'};
  18. print "rajat = $rajat\n";
  19.  
  20. print "bye\n";
  21. exit;
  22.  
The output of that is:
Expand|Select|Wrap|Line Numbers
  1. rajat =
  2. rajat = found
  3. bye
  4.  
That is, we started out without RAJAT as an environment variable, as we can see from the line "rajat = " used setone to set it within the perl script and then it was there. Pretty quick and painless. :)

Enjoy,
Paul
May 9 '07 #6
Two other methods would be to

1) source the script prior to the execution of the perl script in a wrapper script

OR:

2) from the perl script, "exec" execute a list of commands that 1) sets a flag, 2) does the source 3) reinvokes the same perl script .

when the perl script is called the second time, since it checks to see if the flag was set, it won't source the second time in and your file will be sourced in this environment.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. $script_to_source="/tmp/sourceme.sh";
  4.  
  5. if($ENV{'SOURCED_IT'} != 1)
  6. {
  7.   exec("SOURCED_IT=1;export SOURCED_IT;. $script_to_source ;$0 argument1 argument2 all arguments");
  8. }
  9.  
  10. print "$script_to_source has been sourced\n";
  11.  
  12. print "TESTVAR from source is set to $ENV{'TESTVAR'} \n";

SHELL SCRIPT TO SOURCE:
Expand|Select|Wrap|Line Numbers
  1. TESTVAR="test value"
  2. export TESTVAR


EXECUTION RESULT:
Expand|Select|Wrap|Line Numbers
  1. /tmp/sourceit.pl
  2. /tmp/sourceme.sh has been sourced
  3. TESTVAR from source is set to test value

I wasn't able to figure out how to easily pass the same arguments that were passed into the script origionall into the second execution... I assume if i added that feature, then it would be as complicated as the previous examples, perhaps this is not the best method, but it is an interesting method.
Nov 19 '08 #7
KevinADC
4,059 Expert 2GB
Welcome to bytes gbowdridge,

just watch those thread dates, this one was about 18 months old. ;)
Nov 19 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by news | last post: by
2 posts views Thread by ellennolan | last post: by
5 posts views Thread by inetquestion | last post: by
4 posts views Thread by devi thapa | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.