DEV Community

Roy
Roy

Posted on

bash 函数 return 1 是表示执行失败

#!/bin/bash

function check_k8s() {
    port_6443=`netstat -nlt | grep -w 6443 | wc -l`
    if  [ $port_6443 = 0 ]; then
      #不存在k8s
      return 0
    else
      #存在k8s
      return 1
    fi
}

function return0 (){
  return 0
}

function return1 (){
  return 1
}

if check_k8s; then
   echo 1
else
   echo 0
fi
Enter fullscreen mode Exit fullscreen mode

在if 语句的条件中调用 return 非0的函数,执行失败分支

Top comments (0)