【shell 】bash file returns unexpected token `$‘do\r‘‘

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.

猜你喜欢

转载自blog.csdn.net/hiliang521/article/details/130108677