Angle brackets in command-line arguments?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Hughitt

    Angle brackets in command-line arguments?

    Hi all,

    I am using someone else's script which expects input in the form of:

    ./script.py <arg1arg2

    I was wondering if the angle-brackets here have a special meaning? It
    seems like
    they specify an input and output stream to use in place of the
    console. I could not
    find anything in the python manual or Python in a Nut-shell though.

    Anyone know?


    Thanks,
    Keith
  • Marc 'BlackJack' Rintsch

    #2
    Re: Angle brackets in command-line arguments?

    On Wed, 16 Jul 2008 07:53:56 -0700, Keith Hughitt wrote:
    I am using someone else's script which expects input in the form of:
    >
    ./script.py <arg1arg2
    >
    I was wondering if the angle-brackets here have a special meaning? It
    seems like they specify an input and output stream to use in place of the
    console. I could not find anything in the python manual or Python in a
    Nut-shell though.
    >
    Anyone know?
    That's not Python's business but the command shell's. Those characters
    are used for redirecting input and ouput from/to files in shells, so it
    should be covered in the documentation of the shell you are using.
    Including ways to protect the characters, so they reach the called program
    in arguments.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Fredrik Lundh

      #3
      Re: Angle brackets in command-line arguments?

      Keith Hughitt wrote:
      I am using someone else's script which expects input in the form of:
      >
      ./script.py <arg1arg2
      <argis a common notation for "replace with argument value", so it
      could be that they're just expecting you to type:

      ./script.py arg1 arg2

      Alternatively, they meant

      ./script.py <arg1 >arg2

      in which case arg1 and arg2 are filenames. This is most commonly used
      with tools that process textfiles in some way. In the latter case, you
      can try the script simply by running:

      ./script.py

      and then typing the input to the terminal (use control-D on Unix or
      control-Z on Windows to terminate).

      Do the instructions use more meaningful names than "arg1" and "arg2"?

      </F>

      Comment

      • Gary Herron

        #4
        Re: Angle brackets in command-line arguments?

        Keith Hughitt wrote:
        Hi all,
        >
        I am using someone else's script which expects input in the form of:
        >
        ./script.py <arg1arg2
        >
        I was wondering if the angle-brackets here have a special meaning? It
        seems like
        they specify an input and output stream to use in place of the
        console. I could not
        find anything in the python manual or Python in a Nut-shell though.
        >
        Anyone know?
        >
        >
        Thanks,
        Keith
        --

        >

        In most Unix/Linux and related OS shells, the angled brackets *do*
        specify input and output streams as you surmise. However, they are
        *not* seen by the script as command line arguments. (And they are
        *not* brackets, and do not need to be matched. )

        For any command,
        cmd < file
        redirects the contents of file to cmd's standard input, which in Python
        is accessed by reading from sys.stdin (use input or raw_input or
        sys.stdin.read. ..)

        Also for any command,
        cmd file
        redirects the output of cmd to the named file. In Python this can be
        accessed using print, sys.stdout.writ e, ...

        Anything written to sys.stderr will not be caught by the ">"
        redirection, ans so will probably end up on the screen instead of in file.

        Also various shells will provide similar functionality using a variety
        of similar syntaxes: <<, >>, >&, and |, and so on.

        Gary Herron


        Comment

        • Keith Hughitt

          #5
          Re: Angle brackets in command-line arguments?

          On Jul 16, 11:16 am, Gary Herron <gher...@island training.comwro te:
          Keith Hughitt wrote:
          Hi all,
          >
          I am using someone else's script which expects input in the form of:
          >
               ./script.py <arg1arg2
          >
          I was wondering if the angle-brackets here have a special meaning? It
          seems like
          they specify an input and output stream to use in place of the
          console. I could not
          find anything in the python manual or Python in a Nut-shell though.
          >
          Anyone know?
          >>
          In most Unix/Linux and related OS shells,  the angled brackets *do*
          specify input and output streams as you surmise.  However, they are
          *not* seen by the script  as command line arguments.  (And they are
          *not* brackets, and do not need to be matched. )
          >
          For any command,
            cmd < file
          redirects the contents of file to cmd's standard input, which in Python
          is accessed by reading from sys.stdin (use input or raw_input or
          sys.stdin.read. ..)
          >
          Also for any command,
            cmd file
          redirects the output of cmd to the named file.  In Python this can be
          accessed using print, sys.stdout.writ e, ...
          >
          Anything written to sys.stderr will not be caught by the ">"
          redirection, ans so will probably end up on the screen instead of in file..
          >
           Also various shells will provide similar functionality using a variety
          of similar syntaxes:  <<, >>, >&, and |, and so on.
          >
          Gary Herron
          Thanks all for the quick response. I should have known it was shell-
          related. I haven't ever had to use this
          kind of redirection before, mostly just the single-bracket version to
          feed the contents of a file into some
          command.

          The reason it was causing me concern in the first place was that I was
          trying to execute a python script
          from Apache Ant, and it was failing. It turned out that when I escaped
          the angle brackets in Ant, they were
          there-after treated as normal command-line arguments, so when the
          script was then executed, it just had
          some additional values in sys.argv.

          I ended up getting around the issue by creating a small bash-script
          which simply wraps input in angle brackets
          and then executes the python script itself. That way I didn't have to
          escape anything in in Ant, and things
          worked well.

          Thanks everyone for taking the time to explain things to me. I really
          appreciate the help :)

          Take care,
          Keith

          Comment

          Working...