简体   繁体   English

Bash:对于文件中的每一行,设置变量

[英]Bash: For Each Line in a File, Set variable

I am trying to iterate through a text file, and set a variable once for each line. 我试图遍历一个文本文件,并为每行设置一次变量。

I am having trouble doing so. 我这样做很麻烦。 I have simplified the process so that it is just echoing the result. 我简化了过程,以使其与结果相呼应。 There would be more going one, but this part of the widget is not working correctly. 会有更多内容,但是小部件的这一部分无法正常工作。

while read p; do
   set $FN=`echo $p`
   echo $FN
done <file.txt

When I don't use set, it looks like it is trying to echo a file called the contents of $p. 当我不使用set时,似乎正在尝试回显名为$ p内容的文件。

I am using echo $p to send the line to a series of cuts, to get the data I want. 我正在使用echo $ p将行发送到一系列剪切,以获取所需的数据。

I know I am doing something fundamentally wrong, just not sure what that might be. 我知道我在做根本上是错误的事情,只是不确定那是什么。

This works for me. 这对我有用。 Just say "FN=" without the "set 只需说“ FN =“而不设置“

while read p; do
  FN=$p
  echo $FN
done < test.list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM