常用 功能性 shell function合集

2017年3月15日 0 条评论 1.5k 次阅读 0 人点赞
function txt_replace() {
#替换指定目录下文本文件中指定内容,比如搜索当前所有txt,替换内容a为b
case "$1" in
 "-h")
echo "txt_replace [directory] aa bb\n"
echo "replace aa to bb in directory ."
 ;;
 *)
if [ -d $1 ]; then
 for val in `find $1 |xargs grep -s $2 |awk -F: '{print $1}'` ; do
  sed -i "s/$2/$3/g" $val
 done
fi
 ;;
esac
}

function txt_find() {
#查找文档中的内容
#txt_find 路径 内容
find $1 | xargs grep -s $2;
}

#!/bin/bash
#打印指定数据库表的大小,支持多个表大小打印
db_host="localhost"
db_name="db_name"
db_root="root"
db_password="password"
tables_name="tables1 tables2 tables3"
for val in $tables_name
do
printf ${val} | awk '{printf "%-20s",$1}'
mysql -h $db_host -u$db_root -p$db_password -D information_schema -e "select concat(round(sum(DATA_LENGTH/1024/1024), 2), 'MB')  as data from TABLES where table_schema='$db_name' and table_name='$val'" | grep -v data |awk '{printf "%-20s %10s\n",'$val',$1}'
done
#运行效果如下
#tables1                                   0.02MB
#tables2                                   0.45MB
#tables3                                  43.58MB

Sevenfal

这个人太懒什么东西都没留下

文章评论(0)