![Shell 脚本 (bash) 添加或替换 .c 、 .h 和 makefile 中包含的许可证文本?](https://linux22.com/image/38951/Shell%20%E8%84%9A%E6%9C%AC%20(bash)%20%E6%B7%BB%E5%8A%A0%E6%88%96%E6%9B%BF%E6%8D%A2%20.c%20%E3%80%81%20.h%20%E5%92%8C%20makefile%20%E4%B8%AD%E5%8C%85%E5%90%AB%E7%9A%84%E8%AE%B8%E5%8F%AF%E8%AF%81%E6%96%87%E6%9C%AC%EF%BC%9F.png)
我在一个文件夹中有一组 *.c 、 *.h 和 Makefiles,其中一些文件包含许可证文本,而一些文件没有任何许可证文本。因此,我需要一个 shell 脚本,如果文件没有许可证文本,我可以在其中添加许可证文本,如果许可证文本已经存在,那么我想用新的许可证文本替换它。
例如
Folder1
┣━ *.c
┣━ *.h
┣━ Folder2
┃ ┣━ *.c
┃ ┣━ *.h
┃ ┣━ Makefiles
┃ ┗━ Folder4
┗━ Folder3
┣━ *.c
┣━ *.h
┗━ Makefiles
笔记:许可证文本始终位于文件的开头。
现有许可证文本示例:
# Copyright (C) 2008 Jack <[email protected]>
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
新的许可证文本应为:
/*---------------------------------------------------------------------
Copyright © 2014 Author Name
All rights reserved
----------------------------------------------------------------------*/
对于 Makefile 来说应该是:
# ---------------------------------------------------------------------
# Copyright © 2014 Author Name
#
# All rights reserved
# ----------------------------------------------------------------------
答案1
假设bash:
function remove_copyright {
printf "%s\n" 1,10d w q | ed "$1"
}
function add_copyright {
if [[ $1 == Makefile ]]; then
ed "$1" <<END
0i
# ---------------------------------------------------------------------
# Copyright © 2014 Author Name
#
# All rights reserved
# ---------------------------------------------------------------------
.
w
q
END
else
ed "$1" <<END
0i
/*---------------------------------------------------------------------
Copyright © 2014 Author Name
All rights reserved
---------------------------------------------------------------------*/
.
w
q
END
fi
}
shopt -s nullglob globstar
for file in **/*.[ch]; do
if grep -q '^# Copyright \(C\)' "$file"; then
remove_copyright "$file"
fi
add_copyright "$file"
done
答案2
此脚本检查文件是否*.c
以*.h
开头/* Copyright (C)
,Makefile*
文件是否以 开头# Copyright (C)
。
LICENCEFILE
如果是这样,此脚本将在每个文件顶部打印您指定的版权文本作为注释。
#!/bin/bash
LICENCEFILE="licence"
[ ! -f "$LICENCEFILE" ] && echo "$LICENCEFILE is missing. Abort." && exit 1
for i in *.c *.h; do
[ "$(head -c16 $i)" == "/* Copyright (C)" ] && continue
NEWFILE="${i}.new"
[ -f "$NEWFILE" ] && echo "Sorry, $NEWFILE already exists" && continue
echo "/* " > "$NEWFILE"
cat "$LICENCEFILE" >> "$NEWFILE"
echo "*/" >> "$NEWFILE"
cat "$i" >> "$NEWFILE"
done
for i in Makefile*; do
[ "$(head -c15 $i)" == "# Copyright (C)" ] && continue
NEWFILE="${i}.new"
[ "${i#*.}" == "new" ] && continue
[ -f "$NEWFILE" ] && echo "Sorry, $NEWFILE already exists" && continue
while read line; do
echo "# $line" >> "$NEWFILE"
done < "$LICENCEFILE"
cat "$i" >> "$NEWFILE"
done
例子LICENCEFILE
:
Copyright (C) year AuthorName <[email protected]>
licence text
licence text
LICENCEFILE
前 13 个字符必须包含“版权 (C)”。
上面的脚本生成的文件是所有找到的文件*.new
的修改版本。验证脚本产生正确的输出后,只需使用以下命令覆盖旧文件:*.c
*.h
Makefile*
for i in *.new; do mv "$i" "${i%.new}"; done