Finding a script/program on $PATH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding a script/program on $PATH
# 1  
Old 05-19-2014
Finding a script/program on $PATH

I am trying to put together a script to find a requested script or program on the user's search path. Am trying to replace the colons separating the parts of a path with a newline to let xargs pass the directories to a list command, but I haven't gotten that far. This is my progress:

Code:
 echo $PATH |sed "s/:/^M/g"

Any ideas? TIA

Last edited by Don Cragun; 05-19-2014 at 07:39 PM.. Reason: Remove FONT and SIZE tags from CODE data.
# 2  
Old 05-19-2014
Quote:
Originally Posted by wbport
I am trying to put together a script to find a requested script or program on the user's search path. Am trying to replace the colons separating the parts of a path with a newline to let xargs pass the directories to a list command, but I haven't gotten that far. This is my progress:

Code:

 echo $PATH |sed "s/:/^M/g"
 

Any ideas? TIA
Code:
echo $PATH | sed -e 's/:/\n/g' | xargs <command>

# 3  
Old 05-20-2014
You could try several ways. Here's one with tr:-
Code:
tr ":" " " <<-END | xargs ls -l 
$PATH
END

Something with the IFS variable is possible too, but what is your goal? That might give us a different approach entirely.




Robin
These 2 Users Gave Thanks to rbatte1 For This Post:
# 4  
Old 05-20-2014
Quote:
Originally Posted by rbatte1
You could try several ways. Here's one with tr:-
Code:
tr ":" " " <<-END | xargs ls -l 
$PATH
END

Something with the IFS variable is possible too, but what is your goal? That might give us a different approach entirely.

Robin
tr ":" "\n" did what I needed. sed didn't do anything with the backslash but ignore it, the letter "n" separated the path directories rather than ":". Thanks for looking at it.
# 5  
Old 05-20-2014
You must have an ancient BSD-ish sed, many newer ones accept \n these days.
# 6  
Old 05-20-2014
Quote:
Originally Posted by wbport
I am trying to put together a script to find a requested script or program on the user's search path. Am trying to replace the colons separating the parts of a path with a newline to let xargs pass the directories to a list command, but I haven't gotten that far. This is my progress:

Code:
 echo $PATH |sed "s/:/^M/g"

Any ideas? TIA
Perhaps I'm completely missing the point, however, why don't you just use "which"

Code:
> which grep
/usr/bin/grep

Let the system do the heavy lifting for you?
Now you have the full path to the command, just report it in whatever way you want ??
This User Gave Thanks to Ditto For This Post:
# 7  
Old 05-20-2014
And I have seen several "solutions" like this that were unnecessary.
Usually you set up your PATH first, then simply run the commands, and let the shell find them.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Finding the path to an executable , installed package

Hi all, Recently i wanted to see if i have openssl installed in my system (solaris 10), so i do this (not sure if this is the right way to do this) pkginfo -i | grep -i "ssl" system SUNWopenssl-commands OpenSSL Commands (Usr) system SUNWopenssl-include ... (3 Replies)
Discussion started by: javanoob
3 Replies

2. Programming

Finding the path of the C program

Hi All, I have a c program called findPath.c in a path /home/harsh/c-Programs/. How can i find the path where the program is stored at runtime?? I have given the following #include<stdio.h> int main() { system("dirname $0"); return 0; } This is resulting in the output as . <single dot... (6 Replies)
Discussion started by: sreeharshasn
6 Replies

3. UNIX for Advanced & Expert Users

Finding register set being used in program

How can i find( or list) contents of all registers being used by my program? Is there any system call or library available for this?:confused: At runtime in my c/c++ program. At runtime using may be some assembly hack!!!!!!!!!!! (2 Replies)
Discussion started by: amit gangarade
2 Replies

4. Programming

get the path of current running program

How does the program know the full path of itself when the program is running in certain diretory? BTW, I have no "argv" information of main() functino. (The program is written in C++ on linux platform) (1 Reply)
Discussion started by: princelinux
1 Replies

5. UNIX for Dummies Questions & Answers

finding a file in its path

I have a file that has multiple entries within the Unix system. Korn shell scripts are calling this file (also a ksh) with a . in front of it, and I'm trying to determine which file it is using based on the $PATH by finding where it is located first. Any suggestion on how to go about this?... (2 Replies)
Discussion started by: gavineq
2 Replies

6. Shell Programming and Scripting

Finding path of a running script

Hi, I just want to know any code by which i can get the path of the script which i am running. This is required to store the output in the same directory from where the script is running. pwd fails if I give absolute path of script from some other directory. Thanks in advance Puneet (3 Replies)
Discussion started by: puneet
3 Replies

7. Shell Programming and Scripting

Finding relative path of a file

I have to relatively get the path of a file to use it in the script. The directory structure is /export/opt/XTools/ and under this there are several version directories - 1.0_A0, 1.0_A1, 1.0_A2 etc.,. The actual file is under these directories: installscript.sh My script should pickup the... (4 Replies)
Discussion started by: chiru_h
4 Replies

8. Programming

finding stack location in C using program

Is there a way to find the address of stack memory writing a program? Please guide me (12 Replies)
Discussion started by: jacques83
12 Replies

9. IP Networking

finding the java path in AIX

How to retrieve the path of Java bin directory in AIX (or any unix OS)?? Actually my problem is, I have a jar file called App.jar. I want to execute it by calling the javaw executable. My command is "/usr/java130/javaw -jar App.jar". I've written this command into a shell script. Now,... (2 Replies)
Discussion started by: fermisoft
2 Replies

10. UNIX for Dummies Questions & Answers

Finding current working dir path

Hi Folks, In a Unix (ksh) script, is there a way to determine the current working directory path of another logged-in user? Of course, I can use "pwd" to find my own path. But, how do I find it for another active user? Thanks for any input you can provide. LY (6 Replies)
Discussion started by: liteyear18
6 Replies
Login or Register to Ask a Question