Bash 个用于遍历目录和有条件地创建或重命名文件的脚本
Bash scripts for transversing directories and conditionally creating or renaming files
我正在创建转换脚本以将内容移动到 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
您没有将目录 (cd
) 更改为循环访问的目录。因此 -f
检查将全部在当前目录中,甚至可能不在 $CONTENT
.
下
所有 -f
检查都需要将当前工作目录设置为所需目录或使用完整路径来完成。可能只是做 if [[ -f ${d?}/index.md ]]; then
并且同样就足够了。
我正在创建转换脚本以将内容移动到 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
您没有将目录 (cd
) 更改为循环访问的目录。因此 -f
检查将全部在当前目录中,甚至可能不在 $CONTENT
.
所有 -f
检查都需要将当前工作目录设置为所需目录或使用完整路径来完成。可能只是做 if [[ -f ${d?}/index.md ]]; then
并且同样就足够了。