Bash:其他脚本中的 运行 脚本会抛出错误,我没有单独得到它们 运行

Bash: Running scripts inside other script throws errors i don't get running them separately

这是我的问题:

我有 4 个脚本来生成不同的文件。我有一个源文件作为参数传递以生成其他输出文件。 4个脚本被认为是连续的运行。 他们有这样的代码:

while read line; do                                                             
    if echo "${line: -3}" | grep -q ','                                         
    then                                                                        
        if echo "${line:0:4}" | grep -q 'int'                                   
            then ...

运行 他们分别给我正确的输出。 从主脚本调用它们执行错误,并出现一些错误消息,例如:

./FirstScript.sh: 35: ./FirstScript.sh: Bad substitution

参考上面那行:

if echo "${line: -3}" | grep -q ','

我调用其他脚本的主要脚本是:

#!/bin/bash                                                            
sh ./FirstScript.sh 
sh ./SecondScript.sh
sh ./ThirdScript.sh 

所有脚本已设置为 $chmod 755 *.sh

您的系统上 sh 是什么?您的 #! 行看起来像您希望用 bash 解释脚本,但您的主脚本使用的是 sh ,它可能不如 bash 功能完整。

尽管您的 shebang 指向 bash,但您称它们为 sh。如果您想将它们称为 bash 只需将 sh 替换为 bash ,例如

#!/bin/bash                                                            
bash ./FirstScript.sh 
bash ./SecondScript.sh
bash ./ThirdScript.sh 

Sh 缺少 bash 的某些功能,这可能会导致错误。参见 Difference between sh and bash 了解更多相关信息。

事实上,正如我在第一句中所说的#!/bin/bash,我可以做到:

#!/bin/bash                                                            
./FirstScript.sh 
./SecondScript.sh
./ThirdScript.sh 

据说 运行 每个可执行文件 /bin/bash。