已达到最大嵌套函数级别

  • 本文关键字:函数 嵌套 bash zsh
  • 更新时间 :
  • 英文 :


>我有目录层次结构,其中"根"目录有一个名为"text.txt"的文件。 我想找到这个"根"目录,然后从其中运行命令"foo"。 这是我目前拥有的

# Locates the root directory
locateRoot () {
  local root
  #Findup looks up through the directory hierarchy. I'm sure this works.
  root=$(findup text.txt  2>&/dev/null)  
  if [[ $? -ne 0 ]]
    then
    echo "Root not found"
    return 1
  fi
  echo root
}
# Does foo from within the root directory
runFoo () {
  local myDir 
  myDir=$(locateRoot)
  pushd $myDir 1>&/dev/null
  foo $@
  popd 1>&/dev/null
}

但是,每当我运行此程序时,我都会得到:

maximum nested function level reached

我有什么问题?我很肯定 foo 按预期工作。

在你locateRoot函数中,你只echo root没有它的内容,这是错误的,你的脚本似乎很长,无法执行一些简单的任务text.txt

#! /bin/bash
locateRoot () 
{
    var=$(find / -iname "text.txt" -printf '%hn' | sort -u)
    printf "%s n" "$var"
}

您可以看到包含该文件的该目录的绝对路径。您可以修改上面的脚本以根据需要执行某些任务,只需cd到该目录即可,例如

cd $var
//execute your command in $var directory

最新更新