1

我有一个读取 lineas 的 bash 脚本

filename=$

while read LINE
do
 ...... 
done < $filename

我想知道如何将 $LINE 存储在字符串 (my_string) 中,以便我可以为每一行

回声 $my_thing" "$my_string

我尝试了几件事,但是当我使用 echo 或 printf 打印时,$LINE 会删除它之前的所有内容

詹斯

4

3 回答 3

1

Your file may have carriage returns in it. For example, it may be a DOS or Windows file that has that style of line endings. If that is the case, you can run dos2unix on the file to convert it to Unix-style line endings or you can strip them as you read them:

LINE="${LINE//$'\r'}"

which would go right after the do statement.

于 2010-01-26T14:21:02.060 回答
0

当您执行这样的 while 读取循环时,该行存储在变量 $LINE 中,所以试试这个

while read -r LINE
do
 echo "$LINE $LINE"
done <"file"

如果您想将 LINE 存储在另一个字符串中,请执行此操作

my_other_string=$LINE
于 2010-01-26T13:47:15.913 回答
0

我认为丹尼斯威廉姆森确定了为什么您会看到“删除之前打印的所有内容”行为。

让我指出,如果您要在 bash 脚本中执行“读取时...”处理,您需要同时使用-r标志来读取IFS并将其设置为空字符串。第一个用于在标准输入中保留任何反斜杠处理原始,第二个用于避免在标准输入的开头和结尾修剪空白。你可以像这样组合它们:

while IFS= read-r LINE; do
    echo "$LINE"
done < "file"
于 2010-02-15T23:29:59.483 回答