-
Notifications
You must be signed in to change notification settings - Fork 0
/
Show_Table_Inf.sh
executable file
·121 lines (113 loc) · 2.75 KB
/
Show_Table_Inf.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
#ユーザー新規作成・変更
Change(){
local file=".my.cnf"
local username
local password
local hostname
echo "ユーザーを新規作成・変更"
read -p "ユーザー名 : " username </dev/tty
read -s -p "パスワード : " password </dev/tty
echo
read -p "ホスト名 : " hostname </dev/tty
#ファイル内にかきこみ
{ echo "[client]";
echo "user = $username";
echo "password = $password";
echo "host = $hostname";
} > $file
echo "新規作成・変更完了"
}
#ログインユーザー選ぶ
Account(){
local file=".my.cnf"
local login_user
if [ ! -e $file ]
then
Change
fi
echo "/* ログインユーザー */"
cat $file | grep 'user = ' | awk '{print $3}'
read -p"ユーザー名を指定[変更:change] : " login_user </dev/tty
if [ "$login_user" = "change" ]
then
Change
Account
else
USER="$login_user"
fi
}
#MYSQL接続テスト
Connect(){
local file=".my.cnf"
local ret
ret=`mysql --defaults-extra-file=./$file -u $USER -e"select user();"`
if [ $? -eq 1 ]
then
echo "MYSQLに接続できませんでした。"
exit 1
else
echo "MYSQLに接続できました"
local user_list=`echo $ret | awk '{ print $2; }'`
echo "$user_listでログインしました"
fi
}
#所有のDBを返す
Show_DB(){
local file=".my.cnf"
local ret
ret=`mysql --defaults-extra-file=./$file -u $USER -e "show databases;"`
if [ $? -gt 0 ]
then
exit 0
fi
echo "/* データベース一覧 */"
ARRAY=($ret)
Show_array
}
#所有のテーブルを配列に格納しShow_arrayを呼ぶ(引数:データベース名)
Show_Table(){
local file=".my.cnf"
local ret
ret=`mysql --defaults-extra-file=./$file -u $USER -e "use $1;show tables;"`
if [ $? -gt 0 ]
then
echo "存在しないデータベース名です"
echo "最初からやり直してください"
exit 1
fi
ARRAY=($ret)
echo "/* $1内のテーブル一覧 */"
Show_array
}
#配列の中身を縦に表示
Show_array(){
local e
unset ARRAY[0]
for e in "${ARRAY[@]}"
do
echo "*** ${e}"
done
}
#テーブル情報を返す(引数:データベース名 テーブル名)
Show_TableInf(){
local file=".my.cnf"
local ret
ret=`mysql --defaults-extra-file=./$file -u $USER -e "use $1;SHOW COLUMNS FROM $2"`
if [ $? -gt 0 ]
then
echo "存在しないテーブル名です"
echo "最初からやり直してください"
exit 1
fi
echo "/* $2テーブルの列情報 */"
mysql --defaults-extra-file=./$file -u $USER -e "use $1;SHOW COLUMNS FROM $2"
}
echo "テーブルの列情報を表示"
Account
Connect
Show_DB
read -p "データベース名を入力 : " Show_DB </dev/tty
Show_Table $Show_DB
read -p "テーブル名を入力 : " Show_Table </dev/tty
Show_TableInf $Show_DB $Show_Table