庆典,zsh中:声明[*](噢,我的) [英] bash, zsh : declare [*] (oh my)

查看:139
本文介绍了庆典,zsh中:声明[*](噢,我的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用具有以下语法bash脚本工作

I'm working with a bash script that has the following syntax

$ declare -a THIS[*]

这似乎是非法的zsh(我得到一个没有找到任何匹配:本[*]的错误)。谁能帮我翻译一下成zsh?

This seems to be illegal in zsh (I get a "no matches found: THIS[*]" error). Can anyone help me translate this to zsh?

此外 - 什么是 [*] 语法是什么意思? (我知道我们声明数组,但为什么[*]?)

Also - what does the [*] syntax mean? (I know we're declaring an array, but why the [*]?)

更新

要提供使用code其中的一个例子,并解释它是如何有效的 - 我复制了埃里克恩斯特龙的后几行上的无密码的ssh

To provide an example of where the code is used, and explain how it is valid - I've copied a few lines from Eric Engstrom's post on password free ssh

declare -a SSSHA_KEYS[*]

# --- PARSE ARGS --- #
sssha_parse_args() {
  local OPTIND=1
  while getopts "xe:k:t:" OPT; do
    #echo "$OPT $OPTARG $OPTIND"
    case $OPT in
      t) SSSHA_ARGS="-t $OPTARG" ;;
      e) SSSHA_ENV="$OPTARG" ;;
      k) [ -f "${OPTARG}" ] && SSSHA_KEYS[${#SSSHA_KEYS[*]}]="$OPTARG" ;;
      x) SSSHA_STOP_ON_EXIT=$OPT
    esac
  done
  shift $(($OPTIND - 1))

  # set default key, if none specified
  if [ -z "${SSSHA_KEYS[*]}" ]; then
    for key in $HOME/.ssh/id_[rd]sa; do
      [ -f "$key" ] && SSSHA_KEYS[${#SSSHA_KEYS[*]}]="$key"
    done
  fi
}

我相信 [*] 被用来作为某种动态的迭代器(因为我们不知道有多少项目后来有)。我只是想知道在zsh的同等声明!

I believe the [*] is being used as some kind of dynamic iterator (as we don't know how many items it will have later). I'd just like to know of the equivalent declaration in zsh!

推荐答案

只有Bash有声明。该kshes和zsh的有无排版。这code是在所有这些荒谬的,而且由于弹不同意有关的参数解析的声明命令的如声明排版(使用bash特别是 -a 变造以特定的方式解析),它会做不同的联合国$ p在每个$ pdictable,有潜在危险的事情。

Only Bash has declare. The kshes and zsh have typeset. That code is nonsensical in all of them, and since shells disagree about the parsing of the arguments to declaration commands like declare and typeset (with Bash in particular, -a alters parsing in a specific way), it's going to do a different unpredictable, potentially dangerous thing in each.

具体猛砸如何跨preting这一点 - 从手册页:

Specifically how Bash is interpreting this - from the manpage:

宣布-a名[下标]也是可以接受的;标被忽略。

declare -a name[subscript] is also accepted; the subscript is ignored.

所以,因为它是不带引号的 - 如果有名为当前目录中的文件此* 然后猛砸会抛出非法的名字错误,由于路径扩展。否则,它只会创建一个名为这个空数组。

So since it's unquoted - if there's a file in the current directory named THIS* then Bash would throw an illegal name error due to the pathname expansion. Otherwise it will just create an empty array named "THIS".

什么 [*] 表示取决于环境。未加引号的在一个普通的指令评估的背景下,这只是一个字符类匹配字面星号。如果一个参数扩展以下数组名使用,它扩展了数组的所有元素,以一个字由IFS的第一个字符分隔。

What [*] means depends upon context. Unquoted in an ordinary command evaluation context, it's a character class matching only literal asterisks. If used in a parameter expansion following an array name, it expands all elements of the array to a single word separated by the first character of IFS.

声明在bash声明变量,返回其值,设置属性,并影响其功能范围。请参阅: http://wiki.bash-hackers.org/commands/builtin/declare

declare in bash declares variables, returns their values, sets attributes, and affects function scope. See: http://wiki.bash-hackers.org/commands/builtin/declare

修改:鉴于现在的问题,在bash的行为是上述的例子。在 [*] 应该从删除声明。这code有一些其他问题,如试图将参数转换为字符串,使用全大写变量名,并使用 [而不是 [在这显然不打算在最小的POSIX sh的运行脚本。

edit: Given the example that's now in the question, in bash the behavior is as described above. The [*] should be deleted from the declare. That code has a number of other issues such as attempting to put arguments into strings, using all-caps variable names, and using [ instead of [[ in a script that's clearly not intended to run on a minimal POSIX sh.

这篇关于庆典,zsh中:声明[*](噢,我的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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