#!/bin/bash
# 定义表名数组
tables=("表名1" "表名2" "表名3" "表名4" "表名5")
months=("01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" )
# 循环统计每个表每个月占用空间大小
for table in ${tables[@]}
do
echo $table >> output.txt
for month in ${months[@]}
do
echo -n "2022${month}* " >> output.txt
hadoop fs -du -s "/user/hive/warehouse/$table/city=*/time=2022${month}*" | awk '{print $2}' | paste -s -d"+" - | bc >> output.txt
done
echo -n "202301* " >> output.txt
hadoop fs -du -s "/user/hive/warehouse/cell.db/$table/city=*/time=202301*" | awk '{print $2}' | paste -s -d"+" - | bc >> output.txt
echo -n "202302* " >> output.txt
hadoop fs -du -s "/user/hive/warehouse/cell.db/$table/city=*/time=202302*" | awk '{print $2}' | paste -s -d"+" - | bc >> output.txt
echo -n "202303* " >> output.txt
hadoop fs -du -s "/user/hive/warehouse/cell.db/$table/city=*/time=202303*" | awk '{print $2}' | paste -s -d"+" - | bc >> output.txt
echo -n "202304* " >> output.txt
hadoop fs -du -s "/user/hive/warehouse/cell.db/$table/city=*/time=202304*" | awk '{print $2}' | paste -s -d"+" - | bc >> output.txt
echo -n "202305* " >> output.txt
hadoop fs -du -s "/user/hive/warehouse/cell.db/$table/city=*/time=202305*" | awk '{print $2}' | paste -s -d"+" - | bc >> output.txt
done
- 数组tables 和 months 的定义
- for in 这种循环结构的使用(可以有别的表达形式)
- echo 的 -n 表示不换行输出
- >> 表示追加到文件(>是覆盖)
- 值得注意的地方在于:一开始使用 sh 文件名.sh 执行文件总显示syntax报错,这是因为在系统中sh被重定向(重命名)了,ls -l /bin/*sh 就可以看到具体的重定向情况,可以将sh 改用 bash或者别的方式执行。