[Previous entry: "From French to Freedom"] [Main Index] [Next entry: "Spiking Babies"]

03/13/2003 Entry: "Unix Utility - Banish ^M forever!"

Hate ^M in your unix files, but it's a pain in the ass to reconfigure SourceSafe or your regular non-emacs editor? Here's a little script to clean those things out of files en masse. (Hey! That's French! I'll let it slide just this once.) Any constructive suggestions for improvement or enhancement are appreciated.


#!/bin/bash
# Program: mstrip
# Author: Spastic Mutant mutant@spasticmutant.com
#
#
# This program removes the ^M character from files.
# If necessary it makes the file writable, strips
# out the ^M's from the file, then returns it to
# read-only permissions. ^M is ascii 0xA, which is
# represented as '\r' below. It represents the Dos
# LineFeed which Unix editors and certain compilers
# hate more than anything in the universe.
#
#

changed='no'

for i in $* ;
do
echo $i
# is it writable? If so, then leave it writable.
if [ -w $i ] ; then
cp $i temp
tr -d '\r' < $i > temp
changed='no'
else
#it's read only. Make it writable while we alter it.
chmod +w $i
cp $i temp
tr -d '\r' < $i > temp
changed='yes'
fi

cp temp $i
if [[ $changed = "yes" ]]; then

# if I changed the permissions to +w, change it back
# using -w.

chmod -w $i
fi

#delete my temp file. I don't need it anymore.
rm temp

done






Symbol for
Universal Recyclables DISCLAIMER: This web page was constructed with 100% recycled electrons (or, during shortages, at least 35% post-consumer electrons). No electrons were harmed in the making of this web page, although several were accelerated against their collective will. No files have been charged. And Remember: the only Good Cow is a Dead Cow.

© 2000, 2001, 2002 Spastic Mutant Productions. All Rights Reserved.