Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 查找扩展名为.md的所有文件,并使用该文件执行命令,并使用通过md文件名生成的名称生成新文件_Html_Linux_Shell_Sh_Markdown - Fatal编程技术网

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

Html 查找扩展名为.md的所有文件,并使用该文件执行命令,并使用通过md文件名生成的名称生成新文件,html,linux,shell,sh,markdown,Html,Linux,Shell,Sh,Markdown,我正在尝试编写一个shell脚本,以递归方式查找扩展名为.md的目录下的所有文件,并使用.md文件执行命令,生成具有相同名称但扩展名不同的新文件 下面是我使用的命令,但它实际上是将.html附加到文件中,而不是将.md替换为.html 找到-名称'*.md'-exec标记html{}-s resources/styles/common-custom.css-o{}.html\ 上面的命令从“home.md”生成一个新文件“home.md.html”,但我希望删除.md。尝试了不同的解决方案,但没

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

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

找到-名称'*.md'-exec标记html{}-s resources/styles/common-custom.css-o{}.html\


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

您好,您必须在这里编写一个小脚本,我已经描述了它的工作原理,请参考以下代码中的注释:-

  • 首先创建一个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
    
  • 现在执行文件:-

    ./convertTohtml.sh
    

  • 如果要多次使用find的输出,可以尝试以下方法:

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

    sh -c "echo {} && touch {}.foo"
    
    sh-c
    将运行从字符串读取的命令,然后将
    {}
    替换为find输出,在本例中,首先执行
    echo{}
    ,如果成功,则
    &
    触摸{}.foo
    ,在您的情况下,这可能类似于:

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

    下面的脚本将用于解决扩展问题

    #!/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
    
    #/bin/bash
    找到-名称'*.md'>filelist.dat
    #列出临时文件中的所有文件
    读取文件时
    做
    file=`echo${file%.md}`
    #获取不带扩展名的文件名
    标记html$file-s resources/styles/common-custom.css-o$file.html
    完成
    Hi,我不想删除md文件。我只想使用md files生成一个.html文件这个命令做什么
    markdownhtml
    你说的
    是什么意思?上面的命令从“home.md”生成一个新文件“home.md.html”,但我想删除.md。尝试了不同的解决方案,但没有成功
    ?我认为您应该在创建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.htmlYou不明白我到底在问什么。此命令是否生成新的html文件?是
    -s resources/styles/common-custom.css-o
    是命令行参数吗?是“markdown html”是用于将md文件转换为html文件的命令
    #!/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