Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've come across an interesting problem that I have not yet been able to solve satisfyingly.

Consider a note taking command that takes two arguments:

note $project  $note

That simply files the content of $note under a project according to the first parameter.

Now I have two projects where I enter some data:

note "projectA" "asdfasdf"
note "projectB" "weradfvsaf"

This worked well when I had few projects. Then I got more projects and I had an Idea: Use the current path to determine the name of the project:

+~
+~/projects/projectA
+~/projects/projectB/subdir
+~/projects/...

So I wrote a wrapper for the above function that parses the project name out of the directory path.

This worked very well, until I started to have several projects spanned across multiple paths:

+~
+~/projects/projectA
+~/projects/projectB/subdir
+~/projects/temp/projectA
+~/test/projectA

Now my problem is: How can I continue to be lazy? I thought the easiest way would be to create a file, for instance project.rc, that only contains the name of the project that is contained in the folder and all subfolders of where it is located. The wrapper simply looks in the current dir for this file and continues to traverse upwards in the directory tree until the file is found. Then the file is read and the $project parameter is set accordingly.

I am not really familiar with shell scripting; Could anyone come up with a solution for this problem?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

This will do the trick:

 PPATH=$(while [[ "$PWD" != / && ! -f project.rc ]]; do cd ..; done; echo "$PWD")

The variable PPATH will then contain the path of the project from which you call the command. You can then find all the info you need in your $PPATH/project.rc. (The variable will be positioned to / if there is no such file in parent directories.)

share|improve this answer
1  
Simpler: PPATH=$(pwd -P); while [[ ! -e project.rc && -n $PPATH ]]; do PPATH=${PPATH#/*}; done –  Gilles Apr 23 '12 at 23:26
    
Indeed, it's more efficient and the "not found" exception can be handled a bit more easily with this one. –  Stéphane Gimenez Apr 24 '12 at 8:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.