标签: while-loop

如何在 Bash 中循环直到达到可用磁盘空间限制?

我想做一组操作 ( x y z),只要我在挂载到/media/z.

我怎样才能做到这一点?

我试过类似的东西

while (`df | grep /media/z | awk '{print $4}'` > 204800); do x; y; z; done;
Run Code Online (Sandbox Code Playgroud)

但我想我的while语法是错误的。

bash disk while-loop

4
推荐指数
1
解决办法
1481
查看次数

我不明白在 C# 中使用“while(true)”和“for(;;)”循环!

While(true)循环的示例代码:

    public void HandleConnection()
    {
        TcpClient tcpClient = tcpListener.AcceptTcpClient();
        NetworkStream networkStream = tcpClient.GetStream();
        while (true)   /**Don't understand this while(true) loop**/
        {
            buffer = new byte[1024];
            n = networkStream.Read(buffer, 0, buffer.Length);
            if (n == 0)
            {
                break;
            }

        }
    }
Run Code Online (Sandbox Code Playgroud)

for(; ;)循环的示例代码:

     public void readLine(NetworkStream inputStream)
     {
        using (StreamReader reader = new StreamReader(inputStream))
        {

            char[] buffer = new char[128];
            int n;

            for (; ; ) /**Don't understand this for(; ;) loop**/
            {
                n = reader.Read();
                if (n == -1 …
Run Code Online (Sandbox Code Playgroud)

c# for-loop while-loop

4
推荐指数
1
解决办法
1826
查看次数

Bash 脚本在 while 循环中停止执行 ffmpeg - 为什么?

我有这个用于将.mp4视频转换为.mp3音频的bash 脚本。它运行,但只循环一次,尽管/home/myname/mp4dir.

该脚本将.mp4它找到的第一个文件转换为.mp3,除非已经有一个.mp3文件。这应该在循环中完成,但在第一次调用 ffmpeg 后脚本停止。

为什么?

#!/bin/bash
find /home/myname/mp4dir -name "*.mp4" | while read f
do
        fOut=`echo $f | sed s/mp4/mp3/`
        echo "convert $f => $fOut"
        if ! test -f $fOut
        then
                ffmpeg -i $f -vn -f mp3 $fOut
        fi
done
Run Code Online (Sandbox Code Playgroud)

bash loops ffmpeg while-loop

4
推荐指数
2
解决办法
6268
查看次数

使 while 循环变量成为全局变量

我想访问我的 while 循环创建的最后一行(最后一个 $var )。我试过这样的事情:

$bar= "";
while ($bar= mysql_fetch_array($foo)) {
    global $bar;
}
echo $bar
Run Code Online (Sandbox Code Playgroud)

为什么那行不通?

php global-variables while-loop

4
推荐指数
1
解决办法
5763
查看次数

while 循环中的 strpos() 永远不会结束

有一个字符串,

$string = 'Foo, Bar, Test,';

我想要做的就是计算字符串中逗号的数量。

但一切都会导致无限的 while 循环。

所以,我试过#1:

$count = 0;

while($pos = strpos($string, ',') !== FALSE){
    $count++;
    // Never ends
}
Run Code Online (Sandbox Code Playgroud)

还有#2,

while(true){
  if ( strpos($string, ',') !== FALSE ){
     $count++;
  } else {
    break;
  }
}
Run Code Online (Sandbox Code Playgroud)

他们俩永远不会结束。问题出在哪里?

php while-loop strpos

4
推荐指数
1
解决办法
5539
查看次数

如何将文本文件中的每一行保存为新文件

我有一个带有 5 列的制表符分隔文本文件,我希望每一行都是它自己的 txt 文件,其中包含来自第 2-5 列的信息并以第 1 列命名。

例如,我的 txt 文件有数百行与此类似:

sample1name_oligos 引物 forwardseq reverseseq sample1name

sample2name_oligos 引物 forwardseq reverseseq sample2name

我想要一个名为 sample1name_oligos 的 txt 文件,如下所示:

引物 forwardseq reverseseq sample1name

和一个名为 sample1name_oligos 的 txt 文件,如下所示:

引物 forwardseq reverseseq sample1name

我试过两种方法:

1.我找到了我认为的解决方案:

awk '{print substr($0,match($0,$2)) >> ( $1 ".txt" )}' 文件名

(来自http://www.linuxquestions.org/questions/linux-newbie-8/how-to-save-each-line-from-textfile-as-new-file-889795/)

这适用于我制作的测试文件(5 行),但是当我在 100 多行文件上运行它时,我得到了前 17 个文件,然后出现错误:

awk: File18.txt 使打开的文件过多,输入记录号 18,文件 myfile.txt 源行号 1

我删除了第 18 行并重试并得到相同的错误。我删除了前 20 行并重试并得到相同的错误。

2.从同一个链接,我试过

猫 myfile.txt | 在阅读 LINE 时;做 echo …

awk while-loop

4
推荐指数
1
解决办法
3528
查看次数

用while循环python写阶乘

我是新手,对 Python 了解不多。有人知道如何在 while 循环中编写阶乘吗?

我可以在 if / elif else 语句中实现:

num = ...
factorial = 1

if num < 0:
   print("must be positive")
elif num == 0:
   print("factorial = 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print(num, factorial)
Run Code Online (Sandbox Code Playgroud)

但我想用一个 while 循环(无功能)来做到这一点。

python loops factorial while-loop

4
推荐指数
2
解决办法
5万
查看次数

JMeter while 循环未检测到 EOF

当它从 JMeter 中的 CSV 读取 EOF 时,我想做“while”控制器转到下一个控制器。while 循环停止,但它又迭代了 1 次,我的意思是说它在检测到 EOF 时不需要迭代。当我将 EOF 值上的停止线程选择为“True”时。while 循环停止并且不进行下一个循环。

当 CSV 文件检测到 EOF 而不重复一个循环时,我需要转到下一个循环控制器。

提前致谢。

csv loops jmeter while-loop eof

4
推荐指数
1
解决办法
3103
查看次数

使用 grep 读取行时

我正在尝试报告使用 grep 和 while 找到的行。

我知道您可以使用以下内容来比较来自 input.txt 的字符串列表,并在您的目标文件中找到它们,如下所示:

grep -f inputs.txt file_to_check
Run Code Online (Sandbox Code Playgroud)

我想要的是读取输入字符串的每一行并在循环中单独grep它们。

所以我尝试了以下方法:

cat inputs.txt | while read line; do if grep "$line" filename_to_check; then echo "found"; else echo "not found"; fi; done
Run Code Online (Sandbox Code Playgroud)

当我将输出重定向到文件时,这不会返回任何内容。

while read line
do
if grep "$line" file_to_check
  then echo "found"
else 
  echo "not found"
fi
done < inputs.txt
Run Code Online (Sandbox Code Playgroud)

与第一个相同,但从我发现的更好。

我知道它会逐行迭代,因为我可以用 echo $line 替换 grep 并打印每一行;但任一方法都不会返回类似 grep -f 的内容,而是显示:

not found
not found
not found
.
. etc.
Run Code Online (Sandbox Code Playgroud)

所以我正在寻找的是它会遍历每一行并使用 if 语句通过 grep 检查它以确定 grep 是否真的找到它的东西。我知道我可能没有所有正确的逻辑,但我想要的输出应该是这样的: …

bash grep if-statement while-loop

4
推荐指数
2
解决办法
3万
查看次数

什么 while(($#)); 做 ...; 转移; done 在 bash 中是什么意思,为什么有人会使用它?

我在网上的 bash 脚本教程中遇到了以下 while 循环:

while(($#)) ; do
   #...
   shift
done
Run Code Online (Sandbox Code Playgroud)

我不明白在 while 循环中使用位置参数基数。我知道该shift命令的作用,但是 while 语句与shift?

parameters bash while-loop

4
推荐指数
2
解决办法
5126
查看次数