2
我有目錄層次結構,其中「根」目錄有一個名爲「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 [email protected]
popd 1>&/dev/null
}
但是,每當我運行這個程序,我得到:
maximum nested function level reached
這有什麼錯我有什麼?我確信foo按預期工作。
你「回聲根」在第11行執行,只要你想某些任務應該是「回聲$根」。另外,當編寫一個輸出一些數據然後用$()捕獲的函數時,將它與消息混合並不是一個好主意:你應該將第8行的錯誤消息寫入stderr。 –
小心解釋一下爲什麼?對不起,我真的很陌生。謝謝:) – Nosrettap
因爲在bash中使用變量「root」的值,所以需要寫入$ root –