函数不在bash脚本中工作 - 错误消息与'\r'有关 [英] Function not working in a bash script - error messages relate to '\r'

查看:153
本文介绍了函数不在bash脚本中工作 - 错误消息与'\r'有关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近安装了新版本的Debian。我创建了这个简单的脚本:

 #!/ bin / bash 

print_something(){
echo你好,我是一个函数
}

print_something
print_something

但是,当我发出 bash test.sh 时显示此错误:

  test.sh:第3行:$'\r':未找到命令
test.sh:第4行:语法错误附近出现意外标记`$'{\ r''
'est.sh:第4行:`print_something(){

我做错了什么?
非常感谢!

解决方案

诊断:


  • Unix实用程序通常希望行结束符只是 \\\
    ,并且通常会打破或显示意外的行为并用Windows样式 \r\\\
    行结尾的文件 - 作为 bash 在这里执行。

  • 包含字符串 \ r 的错误消息表明存在这种Windows风格的行结尾。 li>
  • 将文件传递给 cat -v 并检查 ^ M 的输出输出行的结尾(这是 \r 字符的表示方式)是根据需要检查文件的Windows样式行尾的一种方法。



  • 修复:


    • 要将具有Windows风格的行结尾的文件转换为Unix风格的文件:


      • 使用 dos2unix 实用程序,如果已安装(通常是 )或安装它一个选项;你如何安装它取决于你的平台;例如:Ubuntu上的


        • sudo apt-get install dos2unix

        • 在OSX上,安装了 Homebrew brew install dos2unix 或者,使用标准实用程序执行转换;或者,使用 标准有几种选择;例如:


          • sed $'s / \ $ $'win.txt> unix.txt

          • awk'sub(\r $,)+1'win.txt> unix.txt

          • 上述命令的变体可以更新文件 ,但它们是平台
          • 用于创建用于Unix实用程序的shell脚本/文件的编辑器被配置为使用Unix风格的行结尾。

          I have recently installed a fresh version of Debian. I created this simple script:

          #!/bin/bash
          
          print_something () {
            echo Hello I am a function
          }
          
          print_something
          print_something
          

          However this displays this error upon me issuing bash test.sh:

          test.sh: line 3: $'\r': command not found
          test.sh: line 4: syntax error near unexpected token `$'{\r''
          'est.sh: line 4: `print_something () {
          

          What am I doing wrong? Many thanks!

          解决方案

          Diagnosing:

          • Unix utilities in general expect line endings to be \n only, and usually break or show unexpected behavior and output with files that have Windows-style \r\n line endings - as bash does here.
          • Error messages containing the string \r are an indicator that such Windows-style line endings are present.
          • Passing a file to cat -v and examining the output for ^M at the end of output lines (which is how \r chars. are represented) is a way to check a file for Windows-style line endings on demand.

          Fixing:

          • To convert a file with Windows-style line endings to Unix-style ones:
            • use the dos2unix utility, if already installed (typically, it is not), or installing it is an option; how you install it depends on your platform; e.g.:
              • on Ubuntu: sudo apt-get install dos2unix
              • on OSX, with Homebrew installed, brew install dos2unix
            • alternatively, use standard utilities to perform the conversion; there are several options; e.g.:
              • sed $'s/\r$//' win.txt > unix.txt
              • awk 'sub("\r$", "")+1' win.txt > unix.txt
              • There are variants of the above commands that update a file in place, but they're platform-specific, if available at all.
          • Make sure that the editor you use to create shell scripts / files for use with Unix utilities is configured to use Unix-style line endings.

          这篇关于函数不在bash脚本中工作 - 错误消息与'\r'有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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