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  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM