1#!/bin/bash 2 3# Does the following operations on the files passed as arguments: 4# - Remove trailing space from lines 5# - Remove trailing blank lines 6# - Remove/convert DOS-style line breaks 7# - Expand tabs to spaces with a tabspace of 4 8 9# I once had an error where the conversion had an error (the computer 10# didn't have dos2unix), resulting in the converted files being empty. 11# The result was that every file got replaced by an empty file! That 12# was not so nice, so this script stops operation as soon as any error 13# occurs, and it also checks that only space has been changed before 14# it overwrites the original file with a space-fixed version. 15 16for f in $*; do echo $f; 17 18 tr -d '\r' < $f > __spaceTmp1; 19 if [ $? != 0 ]; then echo "There was an error removing DOS-style line breaks."; exit 1; fi; 20 21 sed 's/[[:blank:]]*$//g' < __spaceTmp1 > __spaceTmp2; 22 if [ $? != 0 ]; then echo "There was an error eliminating trailing space from lines."; exit 1; fi; 23 24 # Make completely sure that we only changed the spaces 25 diff -EbwB -q $f __spaceTmp2; 26 if [ $? != 0 ]; then echo "There was an error. Conversion not confirmed correct."; exit 1; fi; 27 28 sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' < __spaceTmp2 > __spaceTmp3; 29 if [ $? != 0 ]; then echo "There was an error eliminating trailing blank lines."; exit 1; fi; 30 31 # We have to do diff twice, because diff will not ignore trailing 32 # lines that consist only of spaces. It will ignore changes to space and removal of 33 # completely empty lines, so if we do it twice we get the right thing. 34 35 # Make completely sure that we only changed the spaces 36 diff -EbwB -q __spaceTmp2 __spaceTmp3; 37 if [ $? != 0 ]; then echo "There was an error. Conversion not confirmed correct."; exit 1; fi; 38 39 diff -q $f __spaceTmp3 1>/dev/null; 40 if [ $? != 0 ]; then 41 mv -f __spaceTmp3 $f; 42 if [ $? != 0 ]; then echo "There was an error moving fixed file into place."; exit 1; fi; 43 echo "Fixed space issue for $f." 44 fi 45 46 rm -f __spaceTmp1 __spaceTmp2 __spaceTmp3; 47 if [ $? != 0 ]; then echo "There was an error removing temporary files."; exit 1; fi; 48done; 49