Testing whether a command exists in a simple way in script.

This is a discussion on Testing whether a command exists in a simple way in script. within the shell forums in Operating Systems category; Hi, all: If I need to test whether a command exists using command line in shell, then I simply do which . For example: # which nawk /bin/nawk # which gawk gawk: Command not found. Now I need to test whether a command exists while running a script. The way that I can think of is still using which command, and direct the output into a file. Then I can check the file to see whether it's a path or it has Command not found . But I think my approach is not a good way. There must be a much easier ...

Go Back   Database Forum > Operating Systems > shell

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 08:58 AM
Default Testing whether a command exists in a simple way in script.

Hi, all:

If I need to test whether a command exists using command line in
shell, then I simply do "which". For example:
# which nawk
/bin/nawk
# which gawk
gawk: Command not found.

Now I need to test whether a command exists while running a script.
The way that I can think of is still using "which" command, and direct
the output into a file. Then I can check the file to see whether it's
a path or it has "Command not found".

But I think my approach is not a good way. There must be a much easier
way to accomplish the goal. Would you please give ideas?

Thanks.
Reply With Quote
  #2  
Old 08-27-2008, 11:00 AM
Default Re: Testing whether a command exists in a simple way in script.

Kuhl wrote:
> Hi, all:
>
> If I need to test whether a command exists using command line in
> shell, then I simply do "which". For example:
> # which nawk
> /bin/nawk
> # which gawk
> gawk: Command not found.
>
> Now I need to test whether a command exists while running a script.
> The way that I can think of is still using "which" command, and direct
> the output into a file. Then I can check the file to see whether it's
> a path or it has "Command not found".
>
> But I think my approach is not a good way. There must be a much easier
> way to accomplish the goal. Would you please give ideas?


Try one of

which nawk && echo found

or

if which nawk
then echo found
else echo not found
fi

Suppress output of 'which' by redirecting stdout to /dev/null.

Janis

>
> Thanks.

Reply With Quote
  #3  
Old 08-27-2008, 02:49 PM
Default Re: Testing whether a command exists in a simple way in script.

On Wed, 27 Aug 2008 04:58:14 -0700 (PDT), Kuhl wrote:

> Now I need to test whether a command exists while running a script.
> The way that I can think of is still using "which" command, and direct
> the output into a file. Then I can check the file to see whether it's
> a path or it has "Command not found".


You could test the return code.

$ which gawk > /dev/null 2>&1
$ echo $?
1

$ which nawk > /dev/null 2>&1

$ echo $?
0

Reply With Quote
  #4  
Old 08-27-2008, 03:02 PM
Default Re: Testing whether a command exists in a simple way in script.

On Wednesday 27 August 2008 13:58, Kuhl wrote:

> Hi, all:
>
> If I need to test whether a command exists using command line in
> shell, then I simply do "which". For example:
> # which nawk
> /bin/nawk
> # which gawk
> gawk: Command not found.
>
> Now I need to test whether a command exists while running a script.
> The way that I can think of is still using "which" command, and direct
> the output into a file. Then I can check the file to see whether it's
> a path or it has "Command not found".
>
> But I think my approach is not a good way. There must be a much easier
> way to accomplish the goal. Would you please give ideas?


Try with

command -v nawk > /dev/null

and test the exit status (0 if found, nonzero if not)

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Reply With Quote
  #5  
Old 08-27-2008, 06:38 PM
Default Re: Testing whether a command exists in a simple way in script.

On 2008-08-27, Kuhl wrote:
> Hi, all:
>
> If I need to test whether a command exists using command line in
> shell, then I simply do "which". For example:
> # which nawk
> /bin/nawk
> # which gawk
> gawk: Command not found.


I would use 'type' rather than an external command:

type awk

> Now I need to test whether a command exists while running a script.
> The way that I can think of is still using "which" command, and direct
> the output into a file. Then I can check the file to see whether it's
> a path or it has "Command not found".


Why would you use a file? Check the return status of the
command:

if type nawk > /dev/null
then
AWK=nawk
elif type gawk > /dev/null
then
AWK=gawk
fi

> But I think my approach is not a good way. There must be a much easier
> way to accomplish the goal. Would you please give ideas?



--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Reply With Quote
  #6  
Old 08-27-2008, 07:23 PM
Default Re: Testing whether a command exists in a simple way in script.

> if type nawk > /dev/null
> then


If nawk would be not found, you'll se some errors in console sent on
stderr, rather use this:
type nawk > /dev/null 2>&1
Reply With Quote
  #7  
Old 08-27-2008, 07:27 PM
Default Re: Testing whether a command exists in a simple way in script.

Procek wrote:
>>if type nawk > /dev/null
>>then

>
>
> If nawk would be not found, you'll se some errors in console sent on
> stderr, rather use this:
> type nawk > /dev/null 2>&1


Depends on the shell. My ksh sends the output to stdout, my bash to
stderr, for example. Catching both channels is helpful of course.

Janis
Reply With Quote
  #8  
Old 09-04-2008, 09:27 PM
Default Re: Testing whether a command exists in a simple way in script.

On Wed, 27 Aug 2008 21:38:21 +0000, Chris F.A. Johnson wrote:

> On 2008-08-27, Kuhl wrote:
>> Hi, all:
>>
>> If I need to test whether a command exists using command line in shell,
>> then I simply do "which". For example: # which nawk
>> /bin/nawk
>> # which gawk
>> gawk: Command not found.

>
> I would use 'type' rather than an external command:
>
> type awk


type is indeed better than which for being a shell builtin, but it's also
better because some/all which commands will look for *csh aliases that
might not be available in a POSIX shell.

So there's more to the issue than just a little performance.
Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 03:08 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.