从外部脚本调用函数

Use*_*113 2 bash

我有2个脚本:

/home/bin/test.sh

#!/bin/bash
. /home/bin/test_functions.sh
test
Run Code Online (Sandbox Code Playgroud)

/home/bin/test_functions.sh

#!/bin/sh
test()
{
    echo "this is a test"
}
Run Code Online (Sandbox Code Playgroud)

我想从外部脚本调用该函数并在主脚本中执行它.然而,我一直在收到这些错误:

'home/bin/test_functions.sh: line 2: syntax error near unexpected token `
'home/bin/test_functions.sh: line 2: `test()
Run Code Online (Sandbox Code Playgroud)

我正在做什么可能有什么问题?

Joh*_*ica 5

它似乎test_functions.sh是DOS格式,而bash \r\n在行结尾处窒息.使用dos2unix将其转换为UNIX行尾.

你可以告诉,因为bash试图输出的是:

/home/bin/test_functions.sh: line 2: syntax error near unexpected token `\r'
/home/bin/test_functions.sh: line 2: `test()\r'
Run Code Online (Sandbox Code Playgroud)

但是回车\r导致单引号在错误消息的开头结束,覆盖了前导/.