这是代码片段。在这里,我看到了放置错误的 () 错误
#!/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
这是一个小错误。
arr= (one two three)
应该是
arr=(one two three)
你也不能\n在echo. printf如果您想使用,请使用\n.
并修复其余错误,代码如下所示。
# array declaration
arr=(one two three)
# for loop
for (( i=0;i<3;i++ ))
do
printf "\n $((i+1)) : ${arr[i]}"
done
echo ""
arr= (one two three)
让我们分解一下这是做什么的。
arr=
这部分分配$arr一个空值(暂时,因为它在命令之前)。
(one two three)
这部分one在带有参数two和的子shell中运行three,之前分配的值为$arr。
您是否可能打算将这三个值分配给一个数组$arr?