Reputation: 674
I have a file called "test_file1". I want to read each line of this file and write it to another file called "test_file2". Both of these files are in the same directory.
I tried
#!/bin/sh
# My first Script
echo "Hello World!"
file=$(<test_file1.txt)
echo "Test" >> test_file2.txt
while IFS= read -r line;do
echo -e "legendary" >> test_file2.txt
echo "$line" >> test_file2.txt
done <"$file"
echo "completed"
The script writes "Test" into test_file2.txt but does not write 'legendary' or the lines in test_file1 into test_file2.
Can someone please help.
Thank you.
Upvotes: 3
Views: 34494
Reputation: 35358
Just use the file directly instead of first reading it into an array; to do that change your
done <"$file"
to done < "test_file1.txt"
#!/bin/sh
# My first Script
echo "Hello World!"
echo "Test" >> test_file2.txt
while IFS= read -r line;do
echo -e "legendary" >> test_file2.txt
echo "$line" >> test_file2.txt
done < "test_file1.txt"
echo "completed"
Upvotes: 6