Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell while read row

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The difference between for i in cat file and while read line

#! / bin/bash#Auth: andy#Date: 20191114#Describe: statistics word_file= ". / cui" # will take the words of each line as the output of each new line # for i in `cat. / cui`; do# l = `echo $I | wc-L` # echo ${l} # if [${l}-gt 6] The word then# echo $i# fi#done## is still in the line a=0while read linedo axiom $[aq1] echo "line ${a} content" for i in $line; do l = `echo $I | wc-L` if [${l}-gt 6]; then echo-n $i fi done echo done <. / cui

While reads are based on the shell read separator

While IFS is a set variable, when shell handles "command substitution" and "parameter substitution", shell disassembles the interpreted variable according to the value of IFS, default is space, tab, and newline, then processes the special characters, and finally reassembles the assignment to the variable.

The direct output of IFS is not visible, so you can see it by converting it to binary. "040s" is a space, "011s" is Tab, and "012s" is a newline character "\ n". The last 012 is because echo breaks lines by default. Change IFS

$cat 1.txt

1,a

2,b

3,c

4,d

$cat test.sh

Use comma division

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report