qbashlib.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/bash
  2. # v.0.0.0.2
  3. # 23.4.2024
  4. # is_number(value)
  5. # checks if value is whole number numeric string
  6. # returns: 0 - not a number
  7. # 1 - is a number
  8. is_number() {
  9. local value="$1"
  10. re='^[0-9]+$'
  11. if ! [[ $value =~ $re ]] ; then
  12. return 0
  13. else
  14. return 1
  15. fi
  16. }
  17. # is_email(value)
  18. # checks if value is valid email string
  19. # returns: 0 - not a email
  20. # 1 - is a email
  21. is_email() {
  22. local email="$1"
  23. local regex="^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
  24. if [[ "$email" =~ $regex ]]; then
  25. return 1
  26. else
  27. return 0
  28. fi
  29. }
  30. # argument_exists(argument_value argument_name)
  31. # checks if argument is empty, then print message and exit with code 1
  32. # returns: 0 - not exists
  33. # 1 - exists
  34. argument_exists() {
  35. # Zkontroluj, zda je zadán první argument
  36. if [ -z "$1" ]; then
  37. echo "$2 argument must be filled."
  38. return 0
  39. fi
  40. return 1
  41. }
  42. # dir_get_size(path_to_dir)
  43. # returns directory size if exists, otherwise exit 1
  44. # returns: size - size in bytes
  45. # exit 1 - path_to_dir not exists
  46. dir_get_size(){
  47. # Zkontroluj, zda existuje adresář path_to_dir
  48. local dir="$1"
  49. if [ ! -d $dir ]; then
  50. exit 1
  51. fi
  52. result=$(du -sb $dir | cut -f1)
  53. echo $result
  54. }
  55. # dir_ensure_trailing_slash(path_to_dir)
  56. # returns full path with trailing slash at the end, therefore full path not exists.
  57. # returns: full path wirh trailing slash
  58. dir_ensure_trailing_slash() {
  59. local path="$1"
  60. if [[ ! "$path" == */ ]]; then
  61. path="${path}/"
  62. fi
  63. echo "$path"
  64. }
  65. # dir_get_name(path_to_dir)
  66. # returns directory name from full path, therefore full path not exists.
  67. # returns: name of directory
  68. dir_get_name() {
  69. local full_path=$(dir_ensure_trailing_slash "$1")
  70. local directory_name="${full_path##*/}"
  71. echo "$directory_name"
  72. }
  73. # filename_get_ymwd(prefix extension)
  74. # creates filename that contains <prefix>_<year>_<month>_<week>_<day-of-week>.<extension>
  75. # returns: filename
  76. filename_get_ymwd(){
  77. # Získání aktuálního data
  78. local current_date=$(date +'%Y-%m-%d') # Například: 2024-04-22
  79. # Získání čísla roku
  80. local year=$(date +'%Y') # Například: 2024
  81. # Získání čísla měsíce
  82. local month=$(date +'%m') # Například: 04
  83. # Získání čísla týdne v roce
  84. local week_number=$(date +'%U') # Například: 16
  85. # Získání čísla dne v týdnu (0 = neděle, 1 = pondělí, ..., 6 = sobota)
  86. local day_of_week=$(date +'%w') # Například: 6 (sobota)
  87. # Sestavení jména souboru
  88. local file_name="$1_${year}_${month}_${week_number}_${day_of_week}.$2"
  89. echo $file_name
  90. }
  91. # mail_send(recipient subject body is-test)
  92. # sends text email with subject and body to recipient via sendmail
  93. # if is-test="true" then just print message if set to "false" or argument is missing send via sendmail
  94. mail_send(){
  95. local recipient=$1
  96. local subject="$2"
  97. local body="$3"
  98. local is-test=$4
  99. is_email $recipient
  100. if [ $? -eq 0 ]; then
  101. echo "<recipient> argument '$recipient' is not valid email address."
  102. exit 1
  103. fi
  104. argument_exists "$subject" "<subject>"
  105. echo "$body" | mail -s "$subject" $recipient
  106. }