---
title: "合并源代码到一个文件的脚本"
author: "codingstandards"
date: "2012-07-16T15:32:43Z"
tags: []
categories: ["操作系统"]
slug: "untitled"
draft: false
lastmod: "2012-07-16T15:32:43Z"
id: 1595321
url: "https://codingstandards.iteye.com/blog/1595321"
---

最近公司申请知识产权，需要将提交前后各38页源代码，因此写了一个合并源代码的Bash脚本，如下所示：

 

```
#!/bin/sh

find . -name "*.java" -o -name "*.jsp" | while read file
do
if ! grep -q UTF $file; then
echo "Source File: $file"
cat -n $file
echo
fi
done
```

 

上面脚本中，源代码类型为 java 或 jsp

```
find . -name "*.java" -o -name "*.jsp"
```

不打印包含 UTF 的文件，是因为合并之后是乱码

```
if ! grep -q UTF $file; then
```

打印行号

```
cat -n $file
```