简体   繁体   English

Bash文件描述符并在while循环中将printf写入文件

[英]Bash file descriptor and write printf to a file in a while loop

I'm learning bash programming through a book and now I'm learning about opening files and file descriptors but I can't get this code to work(see below), I want to use the file descriptor 4 to write the output of the lower while loop to a file somefile24.log but somefile24.log is empty after I run the code. 我正在通过一本书学习bash编程,现在我正在学习有关打开文件和文件描述符的信息,但是我无法使此代码正常工作(请参见下文),我想使用文件描述符4来编写较低的while循环到文件somefile24.log但是运行代码后somefile24.log为空。

Here is the code: 这是代码:

#!/bin/bash
#
# openFile.sh: print the contents of orders.txt

shopt -s -o nounset

declare LINE

exec 3< orders.txt
while read LINE <&3
do
    printf "%s\n" "$LINE"
done


echo
echo "Here is the new part of the program!"


exec 4> somefile24.log
count=1
while read LINE <&3
do
    printf "%s-----count=%d\n" "$LINE" "$count" >&4
    let count++
done
exit 0

Here is the output: 这是输出:

(something78@notemDEB78)-(01:45:03)-(~/Bash_Programming_2018)
$./openFile.sh 
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something

Here is the new part of the program!

Expected output is: 预期输出为:

The script writes the contents of orders.txt to somefile24.log 该脚本将orders.txt的内容写入somefile24.log


I've tried to check the code in shellcheck but it doesn't see the problem and only gives me this: 我试过检查shellcheck中的代码,但没有看到问题,只给了我这个:

$ shellcheck myscript

Line 10:
while read LINE <&3
      ^-- SC2162: read without -r will mangle backslashes.

Line 22:
while read LINE <&3
      ^-- SC2162: read without -r will mangle backslashes.

Line 25:
    let count++
    ^-- SC2219: Instead of 'let expr', prefer (( expr )) .

I've tried to make a test on the command line: 我试图在命令行上进行测试:

(something78@notemDEB78)-(01:59:41)-(~/Bash_Programming_2018)
$exec 3>somefile24.log 
(something78@notemDEB78)-(01:59:59)-(~/Bash_Programming_2018)
$echo testing >&3
(something78@notemDEB78)-(02:00:03)-(~/Bash_Programming_2018)
$cat somefile24.log 
testing

I know that I can just do: 我知道我可以做到:

while
printf "%s\n" "$LINE" >> somefile24.log

QUESTION: 题:

Is it possible to use a file descriptor to write to a file like this in a loop and if so how and what am I doing wrong? 是否可以使用文件描述符在循环中写入这样的文件?如果是,我怎么做和做错了什么?

And why is somefile24.log alway's empty after I run the script? 为什么运行脚本后somefile24.log总是为空?

You already read the contents of the file in your first loop. 您已经在第一个循环中读取了文件的内容。 Hence the file descriptor 3 is already at EOF. 因此,文件描述符3已经位于EOF。 You can reset the file descriptor 3 before you read on it again. 您可以在重新读取文件描述符3之前重设它。 Eg before your second loop, run this again exec 3< orders.txt 例如,在第二个循环之前,请再次运行exec 3< orders.txt

After the first loop the file descriptor is at EOF. 在第一个循环之后,文件描述符位于EOF。 So you need to reset the file descriptor before going in to the second loop. 因此,在进入第二个循环之前,您需要重置文件描述符。

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

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