bash - Shell 脚本在 bash 中有效,但在 ksh 中无效

标签 bash shell ksh

我需要编写一个脚本来测试命令 blablabla 是否存在于类路径中。所以我写了下面的代码:

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

当脚本在 bash 中执行时,这工作正常。但是如果我在 KSH 中尝试它,那么它不起作用:

#! /usr/bin/ksh

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

我希望 echo not found 被执行,但我什么也没得到。有什么问题?

最佳答案

我相信 command 是可移植的(如果重要的话):

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

关于bash - Shell 脚本在 bash 中有效,但在 ksh 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7687290/

相关文章:

c - 在 Korn shell 脚本中将斜杠替换为单词 'slash'

java - Process.destroy() 不会杀死 Process 的子进程

arrays - 在 Bash 中访问 JSON 对象 - 关联数组/列表/另一个模型

regex - 当模式(不)包含带括号的组时,为什么 expr match 输出不同的东西?

bash/sh 嵌套转义序列

android - Adb shell 提供 root 访问权限而不是 shell 访问权限

linux - 将 vimrc 转换为 Windows

linux - 自定义脚本 echo -e 参数

linux - bash 中的备份脚本

linux - MQ ksh shell 脚本 - 陷阱错误