v_list_db_base 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. # info: list database
  3. # options: user database [format]
  4. #
  5. # The function for obtaining of all database's parameters.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument defenition
  10. user=$1
  11. database=$2
  12. format=${3-shell}
  13. # Importing variables
  14. source $VESTA/conf/vars.conf
  15. source $V_FUNC/shared.func
  16. source $V_FUNC/db.func
  17. # Json function
  18. json_list_db() {
  19. i=1
  20. # Define words number
  21. last_word=$(echo "$fields" | wc -w)
  22. # Reading file line by line
  23. line=$(grep "DB='$database'" $conf)
  24. # Print top bracket
  25. echo '{'
  26. # Parsing key=value
  27. eval $line
  28. # Starting output loop
  29. for field in $fields; do
  30. # Parsing key=value
  31. eval value=$field
  32. # Checking first field
  33. if [ "$i" -eq 1 ]; then
  34. echo -e "\t\"$value\": {"
  35. else
  36. if [ "$last_word" -eq "$i" ]; then
  37. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  38. else
  39. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  40. fi
  41. fi
  42. # Updating iterator
  43. (( ++i))
  44. done
  45. # If there was any output
  46. if [ -n "$value" ]; then
  47. echo -e "\t}"
  48. fi
  49. # Printing bottom json bracket
  50. echo -e '}'
  51. }
  52. # Shell list for single database
  53. shell_list_db() {
  54. # Reading file line by line
  55. line=$(grep "DB='$database'" $conf)
  56. # Parsing key=value
  57. eval $line
  58. # Print result line
  59. for field in $fields; do
  60. eval key="$field"
  61. echo "${field//$/}: $key "
  62. done
  63. }
  64. #----------------------------------------------------------#
  65. # Verifications #
  66. #----------------------------------------------------------#
  67. # Checking args
  68. check_args '2' "$#" 'user db_name [format]'
  69. # Checking argument format
  70. format_validation 'user' 'database'
  71. # Checking user
  72. is_user_valid
  73. # Checking database exist
  74. is_db_valid
  75. #----------------------------------------------------------#
  76. # Action #
  77. #----------------------------------------------------------#
  78. # Defining config
  79. conf=$V_USERS/$user/db.conf
  80. # Defining fileds to select
  81. fields='$DB $USER $HOST $TYPE $CHARSET $U_DISK $SUSPEND $DATE'
  82. # Listing database
  83. case $format in
  84. json) json_list_db ;;
  85. plain) shell_list_db ;;
  86. shell) shell_list_db | column -t ;;
  87. *) check_args '2' '0' 'user database [format]'
  88. esac
  89. #----------------------------------------------------------#
  90. # Vesta #
  91. #----------------------------------------------------------#
  92. exit