Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 用于循环的Bash脚本运行正常,但在使用源代码运行时出现语法错误_Linux_Bash_For Loop - Fatal编程技术网

Linux 用于循环的Bash脚本运行正常,但在使用源代码运行时出现语法错误

Linux 用于循环的Bash脚本运行正常,但在使用源代码运行时出现语法错误,linux,bash,for-loop,Linux,Bash,For Loop,我有以下bash脚本 #!/bin/bash for i in 4 5 6; do echo $i done 当我运行脚本时,它工作正常: $ ./testbash 4 5 6 当我源代码相同的脚本时,我得到以下语法错误 $ source testbash bash: /home/nick/bin/testbash: line 5: syntax error: unexpected end of file 寻源bash文件时是否不允许使用某些命令?我正在使用GNUBash,版

我有以下bash脚本

#!/bin/bash
for i in 4 5 6; do
  echo $i
done    
当我运行脚本时,它工作正常:

$ ./testbash 
4
5
6
当我源代码相同的脚本时,我得到以下语法错误

$ source testbash
bash: /home/nick/bin/testbash: line 5: syntax error: unexpected end of file
寻源bash文件时是否不允许使用某些命令?我正在使用GNUBash,版本4.1.2

下面是脚本的hextump

$hextump-C testbash

00000000  23 21 2f 62 69 6e 2f 62  61 73 68 0a 66 6f 72 20  |#!/bin/bash.for |
00000010  69 20 69 6e 20 34 20 35  20 36 3b 20 64 6f 0a 20  |i in 4 5 6; do. |
00000020  20 65 63 68 6f 20 24 69  0a 64 6f 6e 65 0a        | echo $i.done.|
0000002e

寻源和执行会产生不同结果的原因有很多,但获得新bash语法错误的原因要少得多

运行
env-ibash--norc以获得一个干净的shell,这样就可以检查它是否与当前shell相关

如果在这个干净的shell中寻源工作正常,请将以下命令的输出与失败的shell进行比较:

  • 别名
    (以防替换或引入像
    完成
    这样的关键字)
  • shopt
    (如果您修改语法更改选项,如
    extglob
  • echo$BASH\u版本
    (以防交互式rc文件最终执行不同的shell)

  • 那个文件有什么样的行尾?(如果运行
    echo>/home/nick/bin/testbash
    ,然后再次尝试寻源,它会起作用吗?请尝试
    source./testbash
    以避免它在您的路径中搜索旧的/过时的versions@Etan赖斯纳我试过dos2linux以防万一。没有变化。文件结果:testbash:Bourne脚本文本executable@that
    source./testbash
    产生了相同的语法error@Dijkstra运行
    env-ibash--norc
    并在该shell中再次尝试
    source./testfile
    。我猜你有个别名叫
    done