互動式指令碼

小亮520cl發表於2015-11-18

  1. [root@localhost python]# more goo.sh
  2. #!/bin/bash
  3. # author:Jonnychen
  4. # date:2014-7-19
  5. # set -x

  6. # get the key value of input
  7. get_key_value()
  8. {
  9.         echo "$1" | sed 's/^--[A-Za-z_-]*=//'
  10. }


  11. use_help()    ----用法
  12. {
  13. cat << EOF
  14.         Info :
  15.                 Author:Michael.xu
  16.         Usage :
  17.         Usage : $0 [configure-options]
  18.                 -? , --help
  19.                 --user=<> , set login user
  20.                 --password=<> , set login user password
  21.                 --port=<>,default 3306
  22.                 --host=<>,default 127.0.0.1
  23.                 -innodb , include (com_select,com_insert,com_update,com_delete)
  24.                 -innodb_buffer_hit , innodb buffer pool hits
  25. EOF
  26. }


  1. parse_options()
  2. {
  3.         while [ $# -gt 0 ]
  4.         do
  5.                 case "$1" in
  6.                         --user=*)
  7.                                 LOGIN_USER=`get_key_value "$1"`;;
  8.                         --password=*)
  9.                                 LOGIN_PASSWORD=`get_key_value "$1"`;;
  10.                         --port=*)
  11.                                 LOGIN_PORT=`get_key_value "$1"`;;
  12.                         --host=*)
  13.                                 LOGIN_HOST=`get_key_value "$1"`;;
  14.                         -? | --help)
  15.                                 use_help
  16.                                 exit 0;;
  17.                         *)
  18.                                 echo "unknown configure option '$1'"
  19.                                 exit 1;;
  20.                 esac
  21.                 shift
  22.         done
  23. }

LOGIN_USER=root         ---預設值
LOGIN_PORT=3306         ---預設值
LOGIN_HOST=127.0.0.1    ---預設值




  1. parse_options "$@"
  echo "$LOGIN_USER $LOGIN_PASSWORD $LOGIN_PORT $LOGIN_HOST"
  1. if [ ! -n "$LOGIN_USER" ];then    
  2.      use_help
  3.      exit
  4. fi


  5. if [ ! -n "$LOGIN_PASSWORD" ];then
  6.      use_help
  7.      exit
  8. fi




  9. mysql -u$LOGIN_USER -p$LOGIN_PASSWORD -P$LOGIN_PORT -h$LOGIN_HOST -e "select now()"

使用辦法
[root@localhost python]# sh  goo.sh  --password=ESBecs00   
root ESBecs00 3306 127.0.0.1
+---------------------+
| now()               |
+---------------------+
| 2015-11-18 18:29:52 |
+---------------------+

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-1840266/,如需轉載,請註明出處,否則將追究法律責任。

相關文章