Please do not leave people guessing. Be complete. Show a representative sample of input, desired output, attempts at a solution and specify what OS and version are being used.
#!/bin/bash
count=$((1))
while read item
printf %s `sed -n $"count"p test1.txt` > test2.txt
count=$((count+1))
do
echo "why?"
done <test3.txt
I am just experimenting to investigate the behavior there is no purpose. Please find test1.txt and test3.txt in the attachment. I want to echo for the amount of line numbers in test3.txt but it doesn't quit the loop now.
This should work. Still it would make sense to adopt some programming, say, standards:
Use $(...) instead of deprecated `...`
Use ${...} to separate variable names from its environment/context
Use "..." for strings like format or variables with spaces. So above would become
The output file would be empty because the script is continually overwriting the output file because of the > redirection every time it goes through the loop.
--
If we combine the recommendations in this thread, we end up with something like this:
count=1
while read item
do
printf "%s" "$(sed -n "${count} p" test1.txt)"
count=$((count+1))
echo "why?"
done < test3.txt > test2.txt
There is not output.txt generated anywhere. test2.txt will hold all lines if used as scrutinizer proposed in post#14.
As test1.txt and test3.tst happen to have 215 lines each, your output should end with 1072why? . That's it. No extra "why?"s, no extra dots. The contents of test3.txt is irrelevant as it is read but not used anywhere. Should test3.txt be longer than test1.txt , extra "why?"s will be there, if shorter, the output will stop earlier.
This will never happen. If you printf to test2.txt like in post#13, it will be empty in the former case, and hold test1.txt 's respective line's contents in the latter. All the "why?"s go to stdout in both cases.