| | |
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:
Solved Threads: 0
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:
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:
With the PATH for java set I can run my java app:
My problem is how I get hold of the printed string from "find".
Many thanks in advance!
Marcux
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)
find /usr/local -name "jdk1.6*" -type d
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)
PATH="printed_string/bin:${PATH}"
Shell Scripting Syntax (Toggle Plain Text)
java -jar Memo1.0.0.jar
Many thanks in advance!
Marcux
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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:
Hope that helps 
, Mike
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

, 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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Apr 2008
Posts: 58
Reputation:
Solved Threads: 9
^ 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:
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.
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)
$ 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.
Last edited by omrsafetyo; Jun 9th, 2008 at 6:54 pm.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
Good call,
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
No problem,
And thanks for overlooking the fact that I spelled event "even"
I've gotta cut back on the speed-typing
, Mike
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: Random number generator with awk
- Next Thread: Command Menu Shell Script Help
| Thread Tools | Search this Thread |
Tag cloud for Shell Scripting





PATH