【问题标题】:How to avoid ^M character in unix shell script..?如何避免 ^M 字符在 unix shell 脚本中..?
【发布时间】:2016-04-21 06:02:51
【问题描述】:

我正在尝试使用带有以下模板的 appassembler-maven-plugin 生成 shutdown.sh 文件

#!/bin/sh
@LICENSE_HEADER@
@ENV_SETUP@

kill -1 $(cat ./samp.pid)

生成的sh文件如下。

#!/bin/sh
#   ====================================================================
#   Organization            : Sample Software JLT
#   Copyright (c) 2012
#   ====================================================================

kill -1 $(cat ./aqua.pid)

但是,当我尝试在 linux vi 编辑器中打开它时,我在行的每一端都得到 ^M 字符。如何避免它。

我正在使用下面的插件来生成 sh 文件

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <id>Aqua-Shutdown</id>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                    <configuration>
            <configurationDirectory>aqua/config</configurationDirectory>                <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
                        <showConsoleWindow>false</showConsoleWindow>
                        <platforms>
                            <platform>windows</platform>
                            <platform>unix</platform>
                        </platforms>    
                        <binFileExtensions>
                            <unix>.sh</unix>
                        </binFileExtensions>
                        <unixScriptTemplate>${basedir}/template/unixShutdownScriptTemplate</unixScriptTemplate>         <windowsScriptTemplate>${basedir}/template/windowScriptTemplate</windowsScriptTemplate>
                        <repositoryLayout>flat</repositoryLayout>
                        <useAsterikClassPath>true</useAsterikClassPath>
                        <repositoryLayout>flat</repositoryLayout>
                <assembleDirectory>target/assembleDir</assembleDirectory>
                        <repositoryName>samp/dependencies</repositoryName>
                        <programs>
                            <program>
                                    <mainClass>com.samp.aqua.bootstrap.BootstrapAgent</mainClass>
                                <name>shutdown</name>
                                <commandLineArguments>
                    <commandLineArgument>SHUTDOWN</commandLineArgument>
                                </commandLineArguments>
                            </program>
                        </programs>
                    </configuration>
                </execution>
                </executions>
            </plugin>

【问题讨论】:

  • ^M CR 0x0d 13 CARRIAGE RETURN (CR) - Microsoft 的新行是\r\nCARRIAGE RETURN NEW LINE 而Unix 只使用\n
  • 如果你在 DOS 系统下编写文件,你很可能会有这样的 ^M。您可以通过运行 dos2unix 来“清理”文件。
  • 你想实现什么样的关机脚本。您使用的是 deamon 还是 appassembler-maven-plugin 中的什么?还是使用 JSW?
  • 谢谢你的重播。我正在使用appassembler-maven-plugin和上面的配置。
  • 这不是我想要的。我知道您正在使用 appassembly-maven-plugin ....此外,您为什么使用如此古老的版本?见here for the uptodate information。也许 JSW 是您已经拥有这些东西的更好的选择...

标签: linux shell maven unix


【解决方案1】:

你问如何避免这种情况。

嗯......这不是一个真正的编程问题,但无论如何,解决方案很简单:创建新文件时,使用文本编辑器,它可以被告知使用 unix 行结尾而不是 Windows 行结尾。大多数编辑器都可以做到这一点,但少数编辑器不提供此功能。

当打开一个旧文件时,许多编辑器继续使用已经存在的行尾样式,但几乎所有编辑器都提供了转换结尾的可能性。当然,另一种方法是使用 @fedorqui 推荐的 dos2unix

由于现在的大多数 Windows 程序都可以处理带有 \n 行尾的文件,所以我一直习惯只使用这种行尾样式,除非我确定我需要为某个应用程序使用不同的行尾样式.

你没有告诉我们你的开发环境,有一点需要注意:

\r\n 行结尾的另一个来源可能是您的版本控制系统(Clearcase、Subversion、Git ......)。当从 Windows 主机签入文件时,其中大多数可以配置为“修复”行结尾,即使它们以这种方式配置并且您创建了一个带有 Unix 结尾的文本文件,它也可能会在 Windows 中默默地变成一个结尾,当您在 Windows 下签入时。如果是这种情况,你需要绕过这个“修复”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    相关资源
    最近更新 更多