Bash while 循环逐行读取文件

发布于 2024-12-22 01:18:39 字数 353 浏览 0 评论 0原文

我想在这里讨论两种逐行读取文件的方法:

#!/bin/bash    

while read line    
do    
    echo-e "$ line \ n"    
done <file.txt

所以

#!/bin/bash    
exec 3<file.txt

while read line    
do    
    echo-e "$ line \ n"    
done

第一个版本工作正常,但我不明白 while 循环处理文件的机制。但第二个版本的机制我了解。但在这里我不明白为什么它挂起并且不打印任何内容。

There are two ways of reading a file line by line that I want to discuss here:

#!/bin/bash    

while read line    
do    
    echo-e "$ line \ n"    
done <file.txt

and

#!/bin/bash    
exec 3<file.txt

while read line    
do    
    echo-e "$ line \ n"    
done

So first version works fine but I don't understand the mechanism of working while loop with the file. But the mechanism of the second version I understand. But here I don't understand why it hangs and does not print anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无声静候 2024-12-29 01:18:39

第一个循环之所以有效,是因为 done 之后的重定向适用于整个循环,因此 read 是从文件读取,而不是从脚本的标准输入读取。

第二个版本挂起是因为 read 从文件描述符 0(标准输入)读取,而您没有在其中键入任何内容。 exec 行重定向文件描述符 3 以从文件中读取,但您不是从文件描述符 3 读取。

您可以使用以下方法来挽救第二个:

exec <file.txt

现在从指定文件读取标准输入。

The first loop works because the redirection after the done applies to the whole loop, so the read there is reading from the file, not from the standard input of the script.

The second version hangs because read reads from file descriptor 0, which is standard input, and you've not typed anything there. The exec line redirects file descriptor 3 for reading from the file, but you're not reading from file descriptor 3.

You could rescue the second by using:

exec <file.txt

Now standard input is read from the named file.

情话已封尘 2024-12-29 01:18:39

这可能对您有用:

exec 3<file.txt

while read -u3 line
do
    echo -e "$line \n"
done

-u3 从文件描述符 3 读取

奇怪的是 echo 没有像 ksh 的 print 命令那样的补码开关。

This might work for you:

exec 3<file.txt

while read -u3 line
do
    echo -e "$line \n"
done

-u3 reads from file descriptor 3

Strange that echo does not have the complement switch like ksh's print command.

路弥 2024-12-29 01:18:39

您的脚本中几乎没有错误。

  1. $ 和变量名之间有空格。 (可能是错误的编辑)
  2. echo-e 之间有空格。 (可能是错误的编辑)
  3. 提到在打开文件的文件描述符处进行读取。您正在读取描述符 0 处的文件,并且 exec 正在描述符 3 处运行。

它应该是这样的 -

#!/bin/bash    
exec 3<file.txt

while read line  
do
    echo -e "$line \n"
done <&3

There are few mistakes in your scripts.

  1. Space between $ and variable name. (may be bad edit)
  2. Space between echo and -e. (may be bad edit)
  3. Mentioning the read at a file descriptor where the file is open. You are reading the file at descriptor 0 and exec is running at descriptor 3.

It should be something like this -

#!/bin/bash    
exec 3<file.txt

while read line  
do
    echo -e "$line \n"
done <&3
仅冇旳回忆 2024-12-29 01:18:39

-u3 非常适合我的目的(仅阅读以下行)

#!/bin/bash

logs=(*Logs.txt)
[[ -e "${logs[0]}" ]] || exit 0

exec 3<"${logs[0]}"

while read -u3 line
do
        if [[ $(echo "$line"| grep -c SCSI_STATUS_CHECK_CONDITION) -eq 1 ]]; then
                read -u3 line
                echo "$line"
        fi
done

-u3 is great for my purpose (reading only the following line)

#!/bin/bash

logs=(*Logs.txt)
[[ -e "${logs[0]}" ]] || exit 0

exec 3<"${logs[0]}"

while read -u3 line
do
        if [[ $(echo "$line"| grep -c SCSI_STATUS_CHECK_CONDITION) -eq 1 ]]; then
                read -u3 line
                echo "$line"
        fi
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文