Frage

I have directory hierarchy where the "root" directory has a file called "text.txt". I want to find this "root" directory and then run the command 'foo' from within it. Here is what I currently have

# 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
}

However, whenever I run this program I get:

maximum nested function level reached

What's wrong with what I have? I'm positive that foo works as expected.

War es hilfreich?

Lösung

in you locateRoot function you just echo only root not content of it, which is wrong and your script seems to very lengthy to perform some simple task.i give you sample script which print path to directory which contain text.txt file.

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

you can see absolute path to that directory which contain that file. You can modify above script to perform certain task as you want by just cd to that directory like

cd $var
//execute your command in $var directory
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top