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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值