bash 1:找不到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8002083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:01:34  来源:igfitidea点击:

1: command not found

bashvariablesnumberssetsh

提问by mcandre

I'm writing a divides-by-three function in Bash, and it won't let me set a variable to a number.

我正在 Bash 中编写一个除以三的函数,它不会让我将变量设置为数字。

fizzy.sh:

嘶嘶声:

#!/usr/bin/env sh

div3() {
    return ` % 3 -eq 0`
}

d=div3 1
echo $d

Example:

例子:

$ ./fizzy.sh 
./fizzy.sh: line 7: 1: command not found

回答by Keith Thompson

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

Bash 函数通常通过将值打印到标准输出来“返回”值,调用者可以使用

`func args ...`

or

或者

$(func args ...)

This makes functions work like external commands.

这使得函数像外部命令一样工作。

The returnstatement, 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.

return另一方面,该语句设置 的值$?。通常,它会被设置为 0 表示成功,1 表示失败,或者其他一些指定类型的失败值。将其视为状态代码,而不是一般值。这很可能只支持 0 到 255 之间的值。

Try this instead:

试试这个:

#!/bin/sh

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

d=$(div3 1)
echo $d

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

请注意,我还将shebang 行从更改#!/usr/bin/env sh#!/bin/sh#!/usr/bin/env在调用perl要通过 定位的解释器(例如)时,经常使用该技巧$PATH。但在这种情况下,sh始终存在/bin/sh(如果不是,系统会以各种方式中断)。编写的唯一原因#!/usr/bin/env sh是,如果您想使用sh出现在您的$PATH而不是标准命令中的任何命令。即使在这种情况下,您最好sh直接指定路径。

回答by thiton

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:

line 是罪魁祸首,因为您将字符串 div3 分配给 env 变量 d 并尝试执行 1。为避免这种情况,请使用反引号来分配评估结果:

 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 $(( % 3)) -eq 0`

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

仍然没有输出,但也没有错误。实际上,您的预期输出是什么?

回答by shellter

div3() {
    return $(( (  % 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.

注意, return 只是设置 $? 对于返回值的函数,只需打印它。

div3() {
    printf $(( (  % 3 ) == 0 )) "\n"
}