gpt4 book ai didi

html - 查找所有扩展名为 .md 的文件并使用该文件执行命令并生成一个新文件,其名称通过 md 文件名生成

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:11 25 4
gpt4 key购买 nike

我正在尝试编写一个 shell 脚本以递归地查找扩展名为 .md 的目录下的所有文件,并使用 .md 文件执行命令并生成具有相同名称但扩展名不同的新文件。

下面是我正在执行的命令,但它实际上是将 .html 附加到文件而不是用 .html 替换 .md

find . -name '*.md' -exec markdown-html {} -s resources/styles/common-custom.css -o {}.html \;

上面的命令从“home.md”生成了一个新文件“home.md.html”,但我想删除 .md。尝试了不同的解决方案但没有奏效

最佳答案

你好,你必须在这里写一个小脚本,我已经描述了它是如何工作的,请引用下面代码中的注释:-

  1. 首先创建一个 shell 脚本文件,如 convertTohtml.sh 并在其中添加以下代码

    #!/bin/bash
    find . -name '*.md' > filelist.dat
    # list down all the file in a temp file

    while read file
    do
    html_file=$( echo "$file" | sed -e 's/\.md//g')
    # the above command will store 'home.md' as 'home' to variable 'html_file'
    #hence '$html_file.html' equal to 'home.html'

    markdown-html $file -s resources/styles/common-custom.css -o $html_file.html

    done < filelist.dat
    # with while loop read each line from the file. Here each line is a locatin of .md file

    rm filelist.dat
    #delete the temporary file finally
  2. 向您的脚本文件提供执行权限,如下所示:-

    chmod 777 convertTohtml.sh
  3. 现在执行文件:-

    ./convertTohtml.sh

关于html - 查找所有扩展名为 .md 的文件并使用该文件执行命令并生成一个新文件,其名称通过 md 文件名生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49553290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com