Hi all!
I am all new to shell scripting.
I am trying to make a script that searches for a folder and then adds it to PATH
First I am serching for the folder:

find /usr/local -name "jdk1.6*" -type d

If version 1.6 of Suns java is installed I get the folder printed.
How do I get hold of the printed string?
After that I want to add it to my PATH:

PATH="printed_string/bin:${PATH}"

With the PATH for java set I can run my java app:

java -jar Memo1.0.0.jar

My problem is how I get hold of the printed string from "find".

Many thanks in advance!

Marcux

Recommended Answers

All 5 Replies

Hey There,

You can just use the value that you get from your find command by using backticks to assign it to a variable (just make sure that, if you change your find statement, it returns a full path and not a relative one, as that can cause issues for you when your shell queries the PATH variable), like this:

myvar=`find / -type d -name blah`
PATH=${myvar}:$PATH

Hope that helps :)

, Mike

^ My find command returns a relative path from the current directory - I believe this is how most find commands work.

You can write an advanced function to find the absolute path name, but here is how I would do this:

$ cd `find /usr/local -name "jdk1.6*" -type d`; mydir=`pwd`;cd -
$ PATH=${PATH}:${mydir}

This will determine the relative path; cd to it; set the variable based on the current directory (we know its a directory, because you specified, so that shouldn't fail); and then cd back to the previous directory.
From there, you set your path based on the variable.
This will allow you to search directly from the /usr/local file, instead of having to search from the root directory which would take longer and *may* return multiple directories.

Good call, find / should always return an absolute path, but your suggestion to do the extra checking is an excellent suggestion. In the even that multiple results are returned, and for saving time, this would remove those stumbling blocks.

Good show :)

, Mike

I like your answer as well - but didn't see where you started your search before I started replying (hence why I mentioned my find returns a relative path). By the time I noticed it, I was too far into the post to turn back. :)

No problem,

And thanks for overlooking the fact that I spelled event "even" ;)

I've gotta cut back on the speed-typing ;)

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.