gpt4 book ai didi

linux - 我如何更正 while 循环,它读取两个文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:06 24 4
gpt4 key购买 nike

我尝试执行读取两个文件的.sh 脚本。目前我收到此错误 line 7: file1: No such file or directory

用户和城市文件与脚本位于同一文件夹中。

#!/bin/bash
file1='user.txt'
file2='city.txt'

while read -r -u 4 line1 && read -r -u 5 line2; do
echo "Output-1: $line1 & Output-2: $line2"
done 4<file1 5<file2

最佳答案

此错误试图告诉您不存在名为“file1”的文件:

file1: No such file or directory

您要从中读取输入的不是“file1”,而是“user.txt”,这是变量 file1 的值。为此,请编写 $file1 而不是 file1,如下所示:

#!/bin/bash
file1='user.txt'
file2='city.txt'

while read -r -u 4 line1 && read -r -u 5 line2; do
echo "Output-1: $line1 & Output-2: $line2"
done 4<"$file1" 5<"$file2"

关于linux - 我如何更正 while 循环,它读取两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38681049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com