3

有人可以告诉我为什么第 7 行(if 语句)会产生错误:

test.sh: line 7: [: command_exists: unary operator expected

谢谢!

#!/usr/bin/env bash

command_exists () {
  command -v "$1" &> /dev/null ;
}

if [ ! command_exists ruby ]; then  # test.sh: line 7: [: command_exists: unary operator expected
  echo 'found ruby'
else
  echo 'ruby not found'
fi
4

1 回答 1

5
if ! command_exists ruby; then

在 Bash 中,if执行命令并根据其返回值进行操作。[恰好是一个命令。

于 2012-05-22T00:33:53.840 回答