Newbee: search for folder and add it to PATH

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 17
Reputation: marcux is an unknown quantity at this point 
Solved Threads: 0
marcux marcux is offline Offline
Newbie Poster

Newbee: search for folder and add it to PATH

 
0
  #1
Jun 6th, 2008
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:
Shell Scripting Syntax (Toggle Plain Text)
  1. 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:
Shell Scripting Syntax (Toggle Plain Text)
  1. PATH="printed_string/bin:${PATH}"
With the PATH for java set I can run my java app:
Shell Scripting Syntax (Toggle Plain Text)
  1. java -jar Memo1.0.0.jar
My problem is how I get hold of the printed string from "find".

Many thanks in advance!

Marcux
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Newbee: search for folder and add it to PATH

 
0
  #2
Jun 7th, 2008
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 58
Reputation: omrsafetyo is an unknown quantity at this point 
Solved Threads: 9
omrsafetyo omrsafetyo is offline Offline
Junior Poster in Training

Re: Newbee: search for folder and add it to PATH

 
0
  #3
Jun 9th, 2008
^ 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:

Shell Scripting Syntax (Toggle Plain Text)
  1. $ cd `find /usr/local -name "jdk1.6*" -type d`; mydir=`pwd`;cd -
  2. $ 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.
Last edited by omrsafetyo; Jun 9th, 2008 at 6:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Newbee: search for folder and add it to PATH

 
0
  #4
Jun 9th, 2008
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 58
Reputation: omrsafetyo is an unknown quantity at this point 
Solved Threads: 9
omrsafetyo omrsafetyo is offline Offline
Junior Poster in Training

Re: Newbee: search for folder and add it to PATH

 
0
  #5
Jun 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Newbee: search for folder and add it to PATH

 
0
  #6
Jun 11th, 2008
No problem,

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

I've gotta cut back on the speed-typing

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC