gpt4 book ai didi

bash - 达到最大嵌套函数级别

转载 作者:行者123 更新时间:2023-12-04 18:08:01 26 4
gpt4 key购买 nike

我有目录层次结构,其中“根”目录有一个名为“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 '%h\n' | sort -u)
printf "%s \n" "$var"
}

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

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

关于bash - 达到最大嵌套函数级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21843151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com