【问题标题】:While loop in bash using variable from txt file使用 txt 文件中的变量在 bash 中循环
【发布时间】:2015-06-22 14:41:19
【问题描述】:

我是 bash 的新手,我正在编写一个脚本来读取存储在文本文件每一行中的变量(这些变量有数千个)。所以我尝试编写一个脚本来读取这些行并自动将解决方案输出到屏幕并保存到另一个文本文件中。

./reader.sh > solution.text

我遇到的问题是目前我在 Sheetone.txt 中只有 1 个变量存储用于测试目的,这应该需要大约 2 秒才能输出所有内容,但它卡在 while 循环中并且没有输出解决方案。

#!/bin/bash
file=Sheetone.txt
while IFS= read -r line
do
        echo sh /usr/local/test/bin/test -ID $line -I 
done

【问题讨论】:

  • 您的while read 没有任何输入,您可能想说do ... done < file 以便由file 的内容提供。
  • 使用done < "$file"
  • 感谢解决问题

标签: linux bash rhel


【解决方案1】:

如 cmets 所示,您需要为您的 while 循环提供“一些东西”。 while 构造的编写方式将在条件下执行;如果给出了一个文件,它将继续执行直到 read 耗尽。

#!/bin/bash
file=Sheetone.txt
while IFS= read -r line
do
    echo sh /usr/local/test/bin/test -ID $line -I 
done < "$file"
# -----^^^^^^^ a file!

否则,就像没有轮子的自行车……

【讨论】:

    猜你喜欢
    • 2017-06-15
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    相关资源
    最近更新 更多