From owner-freebsd-questions@freebsd.org Sun Jun 5 22:33:41 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 37BC8B6B022 for ; Sun, 5 Jun 2016 22:33:41 +0000 (UTC) (envelope-from baho-utot@columbus.rr.com) Received: from cdptpa-oedge-vip.email.rr.com (cdptpa-outbound-snat.email.rr.com [107.14.166.229]) by mx1.freebsd.org (Postfix) with ESMTP id E6EB01329 for ; Sun, 5 Jun 2016 22:33:40 +0000 (UTC) (envelope-from baho-utot@columbus.rr.com) Received: from [75.187.32.8] ([75.187.32.8:53125] helo=raspberrypi.bildanet.com) by cdptpa-oedge02 (envelope-from ) (ecelerity 3.5.0.35861 r(Momo-dev:tip)) with ESMTP id D0/75-31413-EB8A4575; Sun, 05 Jun 2016 22:33:34 +0000 Received: from [192.168.1.40] (helo=baho-utot.bildanet.com) by raspberrypi.bildanet.com with esmtp (Exim 4.84) (envelope-from ) id 1b9gbt-0006nH-O8 for freebsd-questions@freebsd.org; Sun, 05 Jun 2016 18:33:33 -0400 Subject: Re: sh[it] and What am I missing here? To: freebsd-questions@freebsd.org 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> From: Baho Utot Message-ID: <9d8dcc37-3047-4fab-abda-e10d3cf29893@columbus.rr.com> Date: Sun, 5 Jun 2016 18:33:33 -0400 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: <575492F8.6080504@holgerdanske.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-RR-Connecting-IP: 107.14.168.130:25 X-Cloudmark-Score: 0 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 22:33:41 -0000 On 06/05/16 17:00, 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 > Ok Thanks I'll give it a go