逐行读取文件,将值赋值给变量我有以下.txt文件:Marco
Paolo
Antonio我想逐行读取它,对于每一行,我都希望将一个.txt行值赋给一个变量。假设我的变量是$name,流量是:从文件中读取第一行指派$name=“马可”用$name从文件中读取第二行指派$name=“Paolo”
3 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
#!/bin/bashwhile IFS= read -r line; do echo "Text read from file: $line"done < "$1"
IFS=
(或 IFS=''
)防止前导/尾随空格被裁剪。 -r
防止反斜杠被解释。
readfile
chmod +x readfile./readfile filename.txt
while IFS= read -r line || [[ -n "$line" ]]; do echo "Text read from file: $line"done < "$1"
|| [[ -n $line ]]
\n
read
read
while IFS= read -r -u3 line; do echo "Text read from file: $line"done 3< "$1"
read -u3
read <&3
缥缈止盈
TA贡献2041条经验 获得超4个赞
-r
read
-r Do not treat a backslash character in any special way. Consider each backslash to be part of the input line.
man 1 read
.
#!/usr/bin/bashfilename="$1"while read -r line; do name="$line" echo "Name read from file - $name"done < "$filename"
添加回答
举报
0/150
提交
取消