【问题标题】:How can I add a block of text to all the PHP files in all subdirectories?如何将文本块添加到所有子目录中的所有 PHP 文件?
【发布时间】:2012-11-13 10:43:50
【问题描述】:

我有一个 PHP 脚本,我需要在顶部添加我的版权。我只是希望它出现在之后

以下是需要添加的内容:

/**
 * MySoftware
 *
 * This file is part of MySoftware.
 *
 * MySoftware is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MySoftware is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MySoftware.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @copyright   Copyright MySoftware
 * @license     GNU Public License V3.0
*/

如果可以在上述消息之前和之后添加一个空行,如果可能的话,它会在每个文件中更加突出。

将它添加到我所有的 php 文件顶部的最快方法是什么?有数百个,我已将它们全部移动到一个主文件夹/子文件夹中,因此我认为可以使用命令行递归运行某些内容以将其添加到每个文件夹的顶部。

如果有人可以通过命令行或任何其他方法发布如何执行此操作的示例脚本/代码,将不胜感激!

【问题讨论】:

  • 请注意,您要执行的操作分为两个不同的任务:递归查找目录下的所有 PHP 文件,并在文件顶部添加一个文本块。

标签: bash shell command-line command


【解决方案1】:

将您的文本保存到文件中,例如/tmp/header,然后运行以下脚本。

DIR=/your/php/files/dir
for f in $(find ${DIR} -iname "*.php" -type f 2>/dev/null) ; do
    cat /tmp/header "${f}" > /tmp/tmp_file || continue
    /bin/mv /tmp/tmp_file "${f}"
done

【讨论】:

    【解决方案2】:
    #!/bin/bash
    text="Hello world
    What's up?"
    
    exec 3<> $1 && awk -v TEXT="$text" 'BEGIN {print TEXT}{print}' $1 >&3
    

    试试看。

    【讨论】:

    • 我需要对此进行哪些更改以使其适用于所有 PHP 文件?看起来这是针对一个特定的文件...
    • 找到 . -name '*php' -打印 | xargs -J% sh /from/the/revised/answer
    猜你喜欢
    • 1970-01-01
    • 2019-05-13
    • 2021-01-05
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    相关资源
    最近更新 更多