Bash 文件描述符并在 while 循环中将 printf 写入文件
Bash file descriptor and write printf to a file in a while loop
我正在通过一本书学习 bash 编程,现在我正在学习如何打开文件和文件描述符,但我无法使这段代码工作(见下文),我想使用文件描述符 4
将下层 while 循环的输出写入文件 somefile24.log
但在我 运行 代码后 somefile24.log
为空。
代码如下:
#!/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
这是输出:
(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!
预期输出为:
脚本将orders.txt的内容写入somefile24.log
我已经尝试检查 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 )) .
我尝试在命令行上进行测试:
(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
我知道我可以做到:
while
printf "%s\n" "$LINE" >> somefile24.log
问题:
是否可以使用文件描述符在循环中像这样写入文件?如果可以,我做错了什么?
为什么我运行脚本后somefile24.log
总是空的?
您已经在第一个循环中读取了文件的内容。因此文件描述符 3
已经在 EOF。您可以在再次阅读之前重置文件描述符 3
。例如。在你的第二个循环之前,运行 再次 exec 3< orders.txt
在第一个循环之后,文件描述符位于 EOF。所以你需要在进入第二个循环之前重置文件描述符。
我正在通过一本书学习 bash 编程,现在我正在学习如何打开文件和文件描述符,但我无法使这段代码工作(见下文),我想使用文件描述符 4
将下层 while 循环的输出写入文件 somefile24.log
但在我 运行 代码后 somefile24.log
为空。
代码如下:
#!/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
这是输出:
(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!
预期输出为:
脚本将orders.txt的内容写入somefile24.log
我已经尝试检查 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 )) .
我尝试在命令行上进行测试:
(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
我知道我可以做到:
while
printf "%s\n" "$LINE" >> somefile24.log
问题:
是否可以使用文件描述符在循环中像这样写入文件?如果可以,我做错了什么?
为什么我运行脚本后somefile24.log
总是空的?
您已经在第一个循环中读取了文件的内容。因此文件描述符 3
已经在 EOF。您可以在再次阅读之前重置文件描述符 3
。例如。在你的第二个循环之前,运行 再次 exec 3< orders.txt
在第一个循环之后,文件描述符位于 EOF。所以你需要在进入第二个循环之前重置文件描述符。