In bash, you can achieve this e.g. like this:
mkt.sh
curr=`pwd`
file_name=build.xml
echo "Current directory: $curr"
while [ "$curr" != "/" ]; do
echo "Processing: $curr"
file="$curr/$file_name"
if [ -f "$file" ]; then
echo "Found at: $file, running command..."
cd "$curr"
"$@"
exit
fi
curr="`dirname \"$curr\"`"
done
echo "$file_name not found."
Sample run:
~$ cd /tmp
/tmp$ ls mkt.sh
mkt.sh
/tmp$ mkdir -p a/b/c/d
/tmp$ echo hello > a/build.xml
/tmp$ find a
a
a/build.xml
a/b
a/b/c
a/b/c/d
/tmp$ cd a/b/c/d
/tmp/a/b/c/d$ /tmp/mkt.sh cat build.xml
Current directory: /tmp/a/b/c/d
Processing: /tmp/a/b/c/d
Processing: /tmp/a/b/c
Processing: /tmp/a/b
Processing: /tmp/a
Found at: /tmp/a/build.xml, running command...
hello
/tmp/a/b/c/d$ rm /tmp/a/build.xml
/tmp/a/b/c/d$ r/tmp/mkt.sh cat build.xml
Current directory: /tmp/a/b/c/d
Processing: /tmp/a/b/c/d
Processing: /tmp/a/b/c
Processing: /tmp/a/b
Processing: /tmp/a
Processing: /tmp
build.xml not found.
Also, once the script finishes and control comes back to the user I want the current directory to be that one where the script was initially l
There's no need to do this in bash. Child scripts cannot change the current pwd of the parent bash process. Try this:
$ cd /tmp
$ echo "cd /usr" > a.sh
$ chmod u+x a.sh
$ pwd
/tmp
$ cat a.sh
cd /usr
$ ./a.sh
$ pwd
/tmp
As you can see, the current pwd did not change, even though the script did cd /usr inside it.