hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : [UNIX] How do I search the $PATH variable
Reply

Forum Jump

[UNIX] How do I search the $PATH variable

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 09-19-2004, 06:59 PM
Barti1987 Barti1987 is offline
Web Hosting Master
 
Join Date: Mar 2004
Location: USA
Posts: 4,342
Question

[UNIX] How do I search the $PATH variable


I tried to do grep '/foldername:' $PATH

and also '/foldername:' PATH

both do not work.

How can I do it?

is there another function

Peace,

__________________
Testing 1.. Testing 1..2.. Testing 1..2..3...



Sponsored Links
  #2  
Old 09-19-2004, 07:40 PM
sea otter sea otter is offline
the cloud is a lie
 
Join Date: May 2004
Location: NYC
Posts: 793
echo $PATH | grep <search term>

If the directory named "search term" is in your path, you'll get back a string, otherwise, null.

  #3  
Old 09-19-2004, 07:43 PM
Barti1987 Barti1987 is offline
Web Hosting Master
 
Join Date: Mar 2004
Location: USA
Posts: 4,342
Quote:
Originally posted by sea otter
echo $PATH | grep <search term>

If the directory named "search term" is in your path, you'll get back a string, otherwise, null.
I get:

bash: syntax error near unexpected token `newline'

When I enter

echo $PATH | grep </usr>

Peace,

__________________
Testing 1.. Testing 1..2.. Testing 1..2..3...

Sponsored Links
  #4  
Old 09-19-2004, 07:49 PM
Barti1987 Barti1987 is offline
Web Hosting Master
 
Join Date: Mar 2004
Location: USA
Posts: 4,342
LOL....... I knew i should've took the <> from the search string..

thanks alot brother

Peace,

__________________
Testing 1.. Testing 1..2.. Testing 1..2..3...

  #5  
Old 09-19-2004, 07:57 PM
Barti1987 Barti1987 is offline
Web Hosting Master
 
Join Date: Mar 2004
Location: USA
Posts: 4,342
I actually tried this:

Code:
 if ( $PATH | grep $1 ) 2> /dev/null
                        then
                                echo "Directory Already Exists in PATH"
                        else
                                PATH=/$1:$PATH
                                echo "Directory Has Been added to PATH"
                        fi
seems to be adding the thing all over...

Peace,

__________________
Testing 1.. Testing 1..2.. Testing 1..2..3...

  #6  
Old 09-19-2004, 08:07 PM
Barti1987 Barti1987 is offline
Web Hosting Master
 
Join Date: Mar 2004
Location: USA
Posts: 4,342
Ok worked around it... added echo..

the problem is it still echos the results even when I do > dev.null

Peace,

__________________
Testing 1.. Testing 1..2.. Testing 1..2..3...

  #7  
Old 09-19-2004, 08:13 PM
inimino inimino is offline
Aspiring Evangelist
 
Join Date: Aug 2004
Location: Colorado, US
Posts: 425
2> is only redirecting stderr.

Um... just a question about this:

PATH=/$1:$PATH

What's the slash doing there?

  #8  
Old 09-19-2004, 09:49 PM
sea otter sea otter is offline
the cloud is a lie
 
Join Date: May 2004
Location: NYC
Posts: 793
Sorry, stepped away for a few hours. Shame on me!

A couple of things to get it working.

As inimino said, you need a different redirect. This will work:

Code:
if ( echo $PATH | grep $1 )  >& /dev/null
Also as inimino said, what's the leading slash for?

You might want to try this (use export):

Code:
export PATH=$1:$PATH

  #9  
Old 09-20-2004, 11:00 AM
Barti1987 Barti1987 is offline
Web Hosting Master
 
Join Date: Mar 2004
Location: USA
Posts: 4,342
Quote:
Originally posted by sea otter
Sorry, stepped away for a few hours. Shame on me!

A couple of things to get it working.

As inimino said, you need a different redirect. This will work:

Code:
if ( echo $PATH | grep $1 )  >& /dev/null
Also as inimino said, what's the leading slash for?

You might want to try this (use export):

Code:
export PATH=$1:$PATH
Woked fine.. thanks alot guys..

this is the code I made:

Code:
#!/bin/ksh
                                                                      
#Aziz Hussain
#Install :: Install File
#1307
#This script will accept exactly two arguments, first is a file location the second is a directory. Copy the file to the directory and then add that directory to the PATH variable

if [[ $# < 2 ]]
# Check if exactly two arguments has been passed
then
        echo "You Must Enter Two Arguments"
else
        if [[ -d $1 ]] 2> /dev/null
        #Check if a directory is valid
        then
                if [[ -a $2 ]] 2> /dev/null
                #If so then check if the file exists
                then
 			cp $2 $1/$2
			if (  echo $PATH | grep $1 ) > /dev/null
			then
				echo "Directory Already Exists in PATH"
			else
				PATH=/$1:$PATH
				echo "Directory Has Been added to PATH"
			fi
                else
                        echo "Invalid File Entered"
                fi
        else
		echo "Invalid Directory Specified"
	fi
fi
#End of Script
As you might see the trailing slash it to add it as a folder..

tee:

will be /tee:

Peace,

__________________
Testing 1.. Testing 1..2.. Testing 1..2..3...

  #10  
Old 09-20-2004, 11:48 AM
inimino inimino is offline
Aspiring Evangelist
 
Join Date: Aug 2004
Location: Colorado, US
Posts: 425
Looks mostly ok.

About the leading slash, you're trying to make an absolute directory out of whatever is handed to your script.

But the "/" doesn't quite accomplish that.

For example, what if I am in my home directory and I enter "bin" as the first argument.
Then it will fail because /bin is already in the PATH.

It's possible to "cd" to the $1 which will also ensure that the user has permission to do so.
Then you can simply use 'pwd' to find what you add to $PATH.

Only absolute paths should be added to $PATH.

HTH,

Reply

Related posts from TheWhir.com
Title Type Date Posted
Pubcon Las Vegas 2013 Web Hosting Events 2013-09-19 18:04:12
Strategies to Reduce Operating Costs, Avoid Email Blacklisting and Improve Deliverability Webinars 2013-10-16 15:35:21
GoGrid Launches Free Cloud Migration Service through Racemi Partnership Web Hosting News 2013-04-10 11:16:38
Why You Should Embrace Google Plus Blog 2013-03-08 09:18:17
VIDEO: Troy McCasland of Attracta on SEO Software for Hosting Providers Whir Tv 2013-10-05 03:54:30


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?