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

简介

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

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

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

所以编写了这个脚本文件用于将Windows下编写的源码转化为Linux下可用的源码, 

该脚本文件可以将 指定目录及其子目录 下指定 后缀名 的源文件进行转换。

使用方式

sudo apt-get install dos2unix
bash trans.sh 指定文件夹 要转换文件的拓展名

例如,

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

bash trans.sh /home java

代码

#!/bin/bash
#Program:
#   convert the text written in windows to the text usable in linux.
#Author:
#   Chen Zhongzheng
#History:
#   2014年09月03日20:17:36  v1.0
#TODO:
#   add a parameter to specify the origin encoding, eg. gb2312\cp936\gbk...

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

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










#!/bin/bash
#Program:
#   convert the text written in windows to the text usable in linux.
#Author:
#   Chen Zhongzheng
#History:
#   2015年12月04日21:33:29  v1.1
#TODO:
#   add a parameter to specify the origin encoding, eg. gb2312\cp936\gbk...

function recursion()
{
  cd $1
  for i in $(ls)
  do
    if [ -d "$i" ]; then
      recursion ``i ``2
    elif [ "``{i##*.}" = "``{2}" ]; then
      enca -L zh_CN -x UTF-8 $i
      dos2unix $i
    fi
  done
  cd ..
}

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

参考:

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

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

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

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/38980201

/** * RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. * LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/ /* var disqus_config = function () { this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable }; */ (function() { // DON'T EDIT BELOW THIS LINE var d = document, s = d.createElement('script'); s.src = 'https://chenzz.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })();