问题描述:

I have a file /tmp/a.txt whose contents I want to read in a variable many number of times. If the EOF is reached then it should start from the beginning.

i.e. If the contents of the file is "abc" and I want to get 10 chars, it should be "abcabcabca".

For this I wrote an obvious script:

while [ 1 ];

do cat /tmp/a.txt;

done |

for i in {1..3};

do read -N 10 A;

echo "For $i: $A";

done

The only problem is that it hangs! I have no idea why it does so!

I am also open to other solutions in bash.

网友答案:

To repeat over and over a line you can :

yes "abc" | for i in {1..3}; do read -N 10 A; echo "for $i: $A"; done

yes will output 'forever', but then the for i in 1..3 will only execute the "do ... done;" part 3 times

yes add a "\n" after the string. If you don't want it, do:

 yes "abc" | tr -d '\n' | for i in {1..3}; do read -N 10 A; echo "for $i: $A"; done

In all the above, note that as the read is after a pipe, in bash it will be in a subshell, so "$A" will only available in the "do....done;" area, and be lost after!

To loop and read from a file, and also not do that in a subshell:

for i in {1..3}; do read -N 10 A ; echo "for $i: $A"; done <$(cat /the/file)

To be sure there is enough data in /the/file, repeat at will:

for i in {1..3}; do read -N 10 A ; echo "for $i: $A"; done <$(cat /the/file /the/file /the/file)

To test the latest: echo -n "abc" > /the/file (-n, so there is no trainling newline)

相关阅读:
Top