【问题标题】:bash: iterate over list of floating numbersbash:迭代浮点数列表
【发布时间】:2012-08-07 19:00:56
【问题描述】:

在 bash 脚本中,我想遍历一个值列表,我想将这些值作为参数传递给 python 中的脚本。事实证明,$d$minFreq 在传递给 python 脚本时不是浮点数。为什么会这样?

for d in {0.01, 0.05, 0.1}
do
    for i in {1..3}
    do
        someString=`python scrpt1.py -f myfile --delta $d --counter $i| tail -1`
        for minFreq in {0.01, 0.02}
        do
            for bValue in {10..12}
            do
                python testNEW.py $someString -d $bValue $minFreq
            done
        done
    done
done

【问题讨论】:

  • someString = $(python ...) 行中去掉等号周围的空格(用$(...) 替换反引号,因为将反引号作为反引号在评论中充其量是棘手的。

标签: bash for-loop floating-point


【解决方案1】:

去掉空格

for d in {0.01,0.05,0.1}

或者不要使用 {} 扩展(这里没有必要):

for d in 0.01 0.05 0.1

这同样适用于minFreq 循环。


正如所写,

for d in {0.01, 0.05, 0.1}

变量d被分配了文字字符串值{0.01,0.05,0.1}

【讨论】:

  • 虽然扩展中的空格是不必要的,但我认为 Python 进程根本不应该看到它们。我希望 shell 在构造参数列表时删除空格,因为没有引用 shell 变量。
  • 空格防止大括号展开,所以大括号和逗号不是语法,而是包含在传递给python的字符串中。
猜你喜欢
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多