代码之家  ›  专栏  ›  技术社区  ›  IAspireToBeGladOS

用于跨目录和有条件地创建或重命名文件的Bash脚本

  •  -1
  • IAspireToBeGladOS  · 技术社区  · 6 年前

    我正在创建转换脚本以将内容移动到某个位置 Hugo

    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
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Tanktalus    6 年前

    您没有更改目录( cd )到你正在循环的目录。因此 -f $CONTENT .

    检查需要在当前工作目录设置为所需目录或完整路径的情况下完成。可能只是在做 if [[ -f ${d?}/index.md ]]; then 这样就足够了。