0

I can return 1 or 0 from a nested function and check for it as an error.

two()
{
        if [ $1 = "right" ]; then
                return 0
        else
                return 1
        fi  
}

one()
{
        return $(two $1)
}

var="right"

if one $var; then
        echo OK for $var
else
        echo ERROR for $var
fi

var="wrong"

if one $var; then
        echo OK for $var
else
        echo ERROR for $var
fi

This give me:

$ sh error.sh 
OK for right
ERROR for wrong

But when I try to pass more parameters, it won't work.

two()
{
        echo info: $2
        if [ $1 = "right" ]; then
                return 0
        else
                return 1
        fi  
}

one()
{
        return $(two $1 $2)
}

var="right"
info="something"

if one $var $info; then
        echo OK for $var
else
        echo ERROR for $var
fi

var="wrong"

if one $var $info; then
        echo OK for $var
else
        echo ERROR for $var
fi

I'm passing parameters exactly the same way.

$ sh error.sh 
error.sh: line 13: return: info:: numeric argument required
ERROR for right
error.sh: line 13: return: info:: numeric argument required
ERROR for wrong

I've also tried the following:

two()
{
        echo info: $2
        if [ $1 = "right" ]; then
                return 0
        else
                return 1
        fi  
}

one()
{
        info="something"
        return $(two $1 $info)
}

var="right"

if one $var; then
        echo OK for $var
else
        echo ERROR for $var
fi

var="wrong"

if one $var; then
        echo OK for $var
else
        echo ERROR for $var
fi

As well as:

...
one()
{
        return $(two $1 "something")
}
...
Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
  • `$(cmd)` is **not** the exit status of `cmd` but the output (in this case the empty string). Use `cmd; return $?`. – Socowi Jul 29 '19 at 20:07
  • FYI, `[ $1 = "right" ]` is exactly backwards. It needs to be `[ "$1" = right ]`; expansions are subject to string-splitting and glob expansion unless quoted, whereas the word `right` has no other meaning that the parser needs to be prevented from interpreting it with. – Charles Duffy Jul 29 '19 at 20:09
  • I'm not sure what `$(cmd)` you're talking about. – Alexander Kleinhans Jul 29 '19 at 20:09
  • @Displayname, they presumably mean `$(two one)`. That evaluates to the **output written to stdout** by `two`, not its returned exit status. – Charles Duffy Jul 29 '19 at 20:10
  • @Displayname, ...if you want to refer to return value, then `one() { info="something"; two "$1" "$info"; }` will do. Or, in your first example, `one() { two "$1"; }` -- because the default return value is what was returned by the last command you ran, there's no need for a `return` or a `$(...)` there at all. – Charles Duffy Jul 29 '19 at 20:12
  • BTW, http://shellcheck.net/ is a resource it's always worth using. – Charles Duffy Jul 29 '19 at 20:13
  • `two "$1" "info"` worked. Leave an answer, I'll check it. Thanks! – Alexander Kleinhans Jul 29 '19 at 20:15
  • @Socowi I understand what you mean now. – Alexander Kleinhans Jul 29 '19 at 20:17

0 Answers0