1: command not found

爱⌒轻易说出口 提交于 2019-12-04 13:10:26

Bash functions normally "return" values by printing them to standard output, where the caller can capture them using

`func args ...`

or

$(func args ...)

This makes functions work like external commands.

The return statement, on the other hand, sets the value of $?. Normally that's going to be set to 0 for success, 1 for failure, or some other value for a specified kind of failure. Think of it as a status code, not a general value. It's likely that this will only support values from 0 to 255.

Try this instead:

#!/bin/sh

div3() {
    expr $1 % 3 = 0   # prints "0" or "1"
}

d=$(div3 1)
echo $d

Note that I've also changed the shebang line from #!/usr/bin/env sh to #!/bin/sh. The #!/usr/bin/env trick is often used when invoking an interpreter (such as perl) that you want to locate via $PATH. But in this case, sh will always be in /bin/sh (the system would break in various ways if it weren't). The only reason to write #!/usr/bin/env sh would be if you wanted to use whatever sh command happens to appear first in your $PATH rather than the standard one. Even in that case you're probably better of specifying the path to sh directly.

The

 d=div3 1

line is the culprit because you assign the string div3 to the env variable d and try to execute 1. To avoid this, use backticks to assign the result of the evaluation instead:

 d=`div3 1`

Then, another error occurs in your evaluation function. You need to run test on your arguments instead of trying to evaluate them as a command:

return `test $(($1 % 3)) -eq 0`

Still no output, but no errors either. What would your expected output be, actually?

div3() {
    return $(( ( $1 % 3 ) == 0 ))
}

d=$(div3 1)
rc=$?

echo $d
echo $rc


(blank line)
0

Note, return just sets the value of $? for the function to return value, just print it.

div3() {
    printf $(( ( $1 % 3 ) == 0 )) "\n"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!