shell dot on steroids https://pagure.io/shellfu

isa.sh 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/sh
  2. #
  3. # Validation helpers
  4. #
  5. # This module provides several basic validation functions
  6. # to help make code more readable and avoid having to implement
  7. # them (some may be more tricky than they look like).
  8. #
  9. isa__bool() {
  10. #
  11. # True if $1 is a boolean
  12. #
  13. case $1 in
  14. true) return 0 ;;
  15. false) return 0 ;;
  16. *) return 1 ;;
  17. esac
  18. }
  19. isa__false() {
  20. #
  21. # True if $1 is a False boolean
  22. #
  23. test "$1" == false
  24. }
  25. isa__int() {
  26. #
  27. # True if $1 is an integer
  28. #
  29. # Note that $1 does not have to be decimal; in POSIX shells,
  30. # eg. 0xf1 is a valid hexadecimal integer and 0755 is a valid
  31. # octal integer.
  32. #
  33. {
  34. test -z "$1" && return 1
  35. test "$1" -ge 0 && return 0
  36. return 1
  37. } 2>/dev/null
  38. }
  39. isa__name() {
  40. #
  41. # True if $1 is a name in most languages
  42. #
  43. echo "$1" | grep -qx '[[:alpha:]_][[:alnum:]_]*'
  44. }
  45. isa__posint() {
  46. #
  47. # True if $1 is a positive integer
  48. #
  49. {
  50. test -z "$1" && return 1
  51. test "$1" -ge 0 && return 0
  52. return 1
  53. } 2>/dev/null
  54. }
  55. isa__true() {
  56. #
  57. # True if $1 is a True boolean
  58. #
  59. test "$1" == true
  60. }