开发者

Shell script works in bash but not in ksh

I need to write a script to test if the command blablabla exists in the classpath. So I wrote the following code:

if ! hash blablabla >/dev/null 2>&1; then
   echo not found
fi

This works fine when the script is executed in the bash. But if I try it in KSH, then it doesn't work:

#! /usr/bin/ksh

if ! hash blablabla >/dev/null 2>&1; then
   ech开发者_如何学Pythono not found
fi

I expect the echo not found to be executed but instead I get nothing. What's the problem?


I believe command is portable (if that matters):

command -v -- some_command >/dev/null 2>&1 ||
  printf '%s\n' "not found"  


In bash hash is a builtin command. In ksh it's an alias; aliases aren't active in shell scripts.

alias hash='alias -t --'

Try the which command, which is an external command and therefore shell-independent:

if ! which -s blablabla; then
    echo not found >&2
fi


Tha hash command is a shell built-in command in bash, but not in ksh. You might want to use whence instead.

if ! whence blah; then print urgh; fi  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜