linux - shellscript 中行之间的空格被视为命令

标签 linux bash sh

<分区>

我正在编写一个 bash 脚本,如下所示

#!/bin/bash

#getting the environment variable from commandline
environment=$1

echo $environment

现在,当我使用 bash ./bashScript.sh Hello 运行脚本时,出现以下错误

: command not found line 2

: command not found line 5

我看到这两行都是空格,因此 bash 脚本给我一个错误

为了解决这个问题,我将脚本写成

#!/bin/bash
#
#getting the environment variable from commandline
environment=$1
#
echo $environment 

但看起来有点乱

有没有其他方法可以做到这一点。提前感谢您的帮助。

最佳答案

当您使用 od -xc 文件 转储时,您的评论增加了您的行使用 DOS 行尾 (\r\n) 的精度。为避免这种情况,您应该确保您的编辑器使用 Unix 行尾 (\n)。

要在现有的文本文件上修复它,您可以使用 tr:

tr -d '\r' < dos_file > unix_file

关于linux - shellscript 中行之间的空格被视为命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41803777/

相关文章:

linux - 使用grep函数后如何删除文件

linux - 将 HH :MM:SS (hours:minutes:seconds. split seconds) 转换为秒的简单方法

bash - Bash 或 Shell 脚本中的转发函数声明?

linux - 有条件地终止延迟脚本

linux - PGP/GPG无法解密

c - linux 内核模块 : kernel method undefined (kthread_create_on_cpu)

linux - 从命令行创建文件

linux - 在 bash 中比较整数和 float

bash - 从 bash 捕获 telnet 超时

linux - 获取 shell 脚本中的函数名称列表