LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   return value from perl to shell script (http://www.linuxquestions.org/questions/programming-9/return-value-from-perl-to-shell-script-902247/)

csegau 09-10-2011 02:28 AM

return value from perl to shell script
 
Hi All,

i have a shell script which in turns calls a perl script.
this perl script return the file name, which i want in my calling shell script. How can i get the return value in my shell scipt.

---------------------------------------
ex:

shell.sh contains

read Latest_file <<< $( perl.pl arg1 )
-----------------------------------------

perl.pl returns the latest file with extension arg1

therefore in my perl script i use

my @list = `ls *.arg1`;
#then do some processing
#finally i return the latest file as
print $latest_file;

--------------------------------------------------


thanks in advance :)

unSpawn 09-10-2011 07:29 AM

Something like
Code:

Latest_file=$(perl.pl arg1)
but using all-caps variable names
Code:

VARIABLE=$(perl.pl arg1 2>/dev/null)
makes things easier to read and recognize.

Code:

function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq
http://wooledge.org/mywiki/BashPitfalls"; }


theNbomr 09-10-2011 09:41 AM

I would like to point out the distinction between the meaning of 'return' and, in this case, 'write to stdout'. A return value is the integer value returned by the application (exit code), and if the calling process is bash or most other shells, is automatically assigned to the shell variable '$?'. Conventionally, the value '0' (zero) means 'success', whatever that might imply with respect to the particular program. Non-zero values are conventionally used to indicate some form of failure. This is conceptually distinct from output written to either of the standard output streams, stdout and stderr.
The data written to stdout may be redirected to some other stream or file descriptor, or in the case of bash, may be used in command substitution, which seems to be the case here. Assigning to a bash variable using command substitution is common idiom in bash shell scripting, and that seems to be the concept that addresses the OP's question. The subject of command substitution is well addressed in several of unSpawn's links.

--- rod.


All times are GMT -5. The time now is 04:34 AM.