From owner-freebsd-questions@freebsd.org Sun Jun 5 21:00:43 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 13A42B6AA8D for ; Sun, 5 Jun 2016 21:00:43 +0000 (UTC) (envelope-from dpchrist@holgerdanske.com) Received: from holgerdanske.com (holgerdanske.com [IPv6:2001:470:0:19b::b869:801b]) by mx1.freebsd.org (Postfix) with SMTP id 071CF144E for ; Sun, 5 Jun 2016 21:00:43 +0000 (UTC) (envelope-from dpchrist@holgerdanske.com) Received: from ::ffff:99.100.19.101 ([99.100.19.101]) by holgerdanske.com for ; Sun, 5 Jun 2016 14:00:30 -0700 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> From: David Christensen Message-ID: <575492F8.6080504@holgerdanske.com> Date: Sun, 5 Jun 2016 14:00:40 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.8.0 MIME-Version: 1.0 In-Reply-To: <4daed7a2-9a0b-15d9-0bb2-31227f8fcddd@columbus.rr.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 21:00:43 -0000 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