【问题标题】:Badly placed ()'s error with the following shell script错误放置 () 的错误与以下 shell 脚本
【发布时间】:2013-08-16 05:11:10
【问题描述】:

这是代码 sn-p。 这里我看到了放置不好的()的错误

#!/bin/sh 
#!/usr/bin/perl -w

# array declaration
arr= (one two three)  # seeing error here

# for loop
for (( i=0;i<4;i++ ))
do
    echo "\n $i : ${a[i]}"
done

【问题讨论】:

  • 你的 !# 调用正确吗?

标签: arrays shell for-loop sh


【解决方案1】:
arr= (one two three)

让我们分解一下它的作用。

arr=

这部分为$arr 分配一个空值(暂时,因为它在命令之前)。

(one two three)

这部分在带有参数twothree 的子shell 中运行one,之前分配的值为$arr

您是不是打算将这三个值分配给 $arr 中的一个数组?

【讨论】:

    【解决方案2】:

    这是一个小错误。

    arr= (one two three)

    应该是

    arr=(one two three)

    您也不能在echo 中使用\n。如果您想使用\n,请使用printf

    并修复其余错误,代码如下所示。

    # array declaration
    arr=(one two three)  
    
    # for loop
    for (( i=0;i<3;i++ ))
    do
        printf "\n $((i+1)) : ${arr[i]}"
    done
    echo ""
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 2014-11-05
      • 2014-06-06
      • 2016-03-22
      相关资源
      最近更新 更多