Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a shell script in which I am running perl script by below code.

perl perlscript.pl

In the perl script I have defined a variable called $circle. Now I want to use this variable value in my shell script. How can I call?

share|improve this question
2  
Does your Perl script produce any other output to stdout? – roaima May 7 at 8:41
    
You can use perl code to update your PATH and set an environment variable. – Dac Saunders May 7 at 8:58
    
please provide more details. e.g. sample output from your perlscript.pl and maybe the script itself (or a minimal versison of it). – cas May 7 at 10:09
    
Put your perl scritp into your post. – PersianGulf May 7 at 13:43
    
@Programmer400, you cannot. You can alter the variables while the perl script is running but once it exits, whatever it changed will exit along with it. – glenn jackman May 8 at 1:06
up vote 4 down vote accepted

If your perl script produces no other output than the value of $circle, you can use command substitution to store that output in a variable. For example:

circle=$(perl perlscript.pl)

If the perl script produces other output as well (or not output at all), you'll have to either:

  1. extract only the value you want from the output using the usual text processing tools (sed, awk, perl, grep, etc). Here's a very simple example:

    circle=$(perl perlscript.pl | sed -e 's/junk.i.dont.want//')

  2. use an indirect method, such as having the perl script write the value of $circle to a file (e.g. /path/to/circle) for your shell to read it (e.g. circle=$(cat /path/to/circle))

NOTE: Without more details from you, it's impossible to provide more than generic advice like this.

share|improve this answer

Just set an environment variable in perl and your shell will find it. In C you can check your path like this

void getPath() {
    if (getenv("PATH") == NULL) {
        printf("'%s' is not set.\n", "PATH");
        /* Default our path if it is not set. */
        putenv("PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc");
    }
    else if (getenv("PATH")) {
        printf("'%s' is set to %s.\n", "PATH", (getenv("PATH")));
    }
}

There are also functions for setting your path, so you can set an environment variable for your program in your code.

share|improve this answer
2  
child processes (e.g. a perl script) can't change the parent's (e.g. the shell that the perl script was run from) environment. – cas May 7 at 9:36
    
@cas But you can have 2 shells at once can't I? – Dac Saunders May 7 at 9:54
3  
yes, but a child process still can't change its parent's environment. or the environment of an entirely unrelated process (e.g. a 2nd shell). it can affect the environment of itself and any child processes it creates. – cas May 7 at 9:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.