Get exit code for command in bash/ksh

前端 未结 5 944
死守一世寂寞
死守一世寂寞 2021-02-01 01:08

I want to write code like this:

command=\"some command\"

safeRunCommand $command

safeRunCommand() {
   cmnd=$1

   $($cmnd)

   if [ $? != 0 ]         


        
5条回答
  •  离开以前
    2021-02-01 01:47

    Try

    safeRunCommand() {
       "$@"
    
       if [ $? != 0 ]; then
          printf "Error when executing command: '$1'"
          exit $ERROR_CODE
       fi
    }
    

提交回复
热议问题