我有一个名为“ test_file1”的文件。我想读取此文件的每一行并将其写入另一个名为“ test_file2”的文件。这两个文件都在同一目录中。
我试过了
#!/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"
Run Code Online (Sandbox Code Playgroud)
该脚本将“ Test”写入test_file2.txt,但不将“ legendary”或test_file1中的行写入test_file2。
有人可以帮忙吗?
谢谢。
只需直接使用文件,而不是先将其读取到数组中即可;这样做将您更改
done <"$file"
为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"
Run Code Online (Sandbox Code Playgroud)