【问题标题】:Find all files with .md extension and execute a command with the file and generate a new file with a name generated through the the md file name查找所有扩展名为 .md 的文件并对该文件执行命令并生成一个新文件,该文件的名称通过 md 文件名生成
【发布时间】:2018-03-29 10:05:39
【问题描述】:

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

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

找到 . -name '*.md' -exec markdown-html {} -s 资源/样式/common-custom.css -o {}.html \;

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

【问题讨论】:

    标签: html linux shell sh markdown


    【解决方案1】:

    你好,你要在这里写一个小脚本,我已经描述了它是如何工作的,请参考下面代码中的cmets:-

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

      #!/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
      

    【讨论】:

    • 嗨,我不想删除 md 文件。我只想使用 md 文件生成一个 .html 文件
    • 这个命令是做什么的? markdown-html 这个the above command generates a new file "home.md.html" from "home.md" but i want the .md removed. tried different solutions but didn't work 是什么意思?我以为你想在创建 html 后删除 .md。
    • "markdown-html {} -s resources/styles/common-custom.css -o {}.html" 命令应该使用 md 文件生成一个 html 文件。我的问题是上面的命令将html文件生成为“home.md.html”,但我希望它像“home.html”。所以在所有 .md 文件都会有其相应的 html 文件,如“home.md”和 home.html
    • 你不明白我在问什么。这条命令markdown-html 是否会生成一个新的html文件? -s resources/styles/common-custom.css -o是命令行参数吗?
    • yes "markdown-html" 是用于将md转换为html文件的命令
    【解决方案2】:

    下面的脚本可以解决扩展问题。

    #!/bin/bash
    find . -name '*.md' > filelist.dat 
    # list down all the file in a temp file
    while read file
    do
        file=`echo ${file%.md}`
        #get the filename witout extension
    
        markdown-html $file -s resources/styles/common-custom.css -o $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
    

    【讨论】:

      【解决方案3】:

      如果你想多次使用 find 的输出,你可以试试这样:

      find . -name "*.md" -exec sh -c "echo {} && touch {}.foo" \;
      

      注意:

      sh -c "echo {} && touch {}.foo"
      

      sh -c 将运行从字符串中读取的命令,然后{} 将被替换为查找输出,在此示例中首先执行echo {},如果成功则&amp;&amp; 然后将@987654327 @,在你的情况下,这可能是这样的:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-11
        • 1970-01-01
        • 1970-01-01
        • 2011-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多