【问题标题】:Assign Variables in Loop at Bash在 Bash 的循环中分配变量
【发布时间】:2019-07-15 00:54:09
【问题描述】:

我试图在 bash 中运行一些代码。但我必须在循环中分配或设置变量。当我运行我的 .sh 时,出现错误:No command in line

以下是代码示例。

#! /bin/bash
newfile="/exp/exp/code"
bool=true
file="/exp/exp/newcode"
for i in {1..10}
do
    declare $bool=false
    $newfield =$file
    $file=$i
done

【问题讨论】:

  • 删除作业左侧的“$”。并删除“=”符号周围的所有空格。
  • 顺便说一句,您不需要以这种方式在循环中“声明”。
  • shellcheck.net 。祝你好运。

标签: bash shell loops variables set


【解决方案1】:

我不确定您想要做什么或实现什么,但您问题中的片段应为:

#! /bin/bash
newfile="/exp/exp/code"
bool=true
file="/exp/exp/newcode"
for i in {1..10}
do
    bool=false
    newfield=$file
    file=$i
done

【讨论】:

  • 也许你应该问问他们想要达到什么目的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多