Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 用于横穿目录和有条件地创建或重命名文件的Bash脚本 我正在创建转换脚本以将内容移动到一些可以支持的东西。我还在学习Bash,所以我怀疑这段代码的逻辑中有一些错误-它默认为最后一个“elif”子句,默认情况下它只是创建一个新的_index.md,而不是检查任何其他子句_Linux_Bash_Hugo - Fatal编程技术网

Linux 用于横穿目录和有条件地创建或重命名文件的Bash脚本 我正在创建转换脚本以将内容移动到一些可以支持的东西。我还在学习Bash,所以我怀疑这段代码的逻辑中有一些错误-它默认为最后一个“elif”子句,默认情况下它只是创建一个新的_index.md,而不是检查任何其他子句

Linux 用于横穿目录和有条件地创建或重命名文件的Bash脚本 我正在创建转换脚本以将内容移动到一些可以支持的东西。我还在学习Bash,所以我怀疑这段代码的逻辑中有一些错误-它默认为最后一个“elif”子句,默认情况下它只是创建一个新的_index.md,而不是检查任何其他子句,linux,bash,hugo,Linux,Bash,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; renam

任何为我指明调试方向的指导都将不胜感激

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]];然后
也就足够了。

如果在脚本顶部添加一个
set-x
,它将告诉您每次评估的结果。这样可能更容易发现问题。我看到您正在使用
find
查找目录。但是你永远不会直接把它放到cd上。
NAME=$(echo${PWD###*/})
的计算结果不是总是一样的吗?与其到处使用
${foo?}
,不如只使用
set-e
一次<代码>-e将在任何变量未设置时使脚本出错。@omajid是的,我相信你是对的。仍在努力思考如何有效地完成这一部分,感谢您指出这一点!我绝对推荐快速阅读,(U&L.SE),(SO),(AskU),等等。就是这样。我太迷茫了,没有意识到,呵呵。谢谢!:)