【发布时间】:2015-02-05 13:49:39
【问题描述】:
我想将./src/下的所有markdown文件递归转换为html:
#!/bin/bash
function do_recursive_pandoc {
local markdown_src_file_extension=\*.markdown
local markdown_src_path="$1"
local html_output_path="$2"
mkdir "$html_output_path" 2>/dev/null
for i in $(find $markdown_src_path -name $markdown_src_file_extension 2>/dev/null | cut --delimiter='/' --fields=2- )
do
mkdir "$html_output_path"/$(dirname "$i") 2>/dev/null
pandoc -rmarkdown -whtml "$markdown_src_path"/"$i" --output="$html_output_path"/"$i".html
done
}
do_recursive_pandoc "src" "output"
但如果我在文件路径或名称中有空格,bash 会将它们视为for 循环中的多个项目,例如,如果我有:
./src/dir 1/foo.markdown
此脚本将创建./output/1 目录而不是创建./output/dir 1/ 并尝试转换./src/dir 和./src/1/foo.markdown 而不是./src/dir 1/foo.markdown
我该如何解决这个问题?
【问题讨论】:
-
使用更多引号。不要解析
find的输出:使用它的-exec标志或使用glob。
标签: bash for-loop whitespace filenames filepath