加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 未找到Bash中的数组

发布时间:2020-12-13 19:12:49 所属栏目:Linux 来源:网络整理
导读:我试图在bash中声明一个数组,但是当代码运行时它说它找不到数组.我试图以几种不同的方式写出数组的声明,但似乎无论我如何声明它我都无法使它工作.我最初试图宣布它为: candidate[1]= 0candidate[2]= 0candidate[3]= 0 返回的错误消息是: votecalculation.s
我试图在bash中声明一个数组,但是当代码运行时它说它找不到数组.我试图以几种不同的方式写出数组的声明,但似乎无论我如何声明它我都无法使它工作.我最初试图宣布它为:
candidate[1]= 0
candidate[2]= 0
candidate[3]= 0

返回的错误消息是:

votecalculation.sh: 13: candidate[1]=: not found
votecalculation.sh: 14: candidate[2]=: not found
votecalculation.sh: 15: candidate[3]=: not found

在此之后我尝试了另一种在线发现的解决方案

ARRAY=( 'can1' 'can2' 'can3' )

使用它时会返回此错误:

votecalculation.sh: 12: Syntax error: "(" unexpected

我是Bash的新手,我对阵列感到很困惑.我需要一些特定的方式来声明一个数组,还是我只是完全错了?

解决方法

#!/bin/bash

 myarray[0]=hello
 myarray[1]=world

 echo ${myarray[0]}
 echo ${myarray[1]}

保存到helloworld.bash

执行使用. ./helloword.bash

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读