叫别人做网站权重被转移了,上海人才网官网登录不进去,土巴兔装修,劳务建筑公司网站由于一些原因#xff0c;需要将以前编写的所有markdown文件转成docx文件#xff0c;以便做一个备份#xff0c;特别是原文档中引用的图片需要嵌入docx文件#xff0c;作本地化保存。先上脚本吧#xff1a;
sudo yum -y install pandoc
# set new line char as IFS
IFS$\…由于一些原因需要将以前编写的所有markdown文件转成docx文件以便做一个备份特别是原文档中引用的图片需要嵌入docx文件作本地化保存。先上脚本吧
sudo yum -y install pandoc
# set new line char as IFS
IFS$\n# convert...
for srcFile in $(find . -type f -name *.md); dosinkFile$(dirname $srcFile)/$(basename $srcFile .md).docxecho source file: $srcFileecho sink file: $sinkFilepandoc -o $sinkFile $srcFile
done# restore default IFS chars
IFS$ \t\n这个脚本里还是有不少“知识点”的这里特别强调以下几条 由于文件名可能含有空格在迭代时会被截断使用双引号包裹srcFile变量$srcFile 并不能解决问题因为迭代的元素已经不是一行一行的文件路径了使用echo source file: $srcFile打印一下问题就能暴露出来。真正有效的做法是必须设定IFS将其设为换行符\n只有这样才能正确地将find输出的一整行可能包含空格的文件路径解析为一个独立的元素 为IFS设置换行符\n时必须是IFS$\n不是IFS\n$不可省略 上述命令使用 find . -type f -name *.md -exec sh -c ... sh {} 这种形式也可以实现好处是不用特别配置IFS了在-exec中{}能完好表示每一行输出不存在空格截断问题。不过因为在这个案例中我们还是要在文件路径的基础上使用dirname和basename来拼接我们需要的目标文件路径,同时也无法避免不使用for循环所以这时使用-exec的优势并不明显反而还很难阅读所以不如使用上面的传统模式来得很简洁一些。以下是find命令的-exec和-execdir的一些测试命令对于理解它们的用法有一定的帮助
测试
find . -type f -name *.md -exec ls {} \;
find . -type f -name *.md -execdir ls {} \;
find . -type f -name *.md -exec dirname {} \;
find . -type f -name *.md -execdir dirname {} \;
# output:
find . -type f -name *.md -exec sh -c for name dols $(dirname $name)/$(basename $name)done sh {} find . -type f -name *.md -exec sh -c for name dols $(dirname $name)/$(basename $name .md)done sh {} find . -type f -name *.md -exec sh -c for name doecho $(dirname $name)/$(basename $name .md).docxdone sh {}
补充说明 -execdir command {} Like -exec, but the specified command is run from the subdirectory containing the matched file ...参考
https://unix.stackexchange.com/questions/389705/understanding-the-exec-option-of-find