From owner-freebsd-hackers@freebsd.org Mon Aug 19 17:30:35 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 2C700CC68B for ; Mon, 19 Aug 2019 17:30:35 +0000 (UTC) (envelope-from ian@freebsd.org) Received: from outbound3d.ore.mailhop.org (outbound3d.ore.mailhop.org [54.186.57.195]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 46C1Ct5PGYz45B3 for ; Mon, 19 Aug 2019 17:30:34 +0000 (UTC) (envelope-from ian@freebsd.org) ARC-Seal: i=1; a=rsa-sha256; t=1566235832; cv=none; d=outbound.mailhop.org; s=arc-outbound20181012; b=V2m6yg4MoWKOie00MVJE1ZTZPJoXb5tzIv/FsbaZqmyUSgN985ACYIwEOTJmOk5hdfY0V5EMrxSkp +Th+qoujxilpfW/5gayXrDsDJyBg6fDbIa8VX+V3JxcNN1sJnIZK1m6+lCg7dfr4bugvq5T+l3HZrv 6CMxi1YCusOoN+k+1Rk7qkfnSA0PD2LKWM3cJwF76tckEer8Yux4xFnP9RuHv0Y3XYmS/j3YXJZW8Q EC6gvZs8tVbQy8gq+UQuo8X2Qt8PaGdUBZ6t7YJe2/78fAfcKk45WGBURwruTVe959tM+XglTVQ2Ul 59weRGoVcg4xyUqPlOeRxtI7XpUIaMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=outbound.mailhop.org; s=arc-outbound20181012; h=content-transfer-encoding:mime-version:content-type:references:in-reply-to: date:to:from:subject:message-id:dkim-signature:from; bh=xSj4NU+X/f/mSJNkehQ84leLxjgAoNvEyXTZWLJT42o=; b=lUQPGNMN5kv09RKFcbRkwSxNxzJBZUzesHnVhj1cJrgQaER2wreZMCP0H559/Orfg4dq3BQpA94/C V8m1a0l4iMIzwsOzqkb0nT2IH/hAWXdofLYW1z3mS6UJUVz6eGMfummx4QS1Dd0bLU0iH6HrEkP57S A67gZHRPIFoI14/OqLj9shLvBMVkjAKdimcMVfssMKqEFj3epGedJg+v1KabPfhSqLwkFtcEaM1Ie0 w7bMdEUQ5lEDEYRA8jkvzkHPs1kUHxv/V9LvHY+tioObdJ96atGh26oTkmsI+5nh92aWMW69sz+wzs 81kQwbdRRj6sbOlCBmRP7515V0PFsyA== ARC-Authentication-Results: i=1; outbound3.ore.mailhop.org; spf=softfail smtp.mailfrom=freebsd.org smtp.remote-ip=67.177.211.60; dmarc=none header.from=freebsd.org; arc=none header.oldest-pass=0; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outbound.mailhop.org; s=dkim-high; h=content-transfer-encoding:mime-version:content-type:references:in-reply-to: date:to:from:subject:message-id:from; bh=xSj4NU+X/f/mSJNkehQ84leLxjgAoNvEyXTZWLJT42o=; b=ksVyZhJbWPfQDPBepsWC2yEkt4Tju0JgujFJD/EqcnutmhnfuO4bK0wYqDn57Y3G2lstF5ijmFd1O s69BEuEYplknJF67kSTtuDj9jbWf0/qHhgXMzgKisUKe0Cyr+CMElGABbZG4QnIeNuu3IfHpfUIN/N ry5b9kQ1KLpqG+6IeoNjmZk4FqBDKvYFkNphkTmoBs2LsHM5B2tYhbUiL1LxMUzo4NLrht8y2VPV8y xbXdUVBqvOZBRk09DJXSx+3J+WK0p1/i62EWypC8VaCdM3eeg85sfoEd09JpAZcFW7enmQqOhEBcE0 o3PLecilAaXe29F8xsp1nU0O4m1iZ7w== X-MHO-RoutePath: aGlwcGll X-MHO-User: 0a95f868-c2a7-11e9-b67b-cdd75d6ce7a8 X-Report-Abuse-To: https://support.duocircle.com/support/solutions/articles/5000540958-duocircle-standard-smtp-abuse-information X-Originating-IP: 67.177.211.60 X-Mail-Handler: DuoCircle Outbound SMTP Received: from ilsoft.org (unknown [67.177.211.60]) by outbound3.ore.mailhop.org (Halon) with ESMTPSA id 0a95f868-c2a7-11e9-b67b-cdd75d6ce7a8; Mon, 19 Aug 2019 17:30:31 +0000 (UTC) Received: from rev (rev [172.22.42.240]) by ilsoft.org (8.15.2/8.15.2) with ESMTP id x7JHUU5o076500; Mon, 19 Aug 2019 11:30:30 -0600 (MDT) (envelope-from ian@freebsd.org) Message-ID: Subject: Re: undefined behaviour of return statement in /bin/sh and missing man entry From: Ian Lepore To: Mihir Luthra , freebsd-hackers@freebsd.org Date: Mon, 19 Aug 2019 11:30:30 -0600 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5 FreeBSD GNOME Team Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 46C1Ct5PGYz45B3 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-2.98 / 15.00]; local_wl_from(0.00)[freebsd.org]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; NEURAL_HAM_SHORT(-0.98)[-0.983,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; ASN(0.00)[asn:16509, ipnet:54.186.0.0/15, country:US] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Aug 2019 17:30:35 -0000 On Mon, 2019-08-19 at 22:39 +0530, Mihir Luthra wrote: > Hi everyone, > > Recently I was writing script on my virtual machine where I needed to > know > If a /bin/sh script has been sourced. I found many ways on > stackoverflow, > most of which only seem to work for bash. Although after some > research, I > found a way that should have worked with /bin/sh. It was that if I > executed > return from a function or sourced script, it should work fine and in > other > cases it should have wrote to stderr. > > Although on executing return on command line I got logged out and on > executing same on non-login shells it just exited. > Also, there seem to be no entry for return statement on man page of > sh. [1] > > I have posted a brief explanation for the same in my stackoverfow > question. > [2]. > > [1] > https://www.freebsd.org/cgi/man.cgi?query=sh&manpath=Unix+Seventh+Edition > [2] > https://stackoverflow.com/questions/57547626/detect-if-a-script-has-been-sourced-in-bin-sh > You looked in the wrong set of manpages, right one [1] says... The syntax of the return command is return [exitstatus] It terminates the current executional scope, returning from the closest nested function or sourced script; if no function or sourced script is being executed, it exits the shell instance. The return command is implemented as a special built-in command. I think when I tried to solve this problem once, the only thing I came up with is the fact that $0 doesn't change when you source-in a script. So if a given script knows what its name should be, and the expansion of ${0##*/} doesn't match that name, it can mean the script was sourced in. Or it can mean the user renamed the file or executed it through a link, so it's not foolproof. [1] https://www.freebsd.org/cgi/man.cgi?query=sh -- Ian