dos2unixコマンドで改行コード変換

概要

Linuxサーバーにスクリプトをアップロードしたところ改行コードが違っておりスクリプトが正常に動作しなかった。
dos2unixコマンドを使って改行コードを変換した時のメモ

改行コード確認

cat -e test.pl

で確認可能

改行コード
行末 改行コード OS
^M+$ CR+LF DOS / Windows
$ LF Unix系(MacOSX含)


表示例

[tsunokawa@test tmp]$ cat -e test.pl
#!/usr/bin/env perl^M$
^M$
use strict;^M$
use warnings;^M$
use feature 'say';^M$
^M$
say "hoge";^M$
^M$
[tsunokawa@test tmp]$ 

CR+LFになっている。

インストール

改行コード変換のためdos2unixコマンドを使う。

yum install dos2unix.x86_64


改行コード変換

CR+LF(^M+$) → LF($)

dos2unix test.pl


表示例

[tsunokawa@test tmp]$ dos2unix test.pl
dos2unix: converting file test.pl to Unix format ...
[tsunokawa@test tmp]$


確認

[tsunokawa@test tmp]$ cat -e test.pl
#!/usr/bin/env perl$
$
use strict;$
use warnings;$
use feature 'say';$
$
say "hoge";$
$
[tsunokawa@test tmp]$

LFになった。