Shell脚本:Windows下可用源码转换为Linux下可用源码


简介

Windows下编写的代码(如C\C++\Java等)放到Linux下不能直接编译, 因为主要存在两个问题:

1. Windows和Linux的行尾符不同, Windows下行尾符是"\n\r", 而Linux的行尾符是"\n"

2. Windows下编码通常是GB2312, 而Linux中的编码通常是UTF-8。

所以编写了这个脚本文件用于将Windows下编写的源码转化为Linux下可用的源码, 该脚本文件可以将 指定目录及其子目录 下指定 后缀名 的源文件进行转换。

使用方式是:

bash trans.sh 指定文件夹 要转换文件的拓展名

将当前文件夹及其子文件下所有java源文件进行转换

bash trans.sh . java

代码

#!/bin/bash
#Program:
#	tranlate the source code written in windows to source code usable in linux.
#Author:
#	Chen Zhongzheng
#History:
#	2014年09月03日20:17:36  v1.0

function recursion()
{
	cd $1
	for i in $(ls)
	do
		if [ -d "$i" ]; then
			recursion $i $2
		elif [ "${i##*.}" = "${2}" ]; then
			iconv -f gb2312 -t utf-8 $i > temp_111
			mv temp_111 $i
			dos2unix $i
		fi
	done
	cd ..
}

if [ ! $# -eq 3 ]; then
	echo "usage: bash tras.sh directory_name extension_name"
elif [ -d $1 ]; then
	recursion $1 $2
else
	echo "usage: bash tras.sh directory_name extension_name"
fi 

参考:

http://www.wenzizone.cn/?p=313

http://bbs.chinaunix.net/thread-624345-1-1.html

http://blog.csdn.net/rainharder/article/details/6030255

相关内容