473,750 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sourcing a Shell Script

2 New Member
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 22827
docsnyder
88 New Member
@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 Recognized Expert Top Contributor
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
rajataggarwal
2 New Member
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 New Member
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 Recognized Expert Contributor
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
gbowdridge
1 New Member
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 Recognized Expert Specialist
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
3719
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 gives his inputs it runs a series of commands and writes an output to a file. Once the output is producd it needs to communicate to the C++ aplication which would then need to read from this file. This whole process repeats continuously. Is there a...
1
2381
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, etc.) (If this can all be done in PHP, that'd be great.) But, how do I tell the shell script what the file name is supposed to be? Here's what I have so far: from the PHP script:
0
3058
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 to create GUI in C# windows application and call that C program using Shell script so first I have to call unix shell script from C#. Please guide me friends.
9
78376
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 script and export the result to a file. Any code snippet on this will be helpful.
3
5452
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 would like for the python script to exit once the call has been made, without waiting for the shell script to finish. My question is: how do I call a shell script from a python script? and how do I do that such that control is tranferred to the...
2
3478
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 the dst, if ln fails, copy src to dst. If success, return 0, if fails, return 1. I tried both system, and execvp(fork) to work on it. As execvp never returns, so it cannot tell whether the shell script successfully performs or not. Even there is...
5
5083
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 passes these as arguments to a configurable shell script or OS command. I would like for the output generated from the shell script/command shall be displayed in a new javascript window once the form is submitted. Optimally a user should not...
7
6236
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 this shell script using the Subprocess module. Here is my code: def resultFromRunning_(command):
4
34309
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
9339
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.