From owner-freebsd-questions@FreeBSD.ORG Sat Mar 19 17:13:19 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 7DF271065687 for ; Sat, 19 Mar 2011 17:13:19 +0000 (UTC) (envelope-from max@mxcrypt.com) Received: from mail-vx0-f182.google.com (mail-vx0-f182.google.com [209.85.220.182]) by mx1.freebsd.org (Postfix) with ESMTP id 3F2CA8FC12 for ; Sat, 19 Mar 2011 17:13:18 +0000 (UTC) Received: by vxc34 with SMTP id 34so4943326vxc.13 for ; Sat, 19 Mar 2011 10:13:18 -0700 (PDT) Received: by 10.52.179.230 with SMTP id dj6mr302214vdc.249.1300554797141; Sat, 19 Mar 2011 10:13:17 -0700 (PDT) MIME-Version: 1.0 Received: by 10.220.159.19 with HTTP; Sat, 19 Mar 2011 10:12:47 -0700 (PDT) In-Reply-To: References: <759A467E-407A-4DB8-9756-08011B5405F0@vicor.com> From: Maxim Khitrov Date: Sat, 19 Mar 2011 13:12:47 -0400 Message-ID: To: Devin Teske Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: FreeBSD , Andres Perera , 839273@gmail.com Subject: Re: Shell script termination with exit function in backquotes X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 19 Mar 2011 17:13:19 -0000 On Sat, Mar 19, 2011 at 12:44 PM, Devin Teske wrote: > > On Mar 19, 2011, at 9:15 AM, Maxim Khitrov wrote: > >> On Mon, Mar 14, 2011 at 6:40 PM, Andres Perera wrote= : >>> On Mon, Mar 14, 2011 at 7:46 AM, Maxim Khitrov wrote: >>>> On Mon, Mar 14, 2011 at 3:16 AM, Andres Perera wro= te: >>>>> On Sun, Mar 13, 2011 at 9:49 PM, Devin Teske wrote= : >>>>>> If you make the changes that I've suggested, you'll have consistent = execution. The reason you're having inconsistent behavior is because Linux = has /bin/sh symbolically linked to /bin/bash while FreeBSD has a more tradi= tional shell (we'll call it bourne shell "plus"). >>>>> >>>>> that is misleading because command substitutions have traditionally >>>>> invoked subshells, and freebsd sh(1)/ash is an exception, not the nor= m >>>>> >>>>> in this case, ksh and bash deviates are clearly closer to standard >>>>> bourne behaviour >>>>> >>>> >>>> Thanks for that explanation. I can understand the benefits of >>>> optimizing away subshell execution, but that can clearly lead to >>>> unexpected behavior. Is there some documentation on when this >>>> optimization is utilized (i.e. the command executed without a >>>> subshell)? Would I be correct in assuming that it is only restricted >>>> to built-in commands that are known not to produce any output, such as >>>> 'exit'? >>> >>> i would check the source, autoconf docs, and http://www.in-ulm.de/~masc= heck/ >>> >>> netbsd has =C2=A0been patched to fix `exit 1`, according to the last si= te >> >> Here's another, but related, problem that I just ran into. The man page = reads: >> >> =C2=A0 =C2=A0 Commands may be grouped by writing either >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (list) >> =C2=A0 =C2=A0 or >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 { list; } >> =C2=A0 =C2=A0 The first form executes the commands in a subshell. =C2=A0= Note that built-in >> =C2=A0 =C2=A0 commands thus executed do not affect the current shell... >> >> Here's my script: >> >> ---- >> #!/bin/sh >> >> { A=3D1; }; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 echo $A >> echo | { B=3D2; }; =C2=A0 =C2=A0 =C2=A0echo $B >> { C=3D3; } > /dev/null; echo $C >> ---- >> >> And here's the output: >> >> ---- >> 1 >> >> 3 >> ---- >> >> Where did the '2' go? > > You're learning that there are deviations to the rule as-mentioned in the= man-page. I've learned this a long time ago :) My point is that these deviations should be noted in the man page to help eliminate such surprises. A single sentence would have sufficed in this case. > The reason for these deviations is quite simple in-fact... > > The shell needs to create a new set of stdin/stdout file-descriptors for = the block of commands that you've created, and executing said commands with= in a sub-shell achieves that. Something very similar to this should be noted in the man page. I figured out why my code wasn't working quickly after thinking about how data would be piped to stdin. Others may waste a lot of time trying to figure out why their code doesn't do what the man page states it should be doing. - Max