v_list_db_base 2.6 KB

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