<p>Over at <a href="https://stackoverflow.com/questions/38664/can-you-modify-text-files-when-committing-to-subversion">Can you modify text files when committing to subversion?</a> <a href="https://stackoverflow.com/questions/38664/can-you-modify-text-files-when-committing-to-subversion#38666">Grant</a> suggested that I block commits instead.</p>

<p>However I don't know how to check a file ends with a newline. How can you detect that the file ends with a newline?</p>

[{'answer_id': 39162, 'author': 'bstark', 'author_id': 4056, 'author_profile': 'https://Stackoverflow.com/users/4056', 'pm_score': 2, 'selected': False, 'text': '<p>You could use something like this as your pre-commit script:</p>\n\n<pre>\n#! /usr/bin/perl\n\nwhile (&lt;&gt;) {\n    $last = $_;\n}\n\nif (! ($last =~ m/\\n$/)) {\n    print STDERR "File doesn\'t end with \\\\n!\\n";\n    exit 1;\n}\n</pre>\n'}, {'answer_id': 39185, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': '<p>Using only <code>bash</code>:</p>\n\n<pre><code>x=`tail -n 1 your_textfile`\nif [ "$x" == "" ]; then echo "empty line"; fi\n</code></pre>\n\n<p>(Take care to copy the whitespaces correctly!)</p>\n\n<p>@grom:</p>\n\n<blockquote>\n  <p>tail does not return an empty line</p>\n</blockquote>\n\n<p>Damn. My test file didn\'t end on <code>\\n</code> but on <code>\\n\\n</code>. Apparently <code>vim</code> can\'t create files that don\'t end on <code>\\n</code> (?). Anyway, as long as the “get last byte” option works, all\'s well.</p>\n'}, {'answer_id': 40919, 'author': 'grom', 'author_id': 486, 'author_profile': 'https://Stackoverflow.com/users/486', 'pm_score': 6, 'selected': True, 'text': '<p><strong><a href="https://stackoverflow.com/questions/38746/how-to-detect-file-ends-in-newline#39185">@Konrad</a></strong>: tail does not return an empty line. I made a file that has some text that doesn\'t end in newline and a file that does. Here is the output from tail:</p>\n<pre class="lang-none prettyprint-override"><code>$ cat test_no_newline.txt\nthis file doesn\'t end in newline$ \n\n$ cat test_with_newline.txt\nthis file ends in newline\n$\n</code></pre>\n<p>Though I found that tail has get last byte option. So I modified your script to:</p>\n<pre><code>#!/bin/sh\nc=`tail -c 1 $1`\nif [ &quot;$c&quot; != &quot;&quot; ]; then\n    echo &quot;no newline&quot;\nfi\n</code></pre>\n'}, {'answer_id': 2421790, 'author': 'FelipeC', 'author_id': 10474, 'author_profile': 'https://Stackoverflow.com/users/10474', 'pm_score': 4, 'selected': False, 'text': '<p>Or even simpler:</p>\n\n<pre><code>#!/bin/sh\ntest "$(tail -c 1 "$1")" &amp;&amp; echo "no newline at eof: \'$1\'"\n</code></pre>\n\n<p>But if you want a more robust check:</p>\n\n<pre><code>test "$(tail -c 1 "$1" | wc -l)" -eq 0 &amp;&amp; echo "no newline at eof: \'$1\'"\n</code></pre>\n'}, {'answer_id': 10415403, 'author': 'KarolDepka', 'author_id': 170451, 'author_profile': 'https://Stackoverflow.com/users/170451', 'pm_score': 2, 'selected': False, 'text': '<p>Worked for me: <br/></p>\n\n<pre><code>tail -n 1 /path/to/newline_at_end.txt | wc --lines\n# according to "man wc" : --lines - print the newline counts\n</code></pre>\n\n<p>So wc counts number of newline chars, which is good in our case.\nThe oneliner prints either 0 or 1 according to presence of newline at the end of the file.</p>\n'}, {'answer_id': 25749716, 'author': 'Steve Kehlet', 'author_id': 296829, 'author_profile': 'https://Stackoverflow.com/users/296829', 'pm_score': 4, 'selected': False, 'text': '<p>Here is a useful bash function:</p>\n\n<pre><code>function file_ends_with_newline() {\n    [[ $(tail -c1 "$1" | wc -l) -gt 0 ]]\n}\n</code></pre>\n\n<p>You can use it like:</p>\n\n<pre><code>if ! file_ends_with_newline myfile.txt\nthen\n    echo "" &gt;&gt; myfile.txt\nfi\n# continue with other stuff that assumes myfile.txt ends with a newline\n</code></pre>\n'}, {'answer_id': 60911655, 'author': 'Koichi Nakashima', 'author_id': 11267590, 'author_profile': 'https://Stackoverflow.com/users/11267590', 'pm_score': 0, 'selected': False, 'text': '<p>The <code>read</code> command can not read a line without newline.</p>\n\n<pre class="lang-sh prettyprint-override"><code>if tail -c 1 "$1" | read -r line; then\n  echo "newline"\nfi\n</code></pre>\n\n<p>Another answer.</p>\n\n<pre class="lang-sh prettyprint-override"><code>if [ $(tail -c 1 "$1" | od -An -b) = 012 ]; then\n  echo "newline"\nfi\n</code></pre>\n'}, {'answer_id': 61667168, 'author': 'Nicolae Iotu', 'author_id': 10828773, 'author_profile': 'https://Stackoverflow.com/users/10828773', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m coming up with a correction to my own answer.</p>\n\n<p>Below should work in all cases with no failures:</p>\n\n<pre><code>nl=$(printf \'\\012\')\nnls=$(wc -l "${target_file}")\nlastlinecount=${nls%% *}\nlastlinecount=$((lastlinecount+1))\nlastline=$(sed ${lastlinecount}\' !d\' "${target_file}")\nif [ "${lastline}" = "${nl}" ]; then\n    echo "${target_file} ends with a new line!"\nelse\n    echo "${target_file} does NOT end with a new line!"\nfi\n</code></pre>\n'}, {'answer_id': 61876217, 'author': 'thanos.a', 'author_id': 2110865, 'author_profile': 'https://Stackoverflow.com/users/2110865', 'pm_score': 0, 'selected': False, 'text': '<p>You can get the last character of the file using <code>tail -c 1</code>.</p>\n\n<pre><code>   my_file="/path/to/my/file"\n\n   if [[ $(tail -c 1 "$my_file") != "" ]]; then\n      echo "File doesn\'t end with a new line: $my_file"\n   fi\n</code></pre>\n'}, {'answer_id': 63398061, 'author': 'Olivier Pirson', 'author_id': 1333666, 'author_profile': 'https://Stackoverflow.com/users/1333666', 'pm_score': 2, 'selected': False, 'text': '<p>A complete Bash solution with only <code>tail</code> command, that also deal correctly with empty files.</p>\n<pre class="lang-sh prettyprint-override"><code>#!/bin/bash\n# Return 0 if file $1 exists and ending by end of line character,\n# else return 1\n[[ -s &quot;$1&quot; &amp;&amp; -z &quot;$(tail -c 1 &quot;$1&quot;)&quot; ]]\n</code></pre>\n<ul>\n<li><code>-s &quot;$1&quot;</code> checks if the file is not empty</li>\n<li><code>-z &quot;$(tail -c 1 &quot;$1&quot;)&quot;</code> checks if its last (existing) character is end of line character</li>\n<li>the all <code>[[...]]</code> conditional expression is returned</li>\n</ul>\n<p>You can also defined this Bash function to use it in your scripts.</p>\n<pre class="lang-sh prettyprint-override"><code># Return 0 if file $1 exists and ending by end of line character,\n# else return 1\ncheck_ending_eol() {\n    [[ -s &quot;$1&quot; &amp;&amp; -z &quot;$(tail -c 1 &quot;$1&quot;)&quot; ]]\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/38746', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/486/']