Bash脚本会打印“找不到命令".空行 [英] Bash script prints "Command Not Found" on empty lines

查看:93
本文介绍了Bash脚本会打印“找不到命令".空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我在Debian中从命令行使用bash scriptname.sh运行脚本时,都会得到Command Not found,然后得到脚本的结果.

Every time I run a script using bash scriptname.sh from the command line in Debian, I get Command Not found and then the result of the script.

该脚本可以工作,但是屏幕上始终为每个空行打印一个Command Not Found语句.每个空白行都会导致找不到命令.

The script works but there is always a Command Not Found statement printed on screen for each empty line. Each blank line is resulting in a command not found.

我正在从/var文件夹运行脚本.

I am running the script from the /var folder.

这是脚本:

#!/bin/bash

echo Hello World

我通过键入以下命令来运行它:

I run it by typing the following:

bash testscript.sh

为什么会这样?

推荐答案

确保您的第一行是:

#!/bin/bash

如果不是b2,请输入bash的路径.

Enter your path to bash if it is not /bin/bash

尝试运行:

dos2unix script.sh

将行尾等从Windows转换为Unix格式.即从行尾去除\ r(CR),以将它们从\r\n (CR+LF)更改为\n (LF).

That wil convert line endings, etc from Windows to unix format. i.e. it strips \r (CR) from line endings to change them from \r\n (CR+LF) to \n (LF).

有关dos2unix命令(手册页)的更多详细信息

More details about the dos2unix command (man page)

另一种判断文件是否为dos/Win格式的方法:

Another way to tell if your file is in dos/Win format:

cat scriptname.sh | sed 's/\r/<CR>/'

输出将如下所示:

#!/bin/sh<CR>
<CR>
echo Hello World<CR>
<CR>

这将输出整个文件文本,并为文件中的每个\r字符显示<CR>.

This will output the entire file text with <CR> displayed for each \r character in the file.

这篇关于Bash脚本会打印“找不到命令".空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆