View previous topic :: View next topic |
Author |
Message |
woodm Tux's lil' helper
Joined: 18 Jun 2002 Posts: 75
|
Posted: Mon Apr 07, 2003 3:18 am Post subject: Bash programming question. |
|
|
So the changes to the tcsh structure finally gave me the motivation I need to make the switch to bash. But we will leave the politics of that for another time.
My question is this:
Is there a way to check and see if a command is in your path?
I would like to be able to use a
Code: | if [ -X less ]
then
...
| type syntax instead of creating some sort of ugly
Code: | if [ `which less` ]
then
...
| type thing. The which version above will only get me in trouble with scp and stderr, and it's just not clean.
Can you guys/gals help me?
Thanks. _________________ There are thousands of types of people in this world:
The type that seperates people into two groups,
and the thousands of other types. |
|
Back to top |
|
|
avendesora Veteran
Joined: 16 Aug 2002 Posts: 1739 Location: Betelgeuse vicinity
|
Posted: Mon Apr 07, 2003 10:47 am Post subject: |
|
|
I don't think there's a generic way of doing this without relying on external programs (like which).
You can do something like this though (cheap which):
Code: | #! /bin/bash
IFS=:
for i in $PATH ; do
if [ -x $i/$1 ] ; then
echo $i/$1
exit 0
fi
done
exit 1
|
|
|
Back to top |
|
|
duff Guru
Joined: 19 Jun 2002 Posts: 466 Location: Clemson, SC
|
Posted: Mon Apr 07, 2003 2:39 pm Post subject: Re: Bash programming question. |
|
|
woodm wrote: | type syntax instead of creating some sort of ugly
Code: | if [ `which less` ]
then
...
| type thing. The which version above will only get me in trouble with scp and stderr, and it's just not clean.
|
What's so ugly about it? If you don't want the messages displayed, redirect the stderr to /dev/null
Code: | if [ `which less 2>/dev/null` ]
then
...
|
|
|
Back to top |
|
|
woodm Tux's lil' helper
Joined: 18 Jun 2002 Posts: 75
|
Posted: Mon Apr 07, 2003 5:09 pm Post subject: There was a way in TCSH |
|
|
I know that in tcsh you could use syntax like this:
Code: | # if vim is in my path and executable...
if (-X vim) then
alias vi 'vim'
endif |
The capital X worked differently from the lowercase x in that it checked the path.
It just surprised me that there wasn't an equivelent in bash. I will see what I can do.
Thank you both for your suggestions. If I didn't play around with such options, I would never learn. _________________ There are thousands of types of people in this world:
The type that seperates people into two groups,
and the thousands of other types. |
|
Back to top |
|
|
|