Bash:从指定文件中读取行

时间:2014-11-16 20:02:00

标签: bash file

在Bash中,我想知道如何从我指定的目录中的文件中读取行,以便在运行脚本时没有争论。我所看到的只是建议运行一个脚本,文件是作为争论而不是指定的。

2 个答案:

答案 0 :(得分:2)

while read -r line; do
  echo "$line"
done < filename

答案 1 :(得分:0)

% cat read_line_by_line_example.txt_inside
dir="$1"
[ -f "$dir"/example.txt ] || exit 1
# borrowing from Cyrus
while read -r line; do
  echo "$line"
  # do watever you want with "$line"
done < "$dir"/example.txt
% sh read_line_by_line_example.txt_inside "a/b/c/d/my dir"
line 1
line b
last line
% 
相关问题