librunscript.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # librunscript.sh is a library of shell script functions that can be used in
  2. # NEMO run scripts.
  3. #
  4. # Usage: source ./librunscript.sh
  5. # Function info writes information to standard out.
  6. #
  7. # Usage: info MESSAGE ...
  8. #
  9. function info()
  10. {
  11. echo "*II* $@"
  12. }
  13. # Function error writes information to standard out and exits the script with
  14. # error code 1.
  15. #
  16. # Usage: error MESSAGE ...
  17. #
  18. function error()
  19. {
  20. echo "*EE* $@"
  21. exit 1
  22. }
  23. # Function cleanup is called automatically by trapping signals. It is supposed
  24. # to clean up after interruptions.
  25. function cleanup()
  26. {
  27. [[ -n "${tempfile:-}" ]] && if [ -r ${tempfile} ]
  28. then
  29. rm -f ${tempfile}
  30. fi
  31. }
  32. trap 'cleanup' EXIT SIGHUP SIGINT SIGTERM
  33. # Function has_config checks it's arguments for matches in the $config variable
  34. # and returns true (0) or false (1) accordingly. Optionally, the first argument
  35. # can be either "all" (all arguments must match) or "any" (at least one
  36. # argument must match). If the first argument is neither "all" nor "any", the
  37. # function behaves like "all" was given as the first argument.
  38. #
  39. # Usage: has_config [all|any] ARGS ...
  40. #
  41. # Syntax rules:
  42. #
  43. # The $config variable takes a list of names (typically software components),
  44. # separated by white spaces:
  45. #
  46. # config="foo bar baz" # Specifies three components: 'foo', 'bar', and 'baz'
  47. #
  48. # It is possible to add comma-separated lists of options to components. The
  49. # list is separated from the component by a colon:
  50. #
  51. # config="foo bar:qux,fred baz:plugh" # Adds the options 'qux' and 'fred' to
  52. # # component 'bar' as well as option
  53. # # 'plugh' to component 'baz'
  54. #
  55. # When using the has_config function to check the $config variable, it is
  56. # important to list every component-option pair separately. To check for both
  57. # the 'qux' and 'fred' options of component 'bar' in the above example, use:
  58. #
  59. # has_config bar:qux bar:fred && echo "Got it!"
  60. #
  61. function has_config()
  62. {
  63. # If called without arguments, return false
  64. (( $# )) || return 1
  65. # If $config unset or empty, return false
  66. [[ -z "${config:-}" ]] && return 1
  67. local __c
  68. local __m
  69. # If first argument is "any" then only one of the arguments needs to match
  70. # to return true. Return false otherwise
  71. if [ "$1" == "any" ]
  72. then
  73. shift
  74. for __c in "$@"
  75. do
  76. for __m in $config
  77. do
  78. [[ "$__m" =~ "${__c%:*}" ]] && [[ "$__m" =~ "${__c#*:}" ]] && return 0
  79. done
  80. done
  81. return 1
  82. fi
  83. # If first argument is "all", or neither "any" nor "all", all arguments
  84. # must match to return true. Return false otherwise.
  85. [[ "$1" == "all" ]] && shift
  86. local __f
  87. for __c in "$@"
  88. do
  89. __f=0
  90. for __m in $config
  91. do
  92. [[ "$__m" =~ "${__c%:*}" ]] && [[ "$__m" =~ "${__c#*:}" ]] && __f=1
  93. done
  94. (( __f )) || return 1
  95. done
  96. return 0
  97. }
  98. # Function leap days calculates the number of leap days (29th of Februrary) in
  99. # a time intervall between two dates.
  100. #
  101. # Usage leap_days START_DATE END_DATE
  102. function leap_days()
  103. {
  104. local ld=0
  105. local frstYYYY=$(date -ud "$1" +%Y)
  106. local lastYYYY=$(date -ud "$2" +%Y)
  107. set +e
  108. # Check first year for leap day between start and end date
  109. $(date -ud "${frstYYYY}-02-29" > /dev/null 2>&1) \
  110. && (( $(date -ud "$1" +%s) < $(date -ud "${frstYYYY}-03-01" +%s) )) \
  111. && (( $(date -ud "$2" +%s) > $(date -ud "${lastYYYY}-02-28" +%s) )) \
  112. && (( ld++ ))
  113. # Check intermediate years for leap day
  114. for (( y=(( ${frstYYYY}+1 )); y<=(( ${lastYYYY}-1 )); y++ ))
  115. do
  116. $(date -ud "$y-02-29" > /dev/null 2>&1) && (( ld++ ))
  117. done
  118. # Check last year (if different from first year) for leap day between start
  119. # and end date
  120. (( $lastYYYY > $frstYYYY )) \
  121. && $(date -ud "${lastYYYY}-02-29" > /dev/null 2>&1) \
  122. && (( $(date -ud "$1" +%s) < $(date -ud "${frstYYYY}-03-01" +%s) )) \
  123. && (( $(date -ud "$2" +%s) > $(date -ud "${lastYYYY}-02-28" +%s) )) \
  124. && (( ld++ ))
  125. set -e
  126. echo "$ld"
  127. }