| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
|
| 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. |
|
#2
|
| 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. |
|
#3
|
| 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 |
|
#4
|
| 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. |
|
#5
|
| 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 |
|
#6
|
| > 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 |
|
#7
|
| 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 |
|
#8
|
| 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. |
![]() |
| Thread Tools | |
| Display Modes | |