关联数组:错误“声明:-A:无效选项" [英] Associative arrays: error "declare: -A: invalid option"

查看:40
本文介绍了关联数组:错误“声明:-A:无效选项"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,在 bash (v 4) 中使用关联数组.

I've written a script that uses associative arrays in bash (v 4).

它在我使用 4.1.5(1)-release 的本地机器上运行良好.

It works fine on my local machine which is using 4.1.5(1)-release.

在生产机器上,使用 4.1.0(1)-release 声明 assoc 数组的以下行失败:

On the production machine, using 4.1.0(1)-release the following line, which declares the assoc array, fails:

declare -A uniqjars

带有消息:

/script.sh: line 11: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

我的印象是这是 bash 4 的一般功能?

I was under the impression this was a general bash 4 feature?

在生产机器上的 bash 手册中,它讨论了使用 -A,所以我认为它应该工作.

In the man for bash on the production machine it discusses using -A so I assume it should work.

关联数组是使用declare -A name.

Associative arrays are created using declare -A name.

我可以通过打印出 echo 'bash -version 的值来确认脚本使用的是正确版本的 bash.

I can confirm the script is using the right version of bash by printing out the value of echo 'bash -version.

我可能做错了什么?

推荐答案

确保在 shell 脚本(#!/bin/bash 或其他任何) 也是第 4 版.如果你在做:

Make sure the version of bash being invoked as interpreter at the top of your shell script (#!/bin/bash or whatever) is also version 4. If you're doing:

bash --version

它给你 v4,做一个 which bash 来检查它的位置.

and it's giving you v4, do a which bash to check it's location.

这篇关于关联数组:错误“声明:-A:无效选项"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆