日期:2014-05-16  浏览次数:20677 次

passwd 文件行读取
本帖最后由 julius_lee 于 2012-11-22 16:01:02 编辑
请问passwd文件中如下面的行:
hplip:x:105:7:HPLIP system user,,,:/var/run/hplip:/bin/false
polkituser:x:108:115:PolicyKit,,,:/var/run/PolicyKit:/bin/false
ftp:x:110:120:ftp daemon,,,:/srv/ftp:/bin/false
红色处标记的两个字符串,它们间用空格隔开
如果我想用for line读取该文件会因为这个空格产生问题,比如:
  1 #!/bin/bash
  2 file=pass
  3 num=0
  4 for line in `cat $file`
  5 do
  6         echo "the $num line is $line"
  7         num=$(($num+1))
  8 done    

yee@Loong:~/shell$ sh b.sh 
the 0 line is Debian-exim:x:104:110::/var/spool/exim4:/bin/false
the 1 line is hplip:x:105:7:HPLIP
the 2 line is system
the 3 line is user,,,:/var/run/hplip:/bin/false
the 4 line is polkituser:x:108:115:PolicyKit,,,:/var/run/PolicyKit:/bin/false
the 5 line is ftp:x:110:120:ftp
the 6 line is daemon,,,:/srv/ftp:/bin/false

因为该行有空格,有空格就当做另一行的开始,如果我想读取整行如何处理呢?(不想先除去空格,生成中间文件,想一步读取。)
------解决方案--------------------
while read line
do 
   echo $line
done < filename