Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
hi all, i want to export a sytem variable using perl script. export xyz=1 can i do it with system command? is there any other commands for this? thanks in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Location
    Bangalore
    Posts
    1

    how to use export in perl system command


    hi all,

    i want to export a sytem variable using perl script.
    export xyz=1
    can i do it with system command?
    is there any other commands for this?

    thanks in advance

  2. #2
    Linux Newbie
    Join Date
    Jul 2005
    Location
    Chd,India
    Posts
    135
    In a perl script if you try to do
    Code:
    system("export ABC=1");
    It dosent work

    I think the reason is because export exports a variable only for that instance of shell.When u call system inside a perl script , it starts a new shell.So when system returns , the changes are gone

    What you want to do can be achieved by adding the line
    Code:
    system("export ABC=1");
    to the .bashrc file in your home directory from the perl script

    There could be other ways i dont know of.If u find one do post it.
    The strong shall live and the weak will die
    In the end,only the fittest survive in this world
    - Shishio Makoto

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I've never tried this, but is it possible to effect such a change through the %ENV hash?

    I've never tried adding/changing a value in there, but you might try giving it a shot.

  4. $spacer_open
    $spacer_close
  5. #4
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    I am quite sure that you CAN NOT do it with the 'system' command...

    Setting a value in %ENV should be good for anything spawned/forked from the perl script process itself -- but, I do not believe it can be used to affect the environment that the perl program was called from...

    At the moment, I can't think of any way to "export" from perl back to the parent process - other than something IPC-based - if anyone knows of a more direct way -- I would sure like to know it.

  6. #5
    Just Joined!
    Join Date
    Nov 2005
    Location
    Switzerland
    Posts
    2

    Re: how to use export in perl system command

    Quote Originally Posted by kavithanayar
    export xyz=1
    well, I googled and find your question. The way I solved it is by using a subshell

    Code:
    system("sh -c \"xyz=1 /bin/yourprog\"");
    will export xyz for yourprog

    HTH

  7. #6
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028
    Try
    Code:
    system("echo 'VAR=VALUE'>~/.bashrc");
    But that assumes the user uses Bash. Anyone know of a shell-independent file to write to (/etc/profile doesn't work because that's for every user, and needs root permissions to write to)?

  8. #7
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Um...that command will actually wipe out your ~/.bashrc file. You would want to use ">>" to redirect the output, not ">".

    And even in this case, you would need to resource / relogin in order to make the effects.

  9. #8
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028
    OK then,
    Code:
    system("echo 'VAR=VALUE'>>~/.bashrc;source ~/.bashrc");

  10. #9
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Doesn't seem to work:

    Code:
    alex@tux ~/test $ cat test_export
    #!/usr/bin/perl -w
    
    system "echo 'VAR=\"VALUE\"' >> ~/.bashrc; source ~/.bashrc";
    alex@tux ~/test $ ./test_export
    alex@tux ~/test $ echo $VAR
    
    alex@tux ~/test $
    However, once I am out of the script, I can THEN source the ~/.bashrc and it works.

    So yeah...

  11. #10
    Just Joined!
    Join Date
    Nov 2005
    Location
    Switzerland
    Posts
    2

    not sure want you want to do

    Quote Originally Posted by Cabhan
    Doesn't seem to work:
    do you want to change a variable for all subsequent login (=configuring your home) or export a variable for a program called by system (see my answer above) or source a perl program in the current shell (man sh)

    one more try

    Code:
    $ cat export.pl  
    #!/bin/perl
    print "export VAR=VALUE\n"
    $ `./export.pl`
    $ echo $VAR    
    VALUE

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •