cmdlib.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/sh
  2. #
  3. # Command library that provides a boilerplate set of functions and variables
  4. # required for all bourne shell based scripts.
  5. #
  6. # Copyright (C) 2009, Cisco Systems Inc.
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. # Ngie Cooper, August 2009
  23. #
  24. set -u
  25. export SHELL_DEBUG=${SHELL_DEBUG:=0}
  26. if [ "x$SHELL_DEBUG" = x1 ] ; then
  27. set -x
  28. fi
  29. #=============================================================================
  30. # FUNCTION: tst_cleanup
  31. # PURPOSE: Clean up after a testcase.
  32. #=============================================================================
  33. tst_cleanup()
  34. {
  35. # Disable the trap EXIT handler.
  36. trap '' EXIT
  37. # To ensure set -u passes...
  38. TCtmp=${TCtmp:=}
  39. tst_resm TINFO "Cleaning up."
  40. # Nuke the testcase temporary directory if it exists.
  41. [ -d "$TCtmp" ] && rm -rf "$TCtmp"
  42. }
  43. #=============================================================================
  44. # FUNCTION: setup
  45. # PURPOSE: Setup the test environment.
  46. #=============================================================================
  47. tst_setup()
  48. {
  49. TST_COUNT=1
  50. TST_TOTAL=${TST_TOTAL:=1}
  51. export TCID TST_COUNT TST_TOTAL
  52. for varname in TST_TOTAL; do
  53. if eval "test -z \"\$${varname}\""; then
  54. end_testcase "You must set ${varname} before calling setup()."
  55. fi
  56. done
  57. LTPROOT=${LTPROOT:="../../../../"}
  58. TEMPDIR=${TEMPDIR:=/tmp}
  59. TCtmp=${TCtmp:=$TEMPDIR/$TC$$}
  60. # User wants a temporary sandbox to play with.
  61. if [ -n "$TCtmp" -a "$TCtmp" != "$TEMPDIR/$$" ] ; then
  62. test -d "$TCtmp" || mkdir -p "$TCtmp"
  63. # Clean up on exit.
  64. trap tst_cleanup EXIT
  65. fi
  66. }
  67. #=============================================================================
  68. # FUNCTION NAME: end_testcase
  69. #
  70. # FUNCTION DESCRIPTION: Print out whether or not a test failed. Do not use
  71. # this when TBROK messages should be applied.
  72. #
  73. # PARAMETERS: Failure message, or "" / unset if passed.
  74. #
  75. # RETURNS: None.
  76. #=============================================================================
  77. end_testcase()
  78. {
  79. if [ $# -eq 0 ]; then
  80. tst_resm TPASS "Test successful"
  81. exit 0
  82. else
  83. tst_resm TFAIL "Test broken: $*"
  84. exit 1
  85. fi
  86. }
  87. #=============================================================================
  88. # FUNCTION: exists
  89. # PURPOSE: Check if command(s) used by this test script exist.
  90. #=============================================================================
  91. exists()
  92. {
  93. for cmd in $*; do
  94. if ! command -v $cmd >/dev/null 2>&1; then
  95. end_testcase "$cmd: command not found"
  96. exit 1
  97. fi
  98. done
  99. }
  100. incr_tst_count()
  101. {
  102. : $(( TST_COUNT += 1 ))
  103. }
  104. tst_require_root()
  105. {
  106. if [ "x$(id -u)" != "x0" ]; then
  107. tst_resm TCONF "You must be root to execute this test"
  108. exit 0
  109. fi
  110. }
  111. #
  112. # $0 is maintained by the caller script; tested on FreeBSD (ash) and Gentoo
  113. # GNU/Linux (bash) -- assuming you aren't calling this from another function
  114. # or command:
  115. #
  116. # foo.sh:
  117. # echo ${0##*/}
  118. # . ${$0%%*}/bar.sh
  119. # bar.sh:
  120. # echo ${0##*/}
  121. # echo $SHELL
  122. #
  123. # Gentoo:
  124. # gcooper@orangebox ~/Desktop $ ./foo.sh
  125. # foo.sh
  126. # foo.sh
  127. # /bin/bash
  128. # gcooper@orangebox ~/Desktop $ uname -sr
  129. # Linux 2.6.29-gentoo-r5
  130. #
  131. # FreeBSD:
  132. # $ ./foo.sh
  133. # foo.sh
  134. # foo.sh
  135. # /bin/sh
  136. # $ uname -sr
  137. # FreeBSD 8.0-BETA2
  138. #
  139. TCID=${TCID:=}
  140. [ -z "$TCID" ] && TCID=${0##*/}
  141. TC=$(echo "$TCID" | awk '{ sub( /[0-9]+$/,""); print; }')
  142. . daemonlib.sh