在bash中没有正确调用函数(function is not called correctly in bash)

我有这个bash代码,检查操作系统是Linux还是Mac,我使用函数isWhat从其他函数调用。

function isWhat { if [ `uname` == $1 ]; then return 1 else return 0 fi } function isLinux { return isWhat("Linux") } function isMac { return isWhat("Darwin") }

但是,我遇到了这些错误:

/functions.sh: line 13: syntax error near unexpected token `(' /functions.sh: line 13: ` return isWhat("Linux")' runme.sh: line 7: isMac: command not found

可能有什么问题?

I have this bash code that checks if the OS is Linux or Mac, and I use the function isWhat to be invoked from other functions.

function isWhat { if [ `uname` == $1 ]; then return 1 else return 0 fi } function isLinux { return isWhat("Linux") } function isMac { return isWhat("Darwin") }

However, I got these errors:

/functions.sh: line 13: syntax error near unexpected token `(' /functions.sh: line 13: ` return isWhat("Linux")' runme.sh: line 7: isMac: command not found

What might be wrong?

最满意答案

这不是你在bash调用函数的方式。 它们就像其他shell命令一样工作,即:

function isLinux { isWhat "Linux" }

此外, return是冗余的,函数将返回上次命令运行的退出状态。 如果你想要明确,你可以这样写:

function isLinux { isWhat "Linux" return $? }

This is the working code after fixing some bugs.

function isWhat { if [ "`uname`" = $1 ]; then echo 1 else echo 0 fi } function isLinux { isWhat "Linux" } function isMac { isWhat "Darwin" }

The usage is (assuming the previous functions are in abc.sh)

source abc.sh echo $(isMac) if [ $(isMac) == 1 ]; then echo "A" fi

I'm not sure exactly why, but I had to add function to make the code work anyway, I use Mac OS X 10.11.1.

在bash中没有正确调用函数(function is not called correctly in bash)

我有这个bash代码,检查操作系统是Linux还是Mac,我使用函数isWhat从其他函数调用。

function isWhat { if [ `uname` == $1 ]; then return 1 else return 0 fi } function isLinux { return isWhat("Linux") } function isMac { return isWhat("Darwin") }

但是,我遇到了这些错误:

/functions.sh: line 13: syntax error near unexpected token `(' /functions.sh: line 13: ` return isWhat("Linux")' runme.sh: line 7: isMac: command not found

可能有什么问题?

I have this bash code that checks if the OS is Linux or Mac, and I use the function isWhat to be invoked from other functions.

function isWhat { if [ `uname` == $1 ]; then return 1 else return 0 fi } function isLinux { return isWhat("Linux") } function isMac { return isWhat("Darwin") }

However, I got these errors:

/functions.sh: line 13: syntax error near unexpected token `(' /functions.sh: line 13: ` return isWhat("Linux")' runme.sh: line 7: isMac: command not found

What might be wrong?

最满意答案

这不是你在bash调用函数的方式。 它们就像其他shell命令一样工作,即:

function isLinux { isWhat "Linux" }

此外, return是冗余的,函数将返回上次命令运行的退出状态。 如果你想要明确,你可以这样写:

function isLinux { isWhat "Linux" return $? }

This is the working code after fixing some bugs.

function isWhat { if [ "`uname`" = $1 ]; then echo 1 else echo 0 fi } function isLinux { isWhat "Linux" } function isMac { isWhat "Darwin" }

The usage is (assuming the previous functions are in abc.sh)

source abc.sh echo $(isMac) if [ $(isMac) == 1 ]; then echo "A" fi

I'm not sure exactly why, but I had to add function to make the code work anyway, I use Mac OS X 10.11.1.