bash file returns unexpected token `$'do\r'' - Stack Overflow
问题描述:
问题shell
#!/usr/bin/env bash
echo "The ports used are as follows:"
ports=( 8030 9020 9030 9010 9060 8040 9050 8060 8000 )
for num in ${ports[@]}
do
netstat -tunlp |grep $num
done
运行报错:
[gaoliang@di11v ~]$ bash sc_check.sh
sc_check.sh: line 2: $'\r': command not found
The ports used are as follows:
sc_check.sh: line 4: $'\r': command not found
sc_check.sh: line 7: syntax error near unexpected token `$'do\r''
'c_check.sh: line 7: `do
问题解决
问题根本原因描述
This commonly is caused when the executable file is created in an operative system (windows) and used in another operative system (Linux). Also rare text editors could be the problem.
问题解决
using unix text editor vi:
open file in vi edit with vi filename.sh command;
type in vi :set ff=unix command;
save file with :wq
It will save the file with unix line endings.
博客主要讲述了bash文件在Linux系统运行时出现意外令牌错误的问题。根本原因是可执行文件在Windows系统创建,却在Linux系统使用,也可能是文本编辑器的问题。解决办法是使用Unix文本编辑器vi,按特定命令编辑并保存文件,使其具有Unix行尾格式。
737

被折叠的 条评论
为什么被折叠?



