星期二, 二月 27, 2007

bash text line by line process

如下的一个文件:
sh$ head sites.txte.72h.net /home/httpd/72h.net/e.72h.net
syssite /home/httpd/72h.net/ec.72h.net/syssite
mall.72h.net /home/httpd/72h.net/mall.72h.net
shop.72h.net /home/httpd/72h.net/shop.72h.net
aws /home/httpd/aws
ftputil /home/httpd/ftputil
is-me.com /home/httpd/is-me.com
mail.phpedu.org /home/httpd/mail.phpedu.org
payex /home/httpd/payex
phpadm /home/httpd/phpadm
现在需要对每一行进行一个代码生成,例如生成:
fs_backup -a t:$2 $1
# $2--path, $1--identity
如果使用 bash 来处理,那么需要保证每一行都能够正确的被分离,使用下面的方法:
sh$ for line in "`cat sites.txt`"; do echo $line; done
e.72h.net /home/httpd/72h.net/e.72h.net syssite /home/httpd/72h.net/ec.72h.net/syssite mall.72h.net /home/httpd/72h.net/mall.72h.net shop.72h.net /home/httpd/72h.net/shop.72h.net aws /home/httpd/aws ftputil /home/httpd/ftputil is-me.com /home/httpd/is-me.com mail.phpedu.org /home/httpd/mail.phpedu.org payex /home/httpd/payex phpadm /home/httpd/phpadm phpedu.org /home/httpd/phpedu.org 46.shopex.com.cn /home/httpd/shopex/46.shopex.com.cn 99bill.shopex.com.cn /home/httpd/shopex/99bill.shopex.com.cn acco.shopex.com.cn /home/httpd/shopex/acco.shopex.com.cn alipay.shopex.com.cn /home/httpd/shopex/alipay.shopex.com.cn book.shopex.cn /home/httpd/shopex/book.shopex.cn card.shopex.cn /home/httpd/shopex/card.shopex.cn cd.shopex.cn /home/httpd/shopex/cd.shopex.cn clothes.shopex.com.cn /home/httpd/shopex/clothes.shopex.com.cn com.shopex.com.cn /home/httpd/shopex/com.shopex.com.cn digi.shopex.com.cn /home/httpd/shopex/digi.shopex.com.cn free.shopex.com.cn /home/httpd/shopex/free.shopex.com.cn game.shopex.com.cn /home/httpd/shopex/game.shopex.com.cn gift.shopex.com.cn /home/httpd/shopex/gift.shopex.com.cn health.shopex.cn /home/httpd/shopex/health.shopex.cn home.shopex.cn /home/httpd/shopex/home.shopex.cn magicisland.shopex.com.cn /home/httpd/shopex/magicisland.shopex.com.cn makeup.shopex.cn /home/httpd/shopex/makeup.shopex.cn mall.shopex.com.cn /home/httpd/shopex/mall.shopex.com.cn mobile.shopex.cn /home/httpd/shopex/mobile.shopex.cn paypal.shopex.com.cn /home/httpd/shopex/paypal.shopex.com.cn pc.shopex.cn /home/httpd/shopex/pc.shopex.cn pifa.shopex.com.cn /home/httpd/shopex/pifa.shopex.com.cn syssite /home/httpd/shopex/platform.shopex.com.cn/syssite shopex.cn /home/httpd/shopex/shopex.cn blog /home/httpd/shopex/shopex.cn/blog help /home/httpd/shopex/shopex.cn/help store.shopex.cn /home/httpd/shopex/store.shopex.cn tex.shopex.com.cn /home/httpd/shopex/tex.shopex.com.cn top.shopex.cn /home/httpd/shopex/top.shopex.cn update.shopex.com.cn /home/httpd/shopex/update.shopex.com.cn store.verycd.com /home/httpd/store.verycd.com zovamailredir /home/httpd/zovamailredir zovatech /home/httpd/zovatech

sh$ for line in `cat sites.txt`; do echo $line; done
e.72h.net
/home/httpd/72h.net/e.72h.net
syssite
/home/httpd/72h.net/ec.72h.net/syssite
mall.72h.net
/home/httpd/72h.net/mall.72h.net
shop.72h.net
/home/httpd/72h.net/shop.72h.net
aws
/home/httpd/aws
......
都不是想要的结果!

只能使用下面的形式了:
sh$cat sites.txt | while read id path; do echo $id, $path; done
e.72h.net, /home/httpd/72h.net/e.72h.net
syssite, /home/httpd/72h.net/ec.72h.net/syssite
mall.72h.net, /home/httpd/72h.net/mall.72h.net
shop.72h.net, /home/httpd/72h.net/shop.72h.net
aws, /home/httpd/aws
ftputil, /home/httpd/ftputil
is-me.com, /home/httpd/is-me.com
mail.phpedu.org, /home/httpd/mail.phpedu.org
payex, /home/httpd/payex
phpadm, /home/httpd/phpadm
......
所以最终的命令如下:
sh$ cat sites.txt | while read id path; do fs_backup -a t:$path $id; done

没有评论: