azizny
09-19-2004, 06:59 PM
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,
sea otter
09-19-2004, 07:40 PM
echo $PATH | grep <search term>
If the directory named "search term" is in your path, you'll get back a string, otherwise, null.
azizny
09-19-2004, 07:43 PM
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,
azizny
09-19-2004, 07:49 PM
LOL....... I knew i should've took the <> from the search string..
thanks alot brother
Peace,
azizny
09-19-2004, 07:57 PM
I actually tried this:
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,
azizny
09-19-2004, 08:07 PM
Ok worked around it... added echo..
the problem is it still echos the results even when I do > dev.null
Peace,
inimino
09-19-2004, 08:13 PM
2> is only redirecting stderr.
Um... just a question about this:
PATH=/$1:$PATH
What's the slash doing there?
sea otter
09-19-2004, 09:49 PM
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:
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):
export PATH=$1:$PATH
azizny
09-20-2004, 11:00 AM
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:
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):
export PATH=$1:$PATH
Woked fine.. thanks alot guys..
this is the code I made:
#!/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,
inimino
09-20-2004, 11:48 AM
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,