使用脚本将多个目录中的多个 Markdown 文件转换为 HTML

发布于 2024-12-21 08:25:26 字数 742 浏览 2 评论 0原文

我目前有一个很大的目录,其中充满了许多子目录,每个子目录中都有相同名称的 markdown 文件:

/big-directory/sub-directory-name-1/sub-directory-name-1.md

/big-directory/sub-directory-name-2/sub-directory-name-2.md

/big-directory/sub-directory-name-3/sub-directory-name-3.md

我想以这样的结果结束:

/big-directory/sub-directory-name-1/sub-directory-name-1.md
/big-directory/sub-directory-name-1/index.html

/big-directory/sub-directory-name-2/sub-directory-name-2.md
/big-directory/sub-directory-name-1/index.html

/big-directory/sub-directory-name-3/sub-directory-name-3.md
/big-directory/sub-directory-name-1/index.html

我正在尝试在 OS X 上编写一个 shell 脚本,该脚本将在每个文件上运行 multimarkdown,但我很难弄清楚如何使循环在子目录上运行,而不手动将它们全部放入脚本中。预先感谢您的任何帮助。

I currently have a big big directory filled with many sub-directories with identically named markdown files in each:

/big-directory/sub-directory-name-1/sub-directory-name-1.md

/big-directory/sub-directory-name-2/sub-directory-name-2.md

/big-directory/sub-directory-name-3/sub-directory-name-3.md

I would like to end up with this:

/big-directory/sub-directory-name-1/sub-directory-name-1.md
/big-directory/sub-directory-name-1/index.html

/big-directory/sub-directory-name-2/sub-directory-name-2.md
/big-directory/sub-directory-name-1/index.html

/big-directory/sub-directory-name-3/sub-directory-name-3.md
/big-directory/sub-directory-name-1/index.html

I'm trying to write a shell script on OS X that will run multimarkdown on each file, but I'm having difficulty figuring out how to make the loop run over the subdirectories without manually putting them all into the script. Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-12-28 08:25:26

要获取 /big-directory 下面的所有叶目录的列表,您可以使用 这个问题

然后,您可以构造一个像这样的 while 循环:

find /big-directory -type d | sort | awk '$0 !~ last {print last} {last=$0} END {print last}' | while read dir
do
    # Do something useful with $dir
    echo $dir
done

这应该给您一个起点。

To get a list of all the leaf directories below /big-directory, you can use the answer in this question.

Then, you can construct a while loop like this:

find /big-directory -type d | sort | awk '$0 !~ last {print last} {last=$0} END {print last}' | while read dir
do
    # Do something useful with $dir
    echo $dir
done

That should give you a starting point.

何以畏孤独 2024-12-28 08:25:26

我遇到了同样的问题。在阅读了这个问题和其他几个问题之后,加上大量的试验和错误,我想出了以下解决方案:

#!/bin/bash
for dir in `find /big-directory -type d`
do
  if [ -e $dir/file.md ]; #test if dir contains file
  then
    echo "converting $dir/file.md to HTML..."
    markdown $dir/file.md > $dir/file.html
  fi
done

我知道检查文件是否存在并不是严格必要的,但否则,脚本每次都会抛出错误查找不包含此文件的目录。

I encountered the same problem. After reading this question and several others, plus a lot of trial and error, I came up with the following solution:

#!/bin/bash
for dir in `find /big-directory -type d`
do
  if [ -e $dir/file.md ]; #test if dir contains file
  then
    echo "converting $dir/file.md to HTML..."
    markdown $dir/file.md > $dir/file.html
  fi
done

I know that it's not strictly necessary to check whether the file exists, but otherwise, the script will throw an error every time it finds a directory that does not contain this file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文