逐行讀取文件,將值賦值給變量我有以下.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
提交
取消