Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Bash programming question.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
woodm
Tux's lil' helper
Tux's lil' helper


Joined: 18 Jun 2002
Posts: 75

PostPosted: Mon Apr 07, 2003 3:18 am    Post subject: Bash programming question. Reply with quote

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
View user's profile Send private message
avendesora
Veteran
Veteran


Joined: 16 Aug 2002
Posts: 1739
Location: Betelgeuse vicinity

PostPosted: Mon Apr 07, 2003 10:47 am    Post subject: Reply with quote

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
View user's profile Send private message
duff
Guru
Guru


Joined: 19 Jun 2002
Posts: 466
Location: Clemson, SC

PostPosted: Mon Apr 07, 2003 2:39 pm    Post subject: Re: Bash programming question. Reply with quote

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
View user's profile Send private message
woodm
Tux's lil' helper
Tux's lil' helper


Joined: 18 Jun 2002
Posts: 75

PostPosted: Mon Apr 07, 2003 5:09 pm    Post subject: There was a way in TCSH Reply with quote

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. :lol:
_________________
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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum