LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 04-04-2014, 02:36 AM   #1
ajaxStardust
LQ Newbie
 
Registered: Aug 2012
Location: GMT -5 (aka. EST)
Distribution: Mint, Cygwin, MX, Porteus
Posts: 29

Rep: Reputation: Disabled
Question ENV $PATH: discover text patterns


System Environment: Cygwin under Win7

Scenario: Software-Title enumerates prerequisite installations, Soft-A["Python"] and Soft-B["wxPython"]

FIRST THOUGHT:
Uncertain whether the software is installed on this system, I resolve I might quickly determine the answer by checking "$PATH".
This approach occurs to me, from experience with Python installers adding /PATH/TO/PYTHON/BIN to the Environment Variable, $PATH

SECOND THOUGHT:
Knowing a bit about the power of GNU Utilities / BASH / the Linux CLI, etc., I immediately turn this simple task (E.g. Visual observation of the contents of $PATH) to something much more complicated (E.g. author a thread at LinuxQuestions.org, follow up for replies, etc.), because I am determined to figure out how-to figure this out without actually having to read the $PATH contents.

THIRD THOUGHT:
grep. i should be able to pass this data to egrep to locate the simple pattern, such as --regexp "python". But, apparently this isn't going to work, as i don't know how to send the data to GREP. perhaps I need to use SED, or a different type of text processor, instead?
Example Attempt:I TEE'd the $PATH contents to "path_value.txt", only to recall i can't send a singular file to EGREP (having confused the use of GREP option "--file" for my desired result, vs the actual use whereby GREP reads patterns from --file="PATTERNS.File")

Begging your pardon for my tendency to ask questions about "tinkering around" with stuff...
What is your recommendation?

Presuming the reader/ respondent is also unwilling to Visually/ Manually scan the $PATH data for a desired TXT pattern, what would be YOUR approach to discovering the contents of $PATH? (E.g. if GREP is not the best choice)

Last edited by ajaxStardust; 04-04-2014 at 02:42 AM. Reason: included example solution as failed
 
Old 04-04-2014, 08:38 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
I'm not sure I understand your question. If I *do* understand it you want to find out if a utility exists somewhere in the path then you want to use the which command.

Code:
which python
Using that in a script...

Code:
if ! which python &> /dev/null;then
  echo "Python is not installed."
else
  echo "$(python --version) is installed."
fi
However, that won't detect if you have multiple versions of python installed. It will only detect if you have at least one binary called python installed somewhere in the $PATH. If you want to know about all possible instances of python installed in the $PATH you should do the following...

Code:
if ! which python &> /dev/null;then
  echo "Python is not installed."
else
  echo "$PATH" | tr ':' '\n' | sort -u | while read x;do 
    [ -f "$x/python" ] && ( echo -n "$x/python - ";$x/python --version )
  done
  echo "Default: $(which python) - $(python --version)"
fi
That last snippet of code will test if python is installed... if it is it will iterate through all of the paths in the $PATH variable looking for an executable file called python. Then it lists the path of each version and the version of python it is. At the end it shows the default python instance if the user simply types python on the shell. I just arbitrarily chose the python executable but you can wrap that in a function and choose any executable to find all versions of it.

Hopefully, I understood your question.

Last edited by sag47; 04-04-2014 at 08:46 AM.
 
  


Reply

Tags
cygwin, env, grep, path, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
scan text for patterns sycamorex Linux - Software 2 04-04-2011 10:03 AM
editing the Env. Var. PATH drmjh Linux - General 4 10-19-2007 08:39 AM
Deleting text between two different patterns activeco Linux - General 7 09-13-2005 06:05 PM
$PATH env Problems Coda Linux - Newbie 8 11-17-2004 06:04 PM
C shell and env $PATH vikinnun Programming 1 11-10-2004 09:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 06:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration