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)

Make Your Github Profile Stand Out

Github is great, but have you considered how to make yours more attractive for potential employers or other visitors? Even non-tech ones like recruiters!

Take a couple of hours and show your best side as a person - and a programmer.