$‘\r‘: command not found的解决方法

简介: 在Linux系统中,运行Shell脚本,出现了如下错误:one-more.sh: line 1: $'\r': command not found1出现这样的错误,是因为Shell脚本在Windows系统编写时,每行结尾是\r\n,而在Linux系统中行每行结尾是\n,所以在Linux系统中运行脚本时,会认为\r是一个字符,导致运行错误。

缘起

在Linux系统中,运行Shell脚本,出现了如下错误:

one-more.sh: line 1: $'\r': command not found

出现这样的错误,是因为Shell脚本在Windows系统编写时,每行结尾是\r\n,而在Linux系统中行每行结尾是\n,所以在Linux系统中运行脚本时,会认为\r是一个字符,导致运行错误。

解决方法

去除Shell脚本的\r字符:

方法1

sed -i 's/\r//' one-more.sh

方法2

dos2unix one-more.sh

输出如下:

dos2unix: converting file one-more.sh to Unix format ...

如果出现如下错误:

-bash: dos2unix: command not found

说明dos2unix还没有安装,运行如下命令进行安装:

yum install -y dos2unix

输出如下:

Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                                                                                                                                                                                | 3.6 kB  00:00:00     
extras                                                                                                                                                                                                              | 2.9 kB  00:00:00     
salt                                                                                                                                                                                                                | 2.9 kB  00:00:00     
updates                                                                                                                                                                                                             | 2.9 kB  00:00:00     
Determining fastest mirrors
Resolving Dependencies
--> Running transaction check
---> Package dos2unix.x86_64 0:6.0.3-7.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================================================================================================================
 Package                                                  Arch                                                   Version                                                        Repository                                            Size
===========================================================================================================================================================================================================================================
Installing:
 dos2unix                                                 x86_64                                                 6.0.3-7.el7                                                    base                                                  74 k

Transaction Summary
===========================================================================================================================================================================================================================================
Install  1 Package

Total download size: 74 k
Installed size: 190 k
Downloading packages:
dos2unix-6.0.3-7.el7.x86_64.rpm                                                                                                                                                                                     |  74 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : dos2unix-6.0.3-7.el7.x86_64                                                                                                                                                                                             1/1 
  Verifying  : dos2unix-6.0.3-7.el7.x86_64                                                                                                                                                                                             1/1 

Installed:
  dos2unix.x86_64 0:6.0.3-7.el7                                                                                                                                                                                                            

Complete!
相关文章
|
4月前
|
Python
解决安装ConcurrentLogHandler报错error in ConcurrentLogHandler setup command: use_2to3 is invalid.
本文介绍了在Python环境下安装ConcurrentLogHandler时遇到的"use_2to3 is invalid"错误的解决方法,主要是通过降级setuptools到57.5.0版本来解决该问题。
196 2
|
3月前
|
资源调度
安装项目的时候老是报错:Command failed.
安装项目的时候老是报错:Command failed.
|
关系型数据库 MySQL Unix
mysqld_safe: command not found 解决方法
mysqld_safe: command not found 解决方法
1918 0
|
Ubuntu 关系型数据库 MySQL
提示-bash: command not found的解决方法集锦
提示-bash: command not found的解决方法集锦
|
C语言
安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
670 0
autoreconf: command not found
autoreconf: command not found
461 0
g++: command not found
g++: command not found
85 0
Idea运行报错Error running ‘Application‘: Command line is too long的解决方法
Idea运行报错Error running ‘Application‘: Command line is too long的解决方法
203 0
|
关系型数据库 PostgreSQL iOS开发
zsh: command not found: psql 解决方法
zsh: command not found: psql 解决方法
|
Shell
bash:govendor:command not found 如何解决?
bash:govendor:command not found 如何解决?
489 0