finding files in dos format + .sql files without an empty last line

Warren Head warrenhead at gmail.com
Fri Feb 23 00:11:40 PST 2007


2007/2/22, Ag. Hatzimanikas <a.hatzim at gmail.com>:
>
> What I want to see from Warren and in a way that also would be beneficial
> for
> the list,is to translate the human logic to actions and code.


What I got:

pseudo code:
- recursively find files from current directory
    for each file found:
       - test whether it ends with dos line terminators, eg. CRLF
           if so:
               - change the line terminators into linux line terminators
               - overwrite the file
               - echo the filename so it is visible that something happened
      - test whether it ends with an .sql extension
           if so:
              - test whether the last character in the file is not a $
(linefeed)
                    if so:
                         - insert  the $
                         - overwrite the file
                         - echo the filename so it is visible that something
happened



> Then we can translate the above paragraph to a snippet of code and do the
> job.
>
>
I took a shot at it, but I am missing the linefeed check and insert linefeed
stuff.

#!/bin/bash
for filename in $(find . -type f);do
       if [[ -n $(file $filename |grep CRLF) ]]
       then
           sed 's/^M$//' $filename > $filename.unix
           echo File converted from dos to unix: $filename
       fi
       if [[ -n $(echo $filename | grep .sql) ]]
       then
           if [[ -n (cat -E $filename | findthelastcharacterisnot$) ]]
           then
               insert a line feed
               overwrite the file
               echo File had a LF inserted: $filename
           fi
           # echo File ends with .sql: $filename
       fi
done
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxfromscratch.org/pipermail/blfs-support/attachments/20070223/86c01b66/attachment.html>


More information about the blfs-support mailing list