473,441 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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 22605
@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

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

Similar topics

3
by: FPGA05 | last post by:
Hello All, I am developing a small application in which I would need a C++ application to read the output from a shell script. A shell script keeps looking for user inputs and once the user...
1
by: news | last post by:
At the end of a PHP script, I'm sending a file via FTP to a server. I thought it'd be best to use a shell script in order to automate the FTP (logging in, changing to binary, putting the file,...
0
by: Aashif | last post by:
I want to call Unix Shell script which is available in other Server (Unix server) from windows application using C#. Currently the shell script runs the C program but the GUI is not good, So I want...
9
by: sohan | last post by:
Hi, I want to know how to connect and execute a db2 query from inside a UNIX shell script. Details: We have a unix shell script. We need to execute multiple db2 sql queries from this shell...
3
by: telduivel | last post by:
Can someone please help me with this: I have a python script, that at some point calls a linux bash script (.sh). Starting the shell script is the last thing my python script needs to do, so I...
2
by: ellennolan | last post by:
Hello, I wonder if anyone can help with calling external shell script in c++. Basically, in the shellscript, I want to pass src, dst, md5. If the src's md5 matches md5 given, it will be link to...
5
by: inetquestion | last post by:
I am looking for a web interface for shell commands or shell scripts. Does anyone know of any exexisting php scripts which would solve this requirement? PHP form accepts input from a user, then...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
4
by: devi thapa | last post by:
Hi, I am executing a python script in a shell script. The python script actually returns a value. So, can I get the return value in a shell script? If yes, then help me out. Regards, Devi
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.