return a value to shell script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • devi thapa

    #1

    return a value to shell script

    Hi,

    I am executing a python script in a shell script. The python script
    actually returns a value.
    So, can I get the return value in a shell script? If yes, then help me out.

    Regards,
    Devi
  • Tom Wright

    #2
    Re: return a value to shell script

    devi thapa wrote:
    I am executing a python script in a shell script. The python script
    actually returns a value.
    So, can I get the return value in a shell script? If yes, then help me
    out.
    Yes. The variable $? should be bound to the return value of the last
    foreground program to exit. The python script can return a value using
    sys.exit(value)

    HTH


    --
    I'm at CAMbridge, not SPAMbridge

    Comment

    • D'Arcy J.M. Cain

      #3
      Re: return a value to shell script

      On Wed, 12 Nov 2008 13:09:21 +0000
      Tom Wright <tew24@spam.ac. ukwrote:
      devi thapa wrote:
      I am executing a python script in a shell script. The python script
      actually returns a value.
      So, can I get the return value in a shell script? If yes, then help me
      out.
      >
      Yes. The variable $? should be bound to the return value of the last
      foreground program to exit. The python script can return a value using
      sys.exit(value)
      That may be true for some shells but not all. The correct answer to
      the OP's question is - check the documentation for your specific shell
      or ask on a group dedicated to the shell you are using.

      --
      D'Arcy J.M. Cain <darcy@druid.ne t | Democracy is three wolves
      http://www.druid.net/darcy/ | and a sheep voting on
      +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

      Comment

      • Jeff McNeil

        #4
        Re: return a value to shell script

        On Nov 12, 8:19 am, "D'Arcy J.M. Cain" <da...@druid.ne twrote:
        On Wed, 12 Nov 2008 13:09:21 +0000
        >
        Tom Wright <te...@spam.ac. ukwrote:
        devi thapa wrote:
        I am executing a python script in a shell script. The python script
        actually returns a value.
        So, can I get the return value in a shell script? If yes, then help me
        out.
        >
        Yes. The variable $? should be bound to the return value of the last
        foreground program to exit. The python script can return a value using
        sys.exit(value)
        >
        That may be true for some shells but not all. The correct answer to
        the OP's question is - check the documentation for your specific shell
        or ask on a group dedicated to the shell you are using.
        >
        --
        D'Arcy J.M. Cain <da...@druid.ne t | Democracy is three wolveshttp://www.druid.net/darcy/ | and a sheep voting on
        +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
        It depends on what your exit value is, though. While your shell will
        probably provide some method for determining the exit status of your
        process, you're limited to a range of 0-255. If, for example, you've
        got your Python script exiting with the number of rows in a database,
        you'll simply overflow.

        [jeff@marvin ~]$ python -c 'import sys; sys.exit(0)' ; echo $?
        0
        [jeff@marvin ~]$ python -c 'import sys; sys.exit(1)' ; echo $?
        1
        [jeff@marvin ~]$ python -c 'import sys; sys.exit(255)' ; echo $?
        255
        [jeff@marvin ~]$ python -c 'import sys; sys.exit(256)' ; echo $?
        0

        Depending on what you're doing, printing from your Python program and
        capturing standard out might be a better approach.

        HTH,

        Jeff

        Comment

        • Grant Edwards

          #5
          Re: return a value to shell script

          On 2008-11-12, devi thapa <devi.thapa@gma il.comwrote:
          I am executing a python script in a shell script. The python
          script actually returns a value. So, can I get the return
          value in a shell script? If yes, then help me out.
          There are two ways to "return" something to a shell script.

          1) To return a success/fail indication, use sys.exit(n). n==0
          means success, n!=0 means fail.

          2) To return the end value of an operation or computation to a
          shell script, you write it to stdout as an ascii string.

          --
          Grant Edwards grante Yow! I'm wet! I'm wild!
          at
          visi.com

          Comment

          Working...