shell中for循环,读取一整行

摘自:https://blog.csdn.net/peterxiaoq/article/details/77247547

shell中for循环的默认分隔符是:空格、tab、\n

需求是只以\n作为分隔符

shell for循环以\n作为分割符,

方式一:

文件aa.sh

#!/bin/bash  
  
  
IFS=$'\n\n'  
  
for i in `cat 1.txt`;  
do  
    echo "begin"  
    echo $i  
    echo "end"  
done  

 

运行方式也要注意:./aa.sh 或  bash aa.sh

不要使用sh aa.sh, 为啥?因为无效!


方式二:

#!/bin/bash  
  
while read i;    
do  
    echo "begin"  
  echo $i  
   a=`echo $i | cut -f 1 -d " "`  
   echo $a  
   b=`echo $i | cut -f 2 -d " "`  
   echo $b  
   echo "end"  
done<1.txt  

 

posted @ 2020-03-26 21:06  LiuYanYGZ  阅读(2492)  评论(0编辑  收藏  举报