.c, .h 및 makefile에 포함된 라이센스 텍스트를 추가하거나 대체하는 쉘 스크립트(bash)가 있습니까?

.c, .h 및 makefile에 포함된 라이센스 텍스트를 추가하거나 대체하는 쉘 스크립트(bash)가 있습니까?

폴더에 *.c, *.h 및 Makefile 세트가 있는데 그 중 일부에는 라이센스 텍스트가 포함되어 있고 일부에는 라이센스 텍스트가 없습니다. 따라서 파일에 라이센스 텍스트가 없는 경우 라이센스 텍스트를 추가할 수 있고 라이센스 텍스트가 이미 존재하는 경우 이를 새 라이센스 텍스트로 대체할 수 있는 쉘 스크립트가 필요합니다.

예를 들어

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

배쉬를 가정하면:

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자는 "Copyright(C)"를 포함해야 합니다.

위 스크립트에 의해 생성된 파일은 발견된 모든 파일 *.new의 수정된 버전 입니다. 스크립트가 올바른 출력을 생성하는지 확인한 후 다음 명령을 사용하여 이전 파일을 덮어쓰면 됩니다.*.c *.hMakefile*

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

관련 정보