Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

I need to export variable as another user

by rahulruns (Sexton)
on Aug 06, 2012 at 12:05 UTC ( #985681=perlquestion: print w/ replies, xml ) Need Help??
rahulruns has asked for the wisdom of the Perl Monks concerning the following question:

I need to export variable as another user and need to confirm whether the variable is exported or not How Do I acheive that. I am trying to append the export command to a file and run the file as script but is not working

my $pig_file = system("ls /home/mrdemo1/pig.jar"); my $pig_directory = system("ls /home/mrdemo1/pigtmp"); open (PH,">>/home/mrdemo1/pig_profile_tmp") or die $!; my $pig_directory_path = `echo \'export PIGDIR=/home/mrdemo1\' >> /hom +e/mrdemo1/pig_profile_tmp`; my $hadoop_site_path = `echo \'export HADOOPSITEPATH=/etc/hadoop/conf\ +' >> /home/mrdemo1/pig_profile_tmp`; my $bash_profile_run = system(". /home/mrdemo1/pig_profile_tmp); my $pig_directory_path_exist = `runuser -l mrdemo1 -c 'echo \$PIGDIR'` +; my $hadoop_site_path_exist = `runuser -l mrdemo1 -c 'echo \$HADOOPSITE +PATH'`; chomp($pig_directory_path_exist); chomp($hadoop_site_path_exist); print " PATH IS $pig_directory_path_exist and $hadoop_site_path_exist" +; #if ( "$pig_directory_path_exist" ne "/home/mrdemo1" || "$hadoop_site_ +path_exist" ne "/etc/hadoop/conf" ) #{ # print " PIGDIR NOT SET. SET PIGDIR TO /home/mrdemo1 using comm +and ..................runuser -l mrdemo1 -c 'export PIGDIR=/home/mrde +mo1' OR SET HADOOPSITEPATH TO /etc/hadoop/conf using command runuser +-l mrdemo1 -c 'export HADOOPSITEPATH=/etc/hadoop/conf' OR SET BOTH \n +\n"; # print " Exiting...................\n\n"; #} output [root@r01mgt ~]# perl testscript.pl Bareword found where operator expected at testscript.pl line 12, near +"print " PATH" (Might be a runaway multi-line "" string starting on line 7) (Do you need to predeclare print?) String found where operator expected at testscript.pl line 13, near "# +if ( "" (Might be a runaway multi-line "" string starting on line 12) (Missing semicolon on previous line?) Scalar found where operator expected at testscript.pl line 13, near "# +if ( "$pig_directory_path_exist" (Missing operator before $pig_directory_path_exist?) String found where operator expected at testscript.pl line 13, near "$ +pig_directory_path_exist" ne "" (Missing operator before " ne "?) String found where operator expected at testscript.pl line 13, near "m +rdemo1" || "" Scalar found where operator expected at testscript.pl line 13, near "" + || "$hadoop_site_path_exist" (Missing operator before $hadoop_site_path_exist?) String found where operator expected at testscript.pl line 13, near "$ +hadoop_site_path_exist" ne "" (Missing operator before " ne "?) String found where operator expected at testscript.pl line 15, near "# + print "" (Might be a runaway multi-line "" string starting on line 13) (Missing semicolon on previous line?) Bareword found where operator expected at testscript.pl line 15, near +"# print " PIGDIR" (Missing operator before PIGDIR?) Bareword found where operator expected at testscript.pl line 15, near +"'export PIGDIR=/home/mrdemo1' OR" (Missing operator before OR?) Bareword found where operator expected at testscript.pl line 15, near +"'export HADOOPSITEPATH=/etc/hadoop/conf' OR" (Missing operator before OR?) Backslash found where operator expected at testscript.pl line 15, near + "BOTH \" (Do you need to predeclare BOTH?) Backslash found where operator expected at testscript.pl line 15, near + "n\" String found where operator expected at testscript.pl line 16, near "# + print "" (Might be a runaway multi-line "" string starting on line 15) (Missing semicolon on previous line?) Bareword found where operator expected at testscript.pl line 16, near +"# print " Exiting" ...........

Comment on I need to export variable as another user
Download Code
Re: I need to export variable as another user
by Corion (Pope) on Aug 06, 2012 at 12:13 UTC
    Bareword found where operator expected at testscript.pl line 12

    Your posted script does not have 12 lines. Please post the reduced script and the error messages that it produces instead of making things up.

    Also, you have already been told about perlvar and %ENV. Also see Get default login environment.

      I have updated the script but other lines are hashed out

        Perl tells you line numbers in its error messages. Consider reading these error messages and looking at the lines in your original script that correspond to these line numbers.

        As you keep your original script hidden from us, and don't tell us what line numbers Perl tells you for the script you posted, we cannot help you.

Re: I need to export variable as another user
by moritz (Cardinal) on Aug 06, 2012 at 12:18 UTC

    The error messages don't match the script you have posted (maybe there's more to the script, and the part that you haven't shown us causes problems?)

    Also please read the documentation of system. It doesn't do what you seem think it does.

    Also lines like my $bash_profile_run = system(". /home/mrdemo1/pig_profile_tmp); are pointless. The sourcing of the pig_profile_tmp happens only in the subshell that system has spawned for you. And then that subshell exits, and you lose all changes that pig_profile_tmp made to the environment.

Re: I need to export variable as another user
by Ratazong (Prior) on Aug 06, 2012 at 12:19 UTC
    Have a look at the first error-message:
    Bareword found where operator expected at testscript.pl line 12, near +"print " PATH" (Might be a runaway multi-line "" string starting on line 7)
    I suppose the following is line 7, as the closing " is missing:
    my $bash_profile_run = system(". /home/mrdemo1/pig_profile_tmp);
    HTH, Rata

      Corrected the error but still not getting output

      output /home/mrdemo1/pig.jar excite.log.bz2 pig_1344241951706.log pig_1344244494512.log p +ig_1344245634547.log pig.jar script1-local-results.txt +tutorial.jar excite-small.log pig_1344243339709.log pig_1344245480551.log p +ig_1344246062938.log script1-hadoop.pig script2-hadoop.pig pig_1344241663743.log pig_1344243831809.log pig_1344245595053.log p +ig_1344246760544.log script1-local.pig script2-local.pig PATH IS and
Re: I need to export variable as another user
by aitap (Deacon) on Aug 06, 2012 at 12:39 UTC

    You have been already told to use %ENV to work with these variables. Try something like this:

    if (fork == 0) { # create a subprocess $ENV{myvariablename} = "desired variable value"; # set the variable ... exec("/path/to/program","some","program","arguments"); # run the program }

    If you need to write shell commands, just run a shell and write your shell commands into it:

    open my $shell, "|/bin/sh" || die "$!\n"; print $shell "export var=value\n"; ... print $shell "some complicated shell command # ;(){:|:&};:\n"; print $shell "/run/the/program --with-args\n";
    Or use multi-line system(), for example, with here-doc:
    system <<"END"; export var=value ... some() {shell commands} /run/some/program END

    Sorry if my advice was wrong.
Re: I need to export variable as another user
by aaron_baugher (Deacon) on Aug 06, 2012 at 12:55 UTC

    Aside from the many errors in your script, what you're trying to do won't work. A shell's environment is only passed to its child processes (if exported), not its parent or any siblings. So when you open a subshell with system in line 6 and set some environment variables for that subshell by running a couple of export commands, those variables go away as soon as that subshell closes and the system command returns. They will not be passed to the subshells started by your next two commands.

    So you have a few options: only open one subshell, and talk to it with a two-way pipe. That could be done with a module like IPC::Open3, or a few other ways. You could also put your export commands in an file that's automatically sourced by your particular shell, but that would depend on your shell. Another (maybe simplest) option would be to set your environment variables within your Perl script by using the %ENV hash, so they can be inherited by any subshells. No need to try to export them in a dot-profile:

    print 'Before setting: ', `echo \$MYVAR`; $ENV{MYVAR} = 'Howdy all!'; print 'After setting: ', `echo \$MYVAR`;

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Re: I need to export variable as another user
by rpnoble419 (Pilgrim) on Aug 06, 2012 at 21:58 UTC
    Have you tried saving the variables to a disk file using YAML? YAML allows you to save your variables and then read them back in at a later time or as I use it to set up a global configuration file between all of my Perl scripts. Using YAML also allows you to pass very complex data between applications that otherwise would cause a script to fail if the same data was passed as a command line argument.

    YAML
Re: I need to export variable as another user
by pvaldes (Chaplain) on Aug 07, 2012 at 15:53 UTC
    my $pig_file = system("ls /home/mrdemo1/pig.jar"); my $bash_profile_run = system(". /home/mrdemo1/pig_profile_tmp);

    nonsense, use something like this instead:

    my $pig_file = '/home/mrdemo1/pig.jar'; # etc...

    see also opendir, File::basename and readdir

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985681]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-08-30 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls