在 .sh 文件中使用 do 时出现“bash:数组和错误替换”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24808994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:53:40  来源:igfitidea点击:

"bash: Array and Bad substitution" when using do in .sh file

arrayslinuxbashshell

提问by fdicarlo

I wuld like to create a script who run some program and check if they started or no, unfortunately I receive several error and I don't know why:

我想创建一个脚本来运行一些程序并检查它们是否启动,不幸的是我收到几个错误,我不知道为什么:

#!/bin/sh
# start file

c[0] = 'foo'
c[1] = 'mickey'
c[2] = 'mouse'

echo "Launching..."

for i in 0 1 2; do
        echo "Starting ${c[i]}"
        ${c[i]}
        ps cax | grep ${c[i]}
        if [ $? -eq 0 ]; then
          echo "${c[i]} is running"
        else
        {
            echo "${c[i]} is not running"
            ${c[i]}
        } 
        fi
done

echo "launch completed."

when I try to run it, the shell don't recognize both the command name and I receive error also on the line

当我尝试运行它时,shell 无法识别命令名称,并且我也在线上收到错误

for i in 0 1 2; do

for "Bad substitution".

为“坏替代”。

Can you please explain where I'm wrong with this script? Thanks in advance.

你能解释一下我对这个脚本的错误吗?提前致谢。

---- Update ----

- - 更新 - -

Thanks to us for your REALLY PRECIOUS answers, I modified my script according to your advices:

感谢我们为您提供非常宝贵的答案,我根据您的建议修改了我的脚本:

#!/bin/bash
# start file


echo "Launching..."

for i in foo mickey mouse; do
        echo "Starting $i"
        $i
        ps cax | grep $i
        if [ $? -eq 0 ]; then
          echo "$i is running"
        else
            echo "$i is not running"
            $i
        fi
done

echo "launch completed."

Thanks again! Fabrizio

再次感谢!法布里齐奥

回答by scragar

OK, there's a few errors here, let's go through them from the start:

好的,这里有一些错误,让我们从头开始:

c[0] = 'foo'
c[1] = 'mickey'
c[2] = 'mouse'

cis undefined, and no spaces in assignments:

c未定义,并且赋值中没有空格:

c=()
c[0]='foo'
c[1]='mickey'
c[2]='mouse'

For

为了

    echo "Starting ${c[i]}"
    ${c[i]}
    ps cax | grep ${c[i]}
    if [ $? -eq 0 ]; then
      echo "${c[i]} is running"
    else
    {
        echo "${c[i]} is not running"
        ${c[i]}
    } 
    fi

You need to refer to ${c[$i]} to access the index, $c[i] is referencing an index based on the character i, not the variable.

您需要引用 ${c[$i]} 来访问索引, $c[i] 引用的是基于字符的索引i,而不是变量。

    echo "Starting ${c[$i]}"
    ${c[$i]}
    ps cax | grep ${c[$i]}
    if [ $? -eq 0 ]; then
      echo "${c[$i]} is running"
    else
    {
        echo "${c[$i]} is not running"
        ${c[$i]}
    } 
    fi

Finally:

最后:

    else
    {
        echo "${c[$i]} is not running"
        ${c[$i]}
    } 
    fi

Bash doesn't use braces for if/else, get rid of them

Bash 不为 if/else 使用大括号,摆脱它们

    else
        echo "${c[$i]} is not running"
        ${c[$i]}
    fi

All done.

全部完成。

回答by sun_dare

Please have a look at this code. Its not good to use 0 1 2 to iterate through the array because you might not know the size of the array. Its better to use ${c[@]} which would have all the elements of the array. This would be more dynamic and better

请看一下这段代码。使用 0 1 2 遍历数组并不好,因为您可能不知道数组的大小。最好使用 ${c[@]} ,它包含数组的所有元素。这会更有活力,更好

 #!/bin/sh
 # start file
 c=(foo mickey mouse)

  echo "Launching..."
  //below one to execute your foo or micckyor mouse command
  $i
  for i in ${c[@]}; do
    echo "Starting $i"
    ps cax | grep $i
    if [ $? -eq 0 ]; then
      echo "$i is running"
    else
    {
        echo "$i is not running"
         //below one to execute your foo or micckyor mouse command
         $i
    }
    fi
 done

 echo "launch completed."

回答by jrjc

You need to change

你需要改变

c[0] = 'foo'
c[1] = 'mickey'
c[2] = 'mouse'

to

c[0]='foo'
c[1]='mickey'
c[2]='mouse'