Is there a way to write a bash function which aborts the whole execution, no matter how it is called?

前端 未结 5 762
悲&欢浪女
悲&欢浪女 2020-11-29 18:19

I was using \"exit 1\" statement in my bash functions to terminate the whole script and it worked fine:

function func()
{
   echo \"Goodbye\"
   exit 1
}
ech         


        
5条回答
  •  自闭症患者
    2020-11-29 18:57

    I guess better is

    #!/bin/bash
    set -e
    trap "exit 1" ERR
    
    myfunc() {
         set -x # OPTIONAL TO SHOW ERROR
         echo "Exit with failure"
         set +x # OPTIONAL
         exit 1
    }
    echo "BEFORE..."
    myvar="$(myfunc)"
    echo "AFTER..But not shown"
    

提交回复
热议问题