免交互操作
1. Here Document
Here Document 使用 I/O 重定向的方式将命令列表提供给交互式程序或命令。它可以帮助脚本开发人员不必使用临时文件来构建输入信息,而是直接就地生成一个“文件”并用作“命令”的标准输入。
示例:
bash
#!/bin/bash
cat <<EOF > myfile.txt
This is a line of text.
This is another line of text.
EOF2. 使用重定向符号
bash
command < inputfile.txt3. 使用 cat 命令
bash
cat inputfile.txt | command4. 使用 while 循环读取文件内容
如果你需要逐行处理文件内容,可以使用 while 循环。例如:
bash
while IFS= read -r line; do
command "$line"
done < inputfile.txt5. 使用 xargs
如果你的文件内容是命令列表,可以使用 xargs 来执行。例如:
bash
xargs -a inputfile.txt -I {} command {}