Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 为什么是“循环”;虽然做“完成”;can';你不读文本文件吗?_Bash_Text Files - Fatal编程技术网

Bash 为什么是“循环”;虽然做“完成”;can';你不读文本文件吗?

Bash 为什么是“循环”;虽然做“完成”;can';你不读文本文件吗?,bash,text-files,Bash,Text Files,我想做的工作是通过循环“While…do…done”从文本文件中读取一行(当前此文件仅包含一行,以后将增加行数)。奇怪的是它只能读取一些文本文件。我的代码是: ...(previous commands to create "myfile.txt")... while read -r line do echo "flag" done < "myfile.txt" …(以前创建“myfile.txt”的命令)。。。 而read-r行 做 回声“旗” 完成

我想做的工作是通过循环“While…do…done”从文本文件中读取一行(当前此文件仅包含一行,以后将增加行数)。奇怪的是它只能读取一些文本文件。我的代码是:

...(previous commands to create "myfile.txt")...
while read -r line
do
 echo "flag" 
done < "myfile.txt"
…(以前创建“myfile.txt”的命令)。。。
而read-r行
做
回声“旗”
完成<“myfile.txt”
我试过几个案子。如果我将“myfile.txt”替换为当前目录中手动创建的另一个文件“test.txt”(此“test.txt”也包含一行),我的脚本可以打印“flag”

同样,在“myfile.txt”创建之后,如果我修改并保存在当前目录中,然后运行我的脚本,它也可以正常打印“flag”

除上述两种情况外,我的脚本无法打印“标志”。 我还尝试在我的脚本中“chmod”和“touch”文本文件,如下所示,它也不能工作

显然,我希望我的脚本能读到文本文件中的行,有人能告诉我原因并给出解决方案吗

顺便说一句,这个文件可以通过cat命令读取

...(previous commands to create "myfile.txt")...
chmod 777 "myfile.txt"
touch "myfile.txt"
cat "myfile.txt" #(I can see the results of this line)
while read -r line
do
 echo "flag" 
done < "myfile.txt"
…(以前创建“myfile.txt”的命令)。。。
chmod 777“myfile.txt”
触摸“myfile.txt”
cat“myfile.txt”#(我可以看到这行的结果)
而read-r行
做
回声“旗”
完成<“myfile.txt”
谢谢

创建文本文件的整个代码大约是800行。但是,我想发布创建文本文件的行。这是:

  for(i = 1, i<=6, ++i){ 
    ...
    ofstream myfile("myfile.txt", std::ios_base::app);
     ...
    if(myfile.is_open()){
       myfile << "rms_" << std::setprecision(3) << RMS_values ;
       myfile.close();
    }
  }

for(i=1,i我真的不明白你的问题-你能给我们展示更多的代码/你是如何创建文件的吗

以下是一个工作示例:

$ cat readfile.sh
#!/bin/bash

{
        cat <<EOT
this
is
a
test
file
EOT
} > ./test.txt

while read -r line; do
        echo "line = [${line}]"
done <./test.txt

$
cat test.sh

#!/bnin/bash
echo "content" > "myfile.txt"
cat "myfile.txt" #(I can see the results of this line)
while read -r line
do
 echo "flag" 
done < "myfile.txt"
content
flag
$


它可以工作。没有问题。脚本与您发布的内容完全相同,只是触摸被替换为一些内容,因为while循环在文件中每行打印一条消息,因此如果没有行,则(并且
touch
不会添加任何内容),它显然不会打印任何内容。

我在这里猜测:

在Unix中,对文本文件有两种假设:

  • 所有行都以
    字符结尾。如果在使用
    的旧Mac上编辑文件,Unix将看不到行尾。如果在Windows程序(如
    Notepad.exe
    )上编辑文件,行将以
    结尾,Unix将假定
    是行的一部分
  • 所有行必须以
    结尾,包括最后一行。如果您使用C程序编写程序,则最后一行可能不会以
    结尾,除非您专门将其写出
Unix实用程序,如
awk
grep
,以及shell,都是基于这些假设而存在的。当有人在使用shell脚本读取文件时通常告诉我有些东西不太管用时,我会告诉他们在
VIM
中编辑该文件,然后保存它(从而强制使用一个
字符结尾).在VIM中,您需要
:设置ff=unix
,然后保存。这通常会解决问题


我的猜测是,您正在读取的文件没有正确的行尾,和/或最后一行没有
字符。

我不知道
bash
脚本,但我猜可能在您创建文件后,缓冲区没有刷新,因此文件没有完全写入脚本对我来说是正确的-如何创建文件?@Zaiborg:file只能在一个进程内刷新。因为每个shell命令都是独立的进程,所以这不成问题。你能发布一个吗?你发布的内容根本没有显示任何错误。@JoshJolly,正如我前面提到的,如果文本文件(在我的例子中是“myfile.text”)是在当前目录下创建的,它对我也很有效。我的情况正如你所猜测的。请参阅我文章的最后一部分。@user2740039别忘了接受David的答案!:-)
#!/bnin/bash
echo "content" > "myfile.txt"
cat "myfile.txt" #(I can see the results of this line)
while read -r line
do
 echo "flag" 
done < "myfile.txt"
content
flag