From owner-freebsd-questions@FreeBSD.ORG Mon Mar 14 07:09:51 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 4EAA51065675 for ; Mon, 14 Mar 2011 07:09:51 +0000 (UTC) (envelope-from 839273@gmail.com) Received: from mail-gy0-f182.google.com (mail-gy0-f182.google.com [209.85.160.182]) by mx1.freebsd.org (Postfix) with ESMTP id 04DC18FC12 for ; Mon, 14 Mar 2011 07:09:50 +0000 (UTC) Received: by gyg13 with SMTP id 13so889814gyg.13 for ; Mon, 14 Mar 2011 00:09:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:reply-to:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type:content-transfer-encoding; bh=3ZOS2TE3MHKWJLK+G3+DZ61VuXIcIN/06W9S1YP15vs=; b=PyeotxjuXmkew3lm4FgDonb7jWefOeX94ekIcLNNkzpbIE9GAbj5znTe9dVwbdNhpx ZoFrBtXdJ4vGL0VsIOkSdQY304TKhZIXSF2gSw2yXf17wfSGlPHRbTM2kQ2TEeEBS+k2 i5pCGV7RScIdOHIJIM5+Y9METJeJLhgzcIt74= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:reply-to:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=hudtjUWzvEv5aUnhClQZty5Hjkk95obBWyIV6qSSlLm1RVOacMrPmCVs6ewXUvxdja 0fYNinz4AnuVfin9uGdwxUnVyXgvX3aIliJJp8LvuGDMMY7D8/cPjBz3pc3Xqs4TBj5K sml/jfVaNn8zd8i5NBDAO9pbhTwZMMg5JxTEY= MIME-Version: 1.0 Received: by 10.150.50.17 with SMTP id x17mr5540580ybx.162.1300086590019; Mon, 14 Mar 2011 00:09:50 -0700 (PDT) Sender: 839273@gmail.com Received: by 10.150.216.2 with HTTP; Mon, 14 Mar 2011 00:09:49 -0700 (PDT) In-Reply-To: References: Date: Mon, 14 Mar 2011 02:39:49 -0430 X-Google-Sender-Auth: FSfPtluJpD2a4ep7WM53lID4fPk Message-ID: From: Andres Perera To: Maxim Khitrov Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: FreeBSD Subject: Re: Shell script termination with exit function in backquotes X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: 839273@gmail.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Mar 2011 07:09:51 -0000 ash tries to overoptimize by running certain command substitutions without a subshell On Sun, Mar 13, 2011 at 7:56 PM, Maxim Khitrov wrote: > Hello everyone, > > I might be doing something dumb here, but this doesn't make sense to > me. When I run the following script, I would expect to see no output: > > ---- > #!/bin/sh > > exit_prog() > { > =C2=A0 =C2=A0 =C2=A0 =C2=A0echo -n 'before' > =C2=A0 =C2=A0 =C2=A0 =C2=A0exit 0 > =C2=A0 =C2=A0 =C2=A0 =C2=A0echo -n 'after' > } > > echo line 1: `exit_prog` > echo line 2: > echo line 3: `exit 1` > echo line 4: > ---- > > The reason I expect to see no output is because 'exit 0' should be > called before any of the echo lines are allowed to execute. Instead, > what I get on FreeBSD 7 & 8 is: > > ---- > line 1: before > line 2: > ---- > > I don't understand this because 'exit 0' seems to terminate the call > to 'exit_prog', but the execution of the script continues. However, > when 'exit 1' is called, the script terminates before printing out the > last 2 lines. > > It seems that 'exit' inside a function doesn't work when that function > is called with backquotes. I assume it has something to do with the > fact that commands in backquotes are executed in a sub-shell, but the > behavior is inconsistent. > > When I run the same script on RHEL using bash, all 4 lines are printed: > > ---- > line 1: before > line 2: > line 3: > line 4: > ---- > > What's going on here? > > - Max > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.o= rg" >