LoginSignup
97

More than 5 years have passed since last update.

改行コードの確認

Last updated at Posted at 2016-02-29

Unix系のサーバ上で動作させるスクリプトをWindows端末で作成することが多い。
Windows上で作成するときに忘れがちな 改行コード

誤った 改行コード のままサーバに作成したスクリプトを配置して実行してもきちんと動作しない。
サーバにスクリプトを配置した時は下記のコマンドを実行して、 改行コード が正しいか確認した方が良い。

-

1.コマンド

下記のコマンドにて改行コードを確認できる。

$ od -c [ファイル名]

それぞれの改行コードは下記のように表示される。

OS 改行コード 「od -c」での見え方
Unix LF \n
Mac(OSX) LF \n
Mac(OS9) CR \r
Windows CR+LF \r\n

-

2.実行例

テスト用に下記の3ファイルを準備。

tests-MBA:test $ ll
total 48
-rw-r--r--@ 1 test  staff  20  2 29 20:05 test001   ★Unix版
-rw-r--r--@ 1 test  staff  20  2 29 20:05 test002   ★OSX版
-rw-r--r--@ 1 test  staff  24  2 29 20:05 test003   ★Windows版
tests-MBA:test test$ 

実行結果は下記の通り。

tests-MBA:test test$ od -c test001
0000000    t   e   s   t  \n   t   e   s   t  \n   t   e   s   t  \n   t
0000020    e   s   t  \n                                                
0000024
tests-MBA:test test$ 
tests-MBA:test test$ 
tests-MBA:test test$ od -c test002
0000000    t   e   s   t  \r   t   e   s   t  \r   t   e   s   t  \r   t
0000020    e   s   t  \r                                                
0000024
tests-MBA:test test$ 
tests-MBA:test test$ 
tests-MBA:test test$ od -c test003
0000000    t   e   s   t  \r  \n   t   e   s   t  \r  \n   t   e   s   t
0000020   \r  \n   t   e   s   t  \r  \n                                
0000030
tests-MBA:test test$ 

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
97