昨天写了一个脚本,只是做简单的表插入和表删除,写shell脚本的时候,死死的卡在shell读文件内容这块,所以写出来跟大家分享下。
num=`cat ptest.txt|wc -l` echo $num if [ $num -gt 0 ];then while read feemobile do echo "insert into mms_subscribe_pause (packageid,businessid,packageidsmc,sendnum,recnum,booktime,subscribetime,subscribetype,subscribeip,mobiletypeid,suspendtime,suspendtyp e,suspendip,suspendname,suspendreason,pidsmt,bidsmt,smcstatus,pidmianfei,bidmianfei,gateway,area,usertype,leagueid,status,Pcode,FeeCode,contenttype,isrelated) select packageid,b usinessid,packageidsmc,sendnum,recnum,booktime,subscribetime,subscribetype,subscribeip,mobiletypeid,now(),'42','MISC_PAUSE','','',pidsmt,bidsmt,smcstatus,pidmianfei,bidmianfei,g ateway,area,usertype,leagueid,status,Pcode,FeeCode,contenttype,isrelated from mms_subscribe where recnum='$feemobile';" |$mmsdb echo "delete from mms_subscribe where recnum='$feemobile';" |$mmsdb done < ptest.txt fi
看,是不是很简单,问题来了,我是逐行逐行读取ptest.txt里的内容,那它怎么可能自动实现读取一行行文件内容的呢?done < ptest.txt起的作用就是把待读取的文件内容准备好。那逐行读取文件写法只有这一种吗?答案是否定的。
写法一:
#!/bin/bash while read line do echo $line done < file(待读取的文件)
写法二:
#!/bin/bash cat file(待读取的文件) | while read line do echo $line done
写法三:
for line in `cat file(待读取的文件)` do echo $line done
以上三种写法都是我摘自网上的,共同分享一下。
花花~ 😆 我头疼