Guest User

Untitled

a guest
Apr 7th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. usage() {
  4. echo "\
  5. Usage: $0 -u username -d database [-h host] [-o port)] \\
  6. [[-p] | [-P password]]
  7. host defaults to localhost
  8. port defaults to 3306
  9. -p will cause mysql to prompt for the password, good
  10. -P password will show the password in your ps list, evil" >&2
  11. exit 1
  12. }
  13.  
  14. username=
  15. passwdprompt=no
  16. host=localhost
  17. port=3306
  18. database=
  19. password=
  20.  
  21. while getopts ":u:pPd:h:" name; do
  22. case $name in
  23. u) username=$OPTARG;;
  24. p) passwdprompt="yes";;
  25. P) password=$OPTARG;;
  26. d) database=$OPTARG;;
  27. h) host=$OPTARG;;
  28. ?) usage;;
  29. esac
  30. done
  31.  
  32. if [ -z "${username}" ] || [ -z "${database}" ]; then
  33. usage
  34. fi
  35.  
  36. basecmd="mysql -u ${username} -D ${database} -h ${host} -P ${port}"
  37.  
  38. if [ "${passwdprompt}" = "yes" ]; then
  39. basecmd="${basecmd} -p"
  40. elif [ -n "${password}" ]; then
  41. basecmd="${basecmd} -p${password}"
  42. fi
  43.  
  44. tables=$(${basecmd} -e "SHOW TABLES;" | grep -v "+--" | grep -v "Tables_in_${dbname}")
  45. if [ $? -ne 0 ]; then
  46. echo "Unable to retrieve the table names." >&2
  47. exit 1
  48. fi
  49.  
  50. cmd=""
  51.  
  52. for table in ${tables}; do
  53. cmd="${cmd} TRUNCATE ${table};"
  54. done
  55.  
  56. $(${basecmd} -e "${cmd}")
Add Comment
Please, Sign In to add comment