Ubuntu Bash 无法理解 Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8648094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:13:21  来源:igfitidea点击:

Bash script not understood by Ubuntu Bash

bashubuntu

提问by Houman

I am new to Bash Script. I run the following simple script in Ubuntu like this:

我是 Bash 脚本的新手。我在 Ubuntu 中运行以下简单脚本,如下所示:

sudo bash -x Script.sh

Output:

输出:

+ $'\r'
: command not found
+ $'\r'
: command not found
+ $'\r'
: command not found
+ adminEmail=$'[email protected]\r'
+ $'\r'
: command not found
' echo 'database name:
database name:
+ read $'dbname\r'

The actual script:

实际脚本:

#!/bin/bash

# Installation script for latest Wordpress website on Ubuntu
# 
# Kave
# December 27, 2011


adminEmail="[email protected]"

echo "database name:"
read dbname

What are all these '\r'error messages coming up? Even the comments seem not to be understood...

出现的所有这些'\r'错误消息是什么?连评论都看不懂。。。

采纳答案by Jonathan Leffler

You downloaded the file via a Windows machine, and you have CRLF line endings (that's \r\nin C and related languages).

您通过 Windows 机器下载了该文件,并且您有 CRLF 行结尾(使用\r\nC 和相关语言)。

Remove the DOS line endings. For example, edit the file in vimand change the format with :set fileformat=unix(plus Return) and then write the file back out. Alternative techniques could use the trcommand, or dos2unixor dtou, depending on what's available.

删除 DOS 行尾。例如,编辑文件vim并使用:set fileformat=unix(plus Return)更改格式,然后将文件写回。替代技术可以使用tr命令或dos2unixdtou,这取决于可用的内容。

回答by jaypal singh

Simply do

简单地做

dos2unix scriptname

and execute your script.

并执行您的脚本。

If you don't have the tool you can do the following to install it.

如果您没有该工具,您可以执行以下操作来安装它。

sudo apt-get install dos2unix

OR

或者

Do the following command line hacks to convert the file.

执行以下命令行技巧来转换文件。

tr -d '5' < scriptname > scriptname.new

OR

或者

sed -i 's/\x0D$//' scriptname

回答by Nanne

Cut and paste your code from this page into an editor on your Ubuntu, so you'll get your line-endings fixed (\r\non windows vs \non Linux for instance)

将此页面中的代码剪切并粘贴到 Ubuntu 上的编辑器中,这样您就可以修复行尾(例如\r\n在 Windows 上与\nLinux 上)

If you save it as a new file I'm sure it'll work fine.

如果您将其另存为新文件,我相信它会正常工作。

回答by eclipse

Sounds like there are some extraneous non-printable characters in the file.
Open the file in Vi/Vim:
press [ESC]to get into command mode
then type :set list

听起来文件中有一些无关的不可打印字符。
在 Vi/Vim 中打开文件:
[ESC]进入命令模式
然后键入:set list