This question already has an answer here:
I want to write an script which has as arguments two files F1 and F2 and prints them alternatively; first will be written F1's first line, then F2'2 second line and so on. If one of them has less lines than the other, when we finish to print the shorter one, the script should write the longest until the end.
My idea was:
1) Check if there are not 2 arguments -> echo and exit 2) Check if F1 or F2 aren't files -> echo and exit 3) Body:
exec 3 < $1 exec 4 < $2 i=0 j=1 while read -u 3 line && ((i==0)) do echo line; echo ((i++)) ((j--)) while read -u 4 line && ((j==0)) do echo line; echo ((j++)) ((i--)) done done exit $?
Doubt: This would only work if both files have the same number of lines. How can I improve this to extend this solution to files with different size?