我正在创建转换脚本,以将内容移动到Hugo可以支持的内容中。我仍在学习Bash,所以我怀疑这段代码的逻辑中有一些错误-它默认为最后一个“ elif”子句,该子句默认情况下只是创建一个新的_index.md而不是检查其他任何子句。
任何向我指出正确方向进行调试的指导,将不胜感激!
for d in `find ${CONTENT?} -type d ! -name 'media' ! -name 'releases' ! -name 'css' ! -name 'img'`
do
NAME=$(echo ${PWD##*/})
# Does index.md exist?
if [[ -f index.md ]]; then
echo_info "A base file already exists; renaming to _index.md."
mv ${d?}/index.md ${d?}/_index.md
# Does _index.md exist?
elif [[ -f _index.md ]]; then
echo_info "_index.md already exists for the selected content directory."
# Does a file exist with the same name as the directory?
elif [[ -f ${NAME?}.md ]]; then
echo_info "A base file already exists; renaming to _index.md."
mv ${d?}/${NAME?}.md ${d?}/_index.md
# If none of the above exist, default to creating a new _index.md.
elif [[ ! -f index.md || ! -f _index.md || ! -f ${NAME?}.md ]]; then
echo_info "Creating _index.md for directory."
cd ${BUILD?} && hugo new ${d?}/_index.md
fi
done
答案 0 :(得分:2)
您没有将目录(cd
)更改为要遍历的目录。因此,-f
检查将全部位于当前目录中,甚至可能不在$CONTENT
下。
所有-f
检查都需要将当前工作目录设置为所需目录,或者使用完整路径。大概只做if [[ -f ${d?}/index.md ]]; then
就足够了。