assert.sh

来自「一本完整的描述Unix Shell 编程的工具书的所有范例」· Shell 代码 · 共 46 行

SH
46
字号
#!/bin/bash# assert.shassert ()                 #  If condition false,{                         #+ exit from script with error message.  E_PARAM_ERR=98  E_ASSERT_FAILED=99  if [ -z "$2" ]          # Not enough parameters passed.  then    return $E_PARAM_ERR   # No damage done.  fi  lineno=$2  if [ ! $1 ]   then    echo "Assertion failed:  \"$1\""    echo "File \"$0\", line $lineno"    exit $E_ASSERT_FAILED  # else  #   return  #   and continue executing script.  fi  }    a=5b=4condition="$a -lt $b"     # Error message and exit from script.                          #  Try setting "condition" to something else,                          #+ and see what happens.assert "$condition" $LINENO# The remainder of the script executes only if the "assert" does not fail.# Some commands.# ...echo "This statement echoes only if the \"assert\" does not fail."# ...# Some more commands.exit 0

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?