Date: Mon, 14 Mar 2011 02:39:49 -0430 From: Andres Perera <andres.p@zoho.com> To: Maxim Khitrov <max@mxcrypt.com> Cc: FreeBSD <freebsd-questions@freebsd.org> Subject: Re: Shell script termination with exit function in backquotes Message-ID: <AANLkTikH%2Bn%2BuTDokiFSKKQ%2BsfkFFm6NLaFatfwOxVSQ=@mail.gmail.com> In-Reply-To: <AANLkTi=-CFmxRicGcosvzhBbM3DMjbWwQNirMrJ1_KP=@mail.gmail.com> References: <AANLkTi=-CFmxRicGcosvzhBbM3DMjbWwQNirMrJ1_KP=@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
ash tries to overoptimize by running certain command substitutions without a subshell On Sun, Mar 13, 2011 at 7:56 PM, Maxim Khitrov <max@mxcrypt.com> 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" >
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTikH%2Bn%2BuTDokiFSKKQ%2BsfkFFm6NLaFatfwOxVSQ=>