Linux in the shell for loop implementation Read File wrapping

Source: Internet
Author: User


I. Issues and needs:

For example, there is a aa.list file that reads:


1 2 3 4
A B c D

For script reads a file, I want to read in one row, read one row at a time, like the first time is 1 2 3 4, the second is a B c D
But I wrote this.

For I in "$ (</aa.list)"
Todo
Echo $i
echo 1111
Done

The results are:

1
11111
2
11111
3
11111
4
11111
A
11111
B
11111
C
11111
D
11111

How to solve it?

Ii. Solutions

1. You can use the while loop

While Read line
Todo
Echo $line
Done < Aa.list

But it seems certain to use a For loop, and the problem does not match.

2. By modifying the system's IFS (internal domain delimiter), customizing the IFS, defined as \ n, to implement the

ifs=$ "\ n"
For I in $ (< Aa.list)
Todo
Echo $i
Done

Note: Generally do not modify the IFS, lest the system display errors, first the IFS value defined to Oldifs, after running the program, then the default IFS value restore. As follows

oldifs= $IFS
ifs=$ "\ n"
For I in $ (< Aa.list)
Todo
Echo $i
Done
ifs= $oldifs

For Loop Writing summary

1, for ((i=1;i<=10;i++));d o echo $ (expr $i \* 4);d One
2, in the shell is commonly used for the I in $ (seq 10)
3, for i in ' ls '

4, for I in ${arr[@]}
5, for I in $*; Todo
6, for File in/proc/sys/net/ipv4/conf/*/accept_redirects; Todo
7, for I in F1 F2 F3;d o
8, for I in *.txt
9, for I in $ (LS *.txt)
The for in statement is combined with the ' and $ (), using ' or $ () to combine multiple lines into one line, which is actually an array of strings

============-_-==============for num in $ (seq 1)
10, list= "Rootfs usr data data2"
for D-in $LIST; does
The for in statement automatically traverses the character of the string by space, traversing the
11, for I in {1..10}
12, to I in Stringchar {1..10}
13, awk ' Begin{for (i=1; i<) for multiple directories. = 10; i++) Print i} '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.