Date: Sun, 5 Jun 2016 14:00:40 -0700 From: David Christensen <dpchrist@holgerdanske.com> To: freebsd-questions@freebsd.org Subject: Re: sh[it] and What am I missing here? Message-ID: <575492F8.6080504@holgerdanske.com> In-Reply-To: <4daed7a2-9a0b-15d9-0bb2-31227f8fcddd@columbus.rr.com> References: <31b2cfb1-1da8-9262-3f03-d964776c905e@columbus.rr.com> <575453F9.9070508@holgerdanske.com> <4daed7a2-9a0b-15d9-0bb2-31227f8fcddd@columbus.rr.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?575492F8.6080504>
