it doesn't work the way you showed it (I added the 3 lines you mentioned right after the last EOF (curly bracket seems unnecessary) and it gave me syntax error at the str3=$... line).
Um. "I did it differently from way you showed it, and it doesn't work."
Why am I not surprised? Why are you surprised?
The "curly bracket" (the parenthesis, actually) closes the opening parenthesis on the 'perl' line, and is indeed necessary - at least if you want the script that I wrote to work.
The core of your question seems to be "can I set a variable in Perl and read it from the shell". The answer is no, you cannot: the Perl instance that you're launching is a child process of the shell. Any variable it sets disappears when the child process exits. The only way to keep the content of that variable is to either save it in some external resource (e.g., a file) or to make it part of the output from the Perl process and capture that output in the shell. The mechanism for doing the latter is called "command substitution" - which my script demonstrates.
One possible (and possibly fraught) exception is if the value that you're trying to pass is a number, 0-255. You could then use that value as the exit status of your script, and read that status via the '$?' variable in the shell. Although this is usable, and in fact used by some standard programs (e.g., "mail"), it's not recommended; that exit flag is normally used to determine whether a program succeeded or failed.
In reply to Re^3: pass perl variable to shell script
by oko1
in thread pass perl variable to shell script
by arthurs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |