5

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?

5
  • 2
    Does your Perl script produce any other output to stdout? Commented May 7, 2016 at 8:41
  • You can use perl code to update your PATH and set an environment variable. Commented May 7, 2016 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
    Commented May 7, 2016 at 10:09
  • Put your perl scritp into your post. Commented May 7, 2016 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. Commented May 8, 2016 at 1:06

3 Answers 3

6

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.

0

To expand on the answer by @cas, here is an example where:

  • The shell script creates a temp file to hold the variables
  • Perl outputs the variables, which the shell script redirects to the temp file
  • The shell sources the temp file (and deletes it)

If you don't use STDERR in your perl script, you can use that to redirect it to the temp file with 2>$tempfile.

Here is a full example:

#!/usr/bin/env bash

# create a temp file to hold the variables from perl
tmpfile=$(mktemp perlvars.XXXX)

#perl writes them to STDERR, which is redirected to the $tmpfile
perl -e '$var1="Perl Variable 1"; $var2="Other variable";
         print "In Perl: \$var1 = $var1, \$var2 = $var2\n";
         END { print STDERR qq(var1="$var1"\nvar2="$var2"\n) }' 2>$tmpfile

# source the $tmpfile and remove it
. "$tmpfile" && rm "$tmpfile"

echo "Now back in Bash: var1 = '$var1', var2 = '$var2'"

I you do also need STDERR in Perl, like if using warnor die, you can open another file descriptor in Perl with open(F3, ">&=", 3), and write to that instead:

#!/usr/bin/env bash

# create a temp file to hold the variables from perl
tmpfile=$(mktemp perlvars.XXXX)

#perl writes them to file handle 3, which is redirected to the $tmpfile
perl -e '$var1="Perl Variable 1"; $var2="Other variable";
         print "In Perl: \$var1 = $var1, \$var2 = $var2\n";
         END {open(F3, ">&=", 3);
              print F3 qq(var1="$var1"\nvar2="$var2"\n)}' 3>$tmpfile

# source the $tmpfile and remove it
. "$tmpfile" && rm "$tmpfile"

echo "Now back in Bash: var1 = '$var1', var2 = '$var2'"
-3

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.

3
  • 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
    Commented May 7, 2016 at 9:36
  • @cas But you can have 2 shells at once can't I? Commented May 7, 2016 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
    Commented May 7, 2016 at 9:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .