From owner-freebsd-questions@freebsd.org Sun Jun 5 23:03:52 2016 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id D85A5B6BA97 for ; Sun, 5 Jun 2016 23:03:52 +0000 (UTC) (envelope-from jd1008@gmail.com) Received: from mail-io0-x231.google.com (mail-io0-x231.google.com [IPv6:2607:f8b0:4001:c06::231]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id A14FD1682 for ; Sun, 5 Jun 2016 23:03:52 +0000 (UTC) (envelope-from jd1008@gmail.com) Received: by mail-io0-x231.google.com with SMTP id n127so2997046iof.3 for ; Sun, 05 Jun 2016 16:03:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-transfer-encoding; bh=WAdSuI4EOYkR5GqeNIzttliU+4ixKpfg/E/bxoXkwSo=; b=S0+TZzRZ8j2MxYfjhXIg1JTYyLdW+eWbJo0V7lGBdwD9hqUzjkNJjXwUK8QxGTtyS0 KVtE86RbyxyU/apCoyQjhzWdpnIpwGT1plPtqgIyzQ+/TrZtUpaOHwY2xbb64AkHr6I6 r1wR6oM6//4nG7VkkietkQoUAsaYixlp7BFwjU72AUnD59eQwHd2Ve1329IZA/KNGfDs +VpmCoXytIpHXU29OauvMvxRdVxlM4vZRTK+m/XDW+G233EwQP953p84a1XV47NT2bcc r2kjLOUuuh15j6pf1hGu/pszktIEakPlUtl/VcXKeqfHdlvg8j69fkM7WRz3ceMjWTUU ljJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-transfer-encoding; bh=WAdSuI4EOYkR5GqeNIzttliU+4ixKpfg/E/bxoXkwSo=; b=CsP4fy5U9ZaS0RzsajHUbbLQn874IgukBXshvuBPgGo1jaUxZG+xsKysngn2V2RiR3 oHFpt0oB/SB7UuQTJ4pUriHAtT04eDUtVfJCHZ3vvzw323KLqzaVOKS9zkjEoJxPEkzh JNnyVDoBeKrvh7CSP+HD+WnwHXk1DIQlaZfouFGjXgjRCBg0xy8rNhpK4WV3f0dB0amh shygGfQ/8EHC1QjMi6cEY2W3VS08g1oS/5ORfhJSbk5kWWDr18L5gWAqg/mOvMquB9sz CFN/4dGf879DXNv/loG8N77lG6M2/Sex3z22MGtji/K5rXZ5+FACXVMdszDXr/PfIv+d c7mA== X-Gm-Message-State: ALyK8tKathyRrYijS5O5QczSj0FkarN6P97tap63H9KphmhF9aqsMZXNTTtSTSFz1VGBmA== X-Received: by 10.107.7.161 with SMTP id g33mr11418066ioi.64.1465167831972; Sun, 05 Jun 2016 16:03:51 -0700 (PDT) Received: from localhost.localdomain (ip-64-134-232-31.public.wayport.net. [64.134.232.31]) by smtp.googlemail.com with ESMTPSA id i128sm7916429ioa.41.2016.06.05.16.03.50 for (version=TLSv1/SSLv3 cipher=OTHER); Sun, 05 Jun 2016 16:03:51 -0700 (PDT) Message-ID: <5754AFD5.70908@gmail.com> Date: Sun, 05 Jun 2016 17:03:49 -0600 From: jd1008 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: freebsd-questions@freebsd.org Subject: Re: sh[it] and What am I missing here? References: <31b2cfb1-1da8-9262-3f03-d964776c905e@columbus.rr.com> <575453F9.9070508@holgerdanske.com> <4daed7a2-9a0b-15d9-0bb2-31227f8fcddd@columbus.rr.com> <575492F8.6080504@holgerdanske.com> In-Reply-To: <575492F8.6080504@holgerdanske.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 05 Jun 2016 23:03:52 -0000 Guys, in shell semantics, a command returning non zero indicates some failure, whereas a command returning 0, indicates success. This is the convention. On 06/05/2016 03:00 PM, David Christensen wrote: > On 06/05/2016 10:15 AM, Baho Utot wrote: >> I only want to create a script ( sh script ) and run if from a clean >> machine with just base install nothing else and then run my sh script to >> build some ports. That's were the trouble lies. ie functions not >> returning status for example: >> >> test.sh >> chmod +x test.sh >> >> #!/bin/sh >> >> func() { >> echo "Yep it's me" >> return 1 >> } >> >> if [ func ] ; then # if [ 1 = func ] or if [ 1 -eq func >> ] doesn't work either >> echo "This works" >> fi >> >> ./test.sh >> >> [: func: unexpected operator > > If I run your code on my FreeBSD 10.1 box with Csh login shell: > > dpchrist@p42800e:~/sandbox/sh % uname -a > FreeBSD p42800e 10.1-RELEASE-p35 FreeBSD 10.1-RELEASE-p35 #0: Sat > May 28 03:02:45 UTC 2016 > root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC i386 > dpchrist@p42800e:~/sandbox/sh % echo $SHELL > /bin/csh > dpchrist@p42800e:~/sandbox/sh % cat baho-utot.sh > #!/bin/sh > > func() { > echo "Yep it's me" > return 1 > } > > if [ func ] ; then # if [ 1 = func ] or if [ 1 -eq > func ] doesn't work either > echo "This works" > fi > dpchrist@p42800e:~/sandbox/sh % ./baho-utot.sh > This works > > > I do not see the string "Yep it's me". > > > I see the string "This works". > > > I do not see an error message. > > > I get the same results if I change my login shell to Sh: > > $ echo $SHELL > /bin/sh > $ ./baho-utot.sh > This works > > > Enabling the -v and -x options for Sh provides a clue -- 'func' is not > getting called: > > $ echo $SHELL > /bin/sh > $ /bin/sh -v -x baho-utot.sh > #!/bin/sh > > func() { > echo "Yep it's me" > return 1 > } > > if [ func ] ; then # if [ 1 = func ] or if [ 1 -eq > func ] doesn't work either > echo "This works" > fi > + [ func ] > + echo 'This works' > This works > > > Calling 'func' and then testing '$?' (exit value special variable) > explicitly seems to help: > > $ cat baho-utot-2.sh > #!/bin/sh > > func() { > echo "func returning 1" > return 1 > } > > func2() { > echo "func2 returning 0" > return 0 > } > > func > if [ $? = 1 ] ; then > echo "func worked" > else > echo "func did not work" > fi > > func2 > if [ $? = 1 ] ; then > echo "func2 worked" > else > echo "func2 did not work" > fi > $ ./baho-utot-2.sh > func returning 1 > func worked > func2 returning 0 > func2 did not work > > > David > > _______________________________________________ > freebsd-questions@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to > "freebsd-questions-unsubscribe@freebsd.org" > . >