Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bash脚本,调用函数作为条件测试_Bash_Scripting - Fatal编程技术网

bash脚本,调用函数作为条件测试

bash脚本,调用函数作为条件测试,bash,scripting,Bash,Scripting,有人能告诉我为什么第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: una

有人能告诉我为什么第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
在Bash中,
if
执行命令并对其返回值进行操作<代码>[恰好是一个命令

if ! command_exists ruby; then