簡體   English   中英

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 if [[ -f ${d?}/index.md ]]; then同樣就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM