【1.7】shell读取文件,逐行读取
【1】while 读取实践
直接上案例:
while read line do table_name=`echo $line | awk '{print $1}'` echo $table_name echo "Start running SQL script for DB "$table_name" at "$(date +%Y-%m-%d%t%A%t%T) >> $logfile mysql -pbfgame20 <<EOF use log_db; alter table log_db.$table_name drop partition p$PARTNAME4,p99; alter table log_db.$table_name add partition(partition p$NOWNAME VALUES LESS THAN (to_days('$NOW_MONTHS')) ENGINE = InnoDB); alter table log_db.$table_name add partition(partition p99 VALUES LESS THAN MAXVALUE ENGINE = InnoDB); EOF done<$FILE1
【2】不同方式读文件
(2.1)for循环读取列表
list="a b c"
for i in ${list[*]}
do
echo $i
done
(2.2)for循环读取文件
for line in `cat filename`
do
echo ${line}
done
(2.3)while循环读取文件
# 第一种
while read line
do
echo $line
done < filename
# 第二种
cat filename | while read line
do
echo $line
done