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

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

问题描述

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

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

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

这是脚本:

#!/bin/bash回声你好世界

我通过键入以下内容来运行它:

bash testscript.sh

为什么会发生这种情况?

解决方案

确保你的第一行是:

#!/bin/bash

如果不是/bin/bash,请输入你的bash路径

<小时>

尝试运行:

dos2unix script.sh

这会将行尾等从 Windows 转换为 unix 格式.即它从行尾去除 (CR) 以将它们从 (CR+LF) 更改为 (LF).

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

<小时>

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

cat scriptname.sh |sed 's/
//'

输出看起来像这样:

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

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

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.

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.

I am running the script from the /var folder.

Here is the script:

#!/bin/bash

echo Hello World

I run it by typing the following:

bash testscript.sh

Why would this occur?

解决方案

Make sure your first line is:

#!/bin/bash

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


Try running:

dos2unix script.sh

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

More details about the dos2unix command (man page)


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

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

The output will look something like this:

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

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

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

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