• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: do shell script without blocking?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: do shell script without blocking?


  • Subject: Re: do shell script without blocking?
  • From: Christopher Nebel <email@hidden>
  • Date: Wed, 16 Feb 2005 12:48:33 -0800

On Feb 16, 2005, at 9:06 AM, Adam K. Wuellner wrote:

On Feb 16, 2005, at 10:43 AM, Stephen Jonke wrote:

What does the & mean in that context? 2>&1 as opposed to 2>1?

I'd love an explanation in plain English, too... I suspect it's necessary to distinguish between file descriptors (0, 1, 2), and filenames. Perhaps 2>1 redirects STDERR to a file named "1", rather than combining STDERR and STDOUT. Just guessing, here.


Here, FWIW, is what the bash manpage has to say about it:

See, to me, the man page is perfectly clear. :-) Adam's guess is correct -- the extra "&" means you're redirecting to a file descriptor; saying "2>1" would redirect stderr to a file named "1" in the working directory. Seeing as how "> f 2>&1" means the same thing as "&> f", you could use either one; perhaps I'll alter the note.


As for *why* this is necessary, "do shell script" watches the command's stdout and stderr, and blocks until they're both closed, effectively saying "There's no more data for you to read." The redirect causes the shell to close them (from d.s.s.'s perspective) immediately. You'll have the same problem in any language that has a "run-a-command-and-return-the-ouput" mechanism (such as Perl's backticks), though typically you'll only need to redirect stdout.


--Chris Nebel AppleScript Engineering

Redirecting Standard Output and Standard Error
Bash allows both the standard output (file descriptor 1) and the stan-
dard error output (file descriptor 2) to be redirected to the file
whose name is the expansion of word with this construct.


There are two formats for redirecting standard output and standard
error:


              &>word
       and
              >&word

Of the two forms, the first is preferred. This is semantically equiva-
lent to


              >word 2>&1

[...]
 Duplicating File Descriptors
       The redirection operator

              [n]<&word

is used to duplicate input file descriptors. If word expands to one or
more digits, the file descriptor denoted by n is made to be a copy of
that file descriptor. If the digits in word do not specify a file
descriptor open for input, a redirection error occurs. If word evalu-
ates to -, file descriptor n is closed. If n is not specified, the
standard input (file descriptor 0) is used.


       The operator

              [n]>&word

is used similarly to duplicate output file descriptors. If n is not
specified, the standard output (file descriptor 1) is used. If the
digits in word do not specify a file descriptor open for output, a
redirection error occurs. As a special case, if n is omitted, and word
does not expand to one or more digits, the standard output and standard
error are redirected as described previously.

_______________________________________________ Do not post admin requests to the list. They will be ignored. Applescript-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: do shell script without blocking?
      • From: Gnarlodious <email@hidden>
References: 
 >Re: do shell script without blocking? (From: Martin Orpen <email@hidden>)
 >Re: do shell script without blocking? (From: Stephen Jonke <email@hidden>)
 >Re: do shell script without blocking? (From: "Adam K. Wuellner" <email@hidden>)

  • Prev by Date: Re: "Authorization API" (Was: do shell script)
  • Next by Date: Re: Quark 6.5 Insert text into insertion point Broken?
  • Previous by thread: Re: do shell script without blocking?
  • Next by thread: Re: do shell script without blocking?
  • Index(es):
    • Date
    • Thread