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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With