arrays - 下面的shell脚本中Badly Placed()的错误

标签 arrays shell for-loop sh

这是代码片段。 在这里我看到了错误放置的 () 错误

#!/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=(一二三)

应该是

arr=(一二三)

此外,您不能在 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 ""

关于arrays - 下面的shell脚本中Badly Placed()的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18228522/

相关文章:

ruby - 如何 "unflatten"一个 Ruby 数组?

bash - 编写运行命令并记录其退出代码的包装函数的最佳方法是什么

shell - 如何更正 shell 脚本以识别另一台运行 Linux 的服务器上的换行符?

python - os.path.isdir() 和 os.path.isfile() 都在脚本中返回 False 但在 Python 控制台中返回 True(就像它们应该的那样)

javascript - 映射 react 中的所有非空对象

javascript - 空数组和排序推送的键 JavaScript

linux - 查找字符串是否直接或间接存在于所有文件中

javascript - 如何让 for 循环包含 NetSuite 中用户事件脚本的最后一行?

javascript - 每个 : $.、.ForEach、for 循环或其他哪个最快?

c++ - 如何在 C++ 11 中创建此结构的数组?