经常会遇到这种情况,在一个不能上网的环境通过mysql客户端登录数据库,想执行一个操作,却忘了操作的具体语法,各种不方便。
其实,mysql数据库内置了帮助文档,通过help contents即可查看。
如下所示:
可见,该文档涵盖了数据库操作的大部分主题。
文档分为以上几大类,每个大类又包括更小粒度的类,类最后有具体的topic组成。
那么这之间的层级关系如何呢?
昨天想执行一个操作
mysql> backup table emp to '/tmp/mysqlbackup';
因为这个backup操作以前没有使用过,准备用mysql自带的帮助文档来查看具体的用法。本来这个命令所属的类别就有点模糊,譬如administration和utility都有可能。最后找了一通,还是没有找到。
想了想,这样查看帮助文档确实没有太大的效率,如果知道这之间的层级关系就好办了。
于是,动手写了个脚本,可直观的显示帮助文档中大类与小类,小类与topic之间的关系。
具体如下:
#!/bin/bash#所有操作都是在/tmp/test中操作,因为中间新建了很多临时文件,方便后续的删除。mkdir /tmp/test#获取上图的内容重定向到/tmp/test/test.txt文件中mysql -uroot -p123456 -e help contents > /tmp/test/test.txt#定义输出的格式,\t输出tab,\b相当于backspace一个空格,参考了tree命令的输出方式。format=|\t\b#删除第一行和最后一行sed -i '1d;$d' /tmp/test/test.txtcd /tmp/test#引入number的作用在于后续格式的输出number=0#后续用了递归调用,这里定义的是函数function recursive(){filename=$1number=$[$number+1]while read linedo#name要做为文件名,譬如account management,利用tr函数是去掉字符中间的空格name=`echo $line|tr -d [:blank:]`#输出每个分类中的内容,可能是topic,可能是categoriesmysql -uroot -p123456 -e help $line > $name#取输出文件的第一行的内容,如果是具体的topic,则第一行的内容为:name: 'alter user',不然就还是categories,需要递归调用firstline=`head -1 $name`#整个逻辑比较复杂的有两类,一个是numeric functions,另一个是plugins。先说plugins,一般对于一个具体的topic,它的输出类似于name: 'alter user',而plugins#对应的输出却是name: 'show plugins',所以下面的判断语句多了一个$firstline = name: 'show plugins',针对的即是plugins。另一比较复杂的是numeric functions#它下面的topic有除号“/”,而这基本上是不能作为文件名的。所以在下面的判断逻辑中,如果遇到numeric functions,就直接打印出该类中的topic,而不进行name: 'alter user'这样的判断if [ $firstline = name: '$line' -o $firstline = name: 'show plugins' ];thenfor i in `seq $[$number-1]`do echo -ne $formatdoneecho ├── $lineelse#如果不是topic,则代表是categories,可递归进行判断,唯一的例外就是numeric functions#下面的echo ├── $line打印出的是categories的名字for i in `seq $[$number-1]`do echo -ne $formatdoneecho ├── $line#遇到numeric functions,就直接打印出该类中的topicif [ $line = numeric functions ];thensed -i '1d;$d' $namewhile read functions dofor i in `seq $number`do echo -ne $formatdoneecho ├── $functionsdone < $nameelse#其它的categories,递归调用该函数进行判断。sed -i '1d;$d' $namerecursive $namenumber=$[$number-1]fifidone 1.txt
最后文件中的结果如下:
├── account management| ├── alter user| ├── create user| ├── drop user| ├── grant| ├── rename user| ├── revoke| ├── set password├── administration| ├── binlog| ├── cache index| ├── flush| ├── flush query cache| ├── help command| ├── kill| ├── load index| ├── reset| ├── set| ├── show| ├── show authors| ├── show binary logs| ├── show binlog events| ├── show character set| ├── show collation| ├── show columns| ├── show contributors| ├── show create database| ├── show create event| ├── show create function| ├── show create procedure| ├── show create table| ├── show create trigger| ├── show create view| ├── show databases| ├── show engine| ├── show engines| ├── show errors| ├── show events| ├── show function code| ├── show function status| ├── show grants| ├── show index| ├── show master status| ├── show open tables| ├── show plugins| ├── show privileges| ├── show procedure code| ├── show procedure status| ├── show processlist| ├── show profile| ├── show profiles| ├── show relaylog events| ├── show slave hosts| ├── show slave status| ├── show status| ├── show table status| ├── show tables| ├── show triggers| ├── show variables| ├── show warnings├── compound statements| ├── begin end| ├── case statement| ├── close| ├── declare condition| ├── declare cursor| ├── declare handler| ├── declare variable| ├── fetch| ├── get diagnostics| ├── if statement| ├── iterate| ├── labels| ├── leave| ├── loop| ├── open| ├── repeat loop| ├── resignal| ├── return| ├── signal| ├── while├── data definition| ├── alter database| ├── alter event| ├── alter function| ├── alter logfile group| ├── alter procedure| ├── alter server| ├── alter table| ├── alter tablespace| ├── alter view| ├── constraint| ├── create database| ├── create event| ├── create function| ├── create index| ├── create logfile group| ├── create procedure| ├── create server| ├── create table| ├── create tablespace| ├── create trigger| ├── create view| ├── drop database| ├── drop event| ├── drop function| ├── drop index| ├── drop procedure| ├── drop server| ├── drop table| ├── drop tablespace| ├── drop trigger| ├── drop view| ├── rename table| ├── truncate table├── data manipulation| ├── call| ├── delete| ├── do| ├── dual| ├── handler| ├── insert| ├── insert delayed| ├── insert select| ├── join| ├── load data| ├── load xml| ├── replace| ├── select| ├── union| ├── update├── data types| ├── auto_increment| ├── bigint| ├── binary| ├── bit| ├── blob| ├── blob data type| ├── boolean| ├── char| ├── char byte| ├── date| ├── datetime| ├── dec| ├── decimal| ├── double| ├── double precision| ├── enum| ├── float| ├── int| ├── integer| ├── longblob| ├── longtext| ├── mediumblob| ├── mediumint| ├── mediumtext| ├── set data type| ├── smallint| ├── text| ├── time| ├── timestamp| ├── tinyblob| ├── tinyint| ├── tinytext| ├── varbinary| ├── varchar| ├── year data type├── functions| ├── bit functions| | ├── &| | ├── | | ├── bit_count| | ├── ^| | ├── || | ├── ~| ├── comparison operators| | ├── !=| | ├── =| | ├── between and| | ├── coalesce| | ├── greatest| | ├── in| | ├── interval| | ├── is| | ├── is not| | ├── is not null| | ├── is null| | ├── isnull| | ├── least| | ├── not between| | ├── not in| ├── control flow functions| | ├── case operator| | ├── if function| | ├── ifnull| | ├── nullif| ├── date and time functions| | ├── adddate| | ├── addtime| | ├── convert_tz| | ├── curdate| | ├── current_date| | ├── current_time| | ├── current_timestamp| | ├── curtime| | ├── date function| | ├── datediff| | ├── date_add| | ├── date_format| | ├── date_sub| | ├── day| | ├── dayname| | ├── dayofmonth| | ├── dayofweek| | ├── dayofyear| | ├── extract| | ├── from_days| | ├── from_unixtime| | ├── get_format| | ├── hour| | ├── last_day| | ├── localtime| | ├── localtimestamp| | ├── makedate| | ├── maketime| | ├── microsecond| | ├── minute| | ├── month| | ├── monthname| | ├── now| | ├── period_add| | ├── period_diff| | ├── quarter| | ├── second| | ├── sec_to_time| | ├── str_to_date| | ├── subdate| | ├── subtime| | ├── sysdate| | ├── time function| | ├── timediff| | ├── timestamp function| | ├── timestampadd| | ├── timestampdiff| | ├── time_format| | ├── time_to_sec| | ├── to_days| | ├── to_seconds| | ├── unix_timestamp| | ├── utc_date| | ├── utc_time| | ├── utc_timestamp| | ├── week| | ├── weekday| | ├── weekofyear| | ├── year| | ├── yearweek| ├── encryption functions| | ├── aes_decrypt| | ├── aes_encrypt| | ├── compress| | ├── decode| | ├── des_decrypt| | ├── des_encrypt| | ├── encode| | ├── encrypt| | ├── md5| | ├── old_password| | ├── password| | ├── random_bytes| | ├── sha1| | ├── sha2| | ├── uncompress| | ├── uncompressed_length| | ├── validate_password_strength| ├── information functions| | ├── benchmark| | ├── charset| | ├── coercibility| | ├── collation| | ├── connection_id| | ├── current_user| | ├── database| | ├── found_rows| | ├── last_insert_id| | ├── row_count| | ├── schema| | ├── session_user| | ├── system_user| | ├── user| | ├── version| ├── logical operators| | ├── !| | ├── and| | ├── assign-equal| | ├── assign-value| | ├── or| | ├── xor| ├── miscellaneous functions| | ├── default| | ├── get_lock| | ├── inet6_aton| | ├── inet6_ntoa| | ├── inet_aton| | ├── inet_ntoa| | ├── is_free_lock| | ├── is_ipv4| | ├── is_ipv4_compat| | ├── is_ipv4_mapped| | ├── is_ipv6| | ├── is_used_lock| | ├── master_pos_wait| | ├── name_const| | ├── release_lock| | ├── sleep| | ├── uuid| | ├── uuid_short| | ├── values| ├── numeric functions| | ├── %| | ├── *| | ├── +| | ├── - binary| | ├── - unary| | ├── /| | ├── abs| | ├── acos| | ├── asin| | ├── atan| | ├── atan2| | ├── ceil| | ├── ceiling| | ├── conv| | ├── cos| | ├── cot| | ├── crc32| | ├── degrees| | ├── div| | ├── exp| | ├── floor| | ├── ln| | ├── log| | ├── log10| | ├── log2| | ├── mod| | ├── pi| | ├── pow| | ├── power| | ├── radians| | ├── rand| | ├── round| | ├── sign| | ├── sin| | ├── sqrt| | ├── tan| | ├── truncate| ├── string functions| | ├── ascii| | ├── bin| | ├── binary operator| | ├── bit_length| | ├── cast| | ├── char function| | ├── character_length| | ├── char_length| | ├── concat| | ├── concat_ws| | ├── convert| | ├── elt| | ├── export_set| | ├── extractvalue| | ├── field| | ├── find_in_set| | ├── format| | ├── from_base64()| | ├── hex| | ├── insert function| | ├── instr| | ├── lcase| | ├── left| | ├── length| | ├── like| | ├── load_file| | ├── locate| | ├── lower| | ├── lpad| | ├── ltrim| | ├── make_set| | ├── match against| | ├── mid| | ├── not like| | ├── not regexp| | ├── oct| | ├── octet_length| | ├── ord| | ├── position| | ├── quote| | ├── regexp| | ├── repeat function| | ├── replace function| | ├── reverse| | ├── right| | ├── rpad| | ├── rtrim| | ├── soundex| | ├── sounds like| | ├── space| | ├── strcmp| | ├── substr| | ├── substring| | ├── substring_index| | ├── to_base64()| | ├── trim| | ├── ucase| | ├── unhex| | ├── updatexml| | ├── upper| | ├── weight_string├── functions and modifiers for use with group by| ├── avg| ├── bit_and| ├── bit_or| ├── bit_xor| ├── count| ├── count distinct| ├── group_concat| ├── max| ├── min| ├── std| ├── stddev| ├── stddev_pop| ├── stddev_samp| ├── sum| ├── variance| ├── var_pop| ├── var_samp├── geographic features| ├── geometry| ├── geometry hierarchy| ├── spatial| ├── geometry constructors| | ├── geometrycollection| | ├── linestring| | ├── multilinestring| | ├── multipoint| | ├── multipolygon| | ├── point| | ├── polygon| ├── geometry properties| | ├── dimension| | ├── envelope| | ├── geometrytype| | ├── isempty| | ├── issimple| | ├── srid| | ├── st_dimension| | ├── st_envelope| | ├── st_geometrytype| | ├── st_isempty| | ├── st_issimple| | ├── st_srid| ├── geometry relations| | ├── contains| | ├── crosses| | ├── disjoint| | ├── equals| | ├── intersects| | ├── overlaps| | ├── st_contains| | ├── st_crosses| | ├── st_disjoint| | ├── st_distance| | ├── st_equals| | ├── st_intersects| | ├── st_overlaps| | ├── st_touches| | ├── st_within| | ├── touches| | ├── within| ├── linestring properties| | ├── endpoint| | ├── glength| | ├── isclosed| | ├── numpoints| | ├── pointn| | ├── startpoint| | ├── st_endpoint| | ├── st_isclosed| | ├── st_numpoints| | ├── st_pointn| | ├── st_startpoint| ├── mbr| | ├── asymmetric_decrypt| | ├── asymmetric_derive| | ├── asymmetric_encrypt| | ├── asymmetric_sign| | ├── asymmetric_verify| | ├── create_asymmetric_priv_key| | ├── create_asymmetric_pub_key| | ├── create_dh_parameters| | ├── create_digest| | ├── gtid_subset| | ├── gtid_subtract| | ├── mbr definition| | ├── mbrcontains| | ├── mbrdisjoint| | ├── mbrequal| | ├── mbrintersects| | ├── mbroverlaps| | ├── mbrtouches| | ├── mbrwithin| | ├── sql_thread_wait_after_gtids| | ├── wait_until_sql_thread_after_gtids| ├── point properties| | ├── st_x| | ├── st_y| | ├── x| | ├── y| ├── polygon properties| | ├── area| | ├── centroid| | ├── exteriorring| | ├── interiorringn| | ├── numinteriorrings| | ├── st_area| | ├── st_centroid| | ├── st_exteriorring| | ├── st_interiorringn| | ├── st_numinteriorrings| ├── wkb| | ├── asbinary| | ├── astext| | ├── geomcollfromwkb| | ├── geomfromwkb| | ├── linefromwkb| | ├── mlinefromwkb| | ├── mpointfromwkb| | ├── mpolyfromwkb| | ├── pointfromwkb| | ├── polyfromwkb| | ├── st_asbinary| | ├── st_astext| | ├── st_geomcollfromwkb| | ├── st_geomfromwkb| | ├── st_linefromwkb| | ├── st_pointfromwkb| | ├── st_polyfromwkb| ├── wkt| | ├── geomcollfromtext| | ├── geomfromtext| | ├── linefromtext| | ├── mlinefromtext| | ├── mpointfromtext| | ├── mpolyfromtext| | ├── pointfromtext| | ├── polyfromtext| | ├── st_geomcollfromtext| | ├── st_geomfromtext| | ├── st_linefromtext| | ├── st_pointfromtext| | ├── st_polyfromtext| | ├── wkt definition├── help metadata| ├── help_date| ├── help_version├── language structure├── plugins├── procedures├── storage engines├── table maintenance| ├── analyze table| ├── check table| ├── checksum table| ├── optimize table| ├── repair table├── transactions| ├── change master to| ├── deallocate prepare| ├── execute statement| ├── isolation| ├── lock| ├── prepare| ├── purge binary logs| ├── reset master| ├── reset slave| ├── savepoint| ├── set global sql_slave_skip_counter| ├── set sql_log_bin| ├── start slave| ├── start transaction| ├── stop slave| ├── xa├── user-defined functions| ├── create function udf| ├── drop function udf├── utility| ├── explain| ├── help statement| ├── use
总结:
整个脚本在写的过程有两点比较有意思。
1. 函数递归操作,以前没怎么使用shell进行函数的递归操作。
2. 借鉴tree的输出格式,对结果进行格式化输出。
思路如下:首先定义一个number为0,每次进入一次recursive函数,都会把当前的number加1,如果只是这样的话,那number值将一直增长了,所以在上述脚本else调用recursive函数部分,会在其后执行number=$[$number-1],类似于恢复到上一层目录下。
以上这篇将mysql help contents的内容有层次的输出方法推荐就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。