Shell 脚本 (bash) 添加或替换 .c 、 .h 和 makefile 中包含的许可证文本?

Shell 脚本 (bash) 添加或替换 .c 、 .h 和 makefile 中包含的许可证文本?

我在一个文件夹中有一组 *.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 *.hMakefile*

for i in *.new; do mv "$i" "${i%.new}"; done

相关内容