给定一个代码包,将其中未加上apache lisence2(al2)头的文件,全加上 al2头。
1> 文件中已经有al2头的文件,直接退出。
查找文件内容中有行内容包括“licenses/LICENSE-2.0”“Apache Software Foundation”;行开头内容包括“^#Licensedto the Apache Software”的文件。
2>文件中没有al2头的文件。其文件的开头如果有几个空行,则将文件空行删除。使得文件的第一行为"#!/bin/bash"或者 "#!/bin/python2"
3>对符合文件的添加al2头。
sed -i是直接修改文件,-n 可以取消屏幕的输出信息。
//add_license.sh:对一个文件操作
#!/bin/bashset -e
set -o xtrace
file=$1
# for file has blank lines at the head, remove those blank lines.
sed -i '/./,/^$/!d' $file
# for file has a license head,exit
#if [[ ` cat $file | grep -iHn "license" | wc -l` -gt 0 ]]; then
# exit 0
#fi
# for files already had al2 head, exit.
if [[ `cat $file | grep "licenses/LICENSE-2.0" | wc -l` -gt 0 ]]; then
if [[ `cat $file | grep "Unless required by applicable law" | wc -l` -gt 0 ]]; then
if [[ `cat $file | grep "Copyright" | wc -l` -gt 0 ]]; then
exit 0
fi
fi
fi
# for files already have MIT Licese, exit.
if [[ ` cat $file | grep "licensed under the MIT" |