There must be a better way


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers There must be a better way
# 1  
Old 03-21-2007
Bug There must be a better way

Unix noobie here. I've just created a shell script that accomplishes these objectives:

1) Remove all DOS CRs (Carriage Returns) out of a set of actionscript files (.as)

2) Recursively remove CRs from any sub directories (how the heck can you tell the difference between directories and files).

I've written the following script which works, but it feels like a round-about way to accomplish this. What built in commands could have accomplished this task with more elegance? Ie: what's a better way to do this?

Thanks a bundle,
-Tolmark

Code:
#!/bin/sh

findActionscriptFiles()
{
	for i in `ls -F $1`
	do
		# Look for actionscripts and sub-directories
		case "$i" in
		*/) # ITS A DIRECTORY 
			len=${#i}-1 
			subPath=$1/`awk 'BEGIN { print substr("'$i'", 1, '$len' ) }'`
			# Call function (recursive)
			findActionscriptFiles $subPath
		;;
		*.as)  # ITS AN ACTIONSCRIPT FILE
			tr '\015' '\012' < $1/$i > $1/tempfile
			cp $1/tempFile $1/$i
			rm $1/tempFile
		;;
		esac

	done
}

# Main Program

# Get Path location (/path/to/folder)
echo Please enter a path to a folder:
read DIR

# Run Function
findActionscriptFiles $DIR
echo Changes Complete.

# 2  
Old 03-21-2007
Hi,
You can try this
for i in *
do
dos2unix $i $i
done

Thanks
Raghuram
# 3  
Old 03-21-2007
This is the response I get:

Code:
line 3: dos2unix: command not found

I'm on a mac, and using its built in bash shell.
# 4  
Old 03-21-2007
HI,
Check this link, in mAC there is another command called mac2unix...

Thanks
Raghuram
# 5  
Old 03-21-2007
Thank you for your replies Raghuram. From what I see, mac2unix is for converting mac line endings to unix, but it isn't avilable in the bash shell either. Is there a way to tell the differences between the bash shell and other shells?
# 6  
Old 03-21-2007
you can use tr
Code:
$ tr -d '\015' <dosfile >unixfile

or sed
Code:
sed -i 's/^M//' filename

# 7  
Old 03-21-2007
yeah, I did use tr, but is there an easier way to loop recursively through the sub-directories? (Again, thanks for the replies, I'm amazed at how fast they've come). Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question