热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ZSHfor循环数组变量问题-ZSHforlooparrayvariableissue

ImworkinginZSH,butImsureBASHinstructionswillprobablyhelpalso.Ineedtohaveaforloop

Im working in ZSH, but Im sure BASH instructions will probably help also. I need to have a for loop that goes thru the values stored in the array lw and then launches a shell script, based on the name stored in the array.

我在ZSH工作,但我确信BASH指令也可能有所帮助。我需要一个for循环,通过存储在数组lw中的值,然后根据存储在数组中的名称启动一个shell脚本。

So far, this is what I've come up with:

到目前为止,这是我提出的:

$lw=('plugin1' 'plugin2' 'plugin3')

for i in $lw;
  do . ~/Library/Rogall/plugins/$lw[$i]/lw.prg end;
done

it just gives me an error when run saying that it can't find ~/Library/Rogall/plugins//lw.prg, so it appears its just ignoring my variable all togethor.

它只是给我一个错误,当运行说它找不到〜/ Library / Rogall / plugins // lw.prg,所以它似乎只是忽略了我的变量all togethor。

Any ideas where I've messed up?

我搞砸了的任何想法?

2 个解决方案

#1


31  

It's actually much simpler than that:

它实际上比这简单得多:

lw=('plugin1' 'plugin2' 'plugin3')

for i in $lw; do
  . ~/Library/Rogall/plugins/$i/lw.prg end
done

In summary:

综上所述:

  • Assign to "foo", not "$foo" (the shell would try to expand $foo and assign to whatever it expands to; typically not useful)
  • 分配给“foo”,而不是“$ foo”(shell将尝试扩展$ foo并分配给它扩展到的任何内容;通常没有用)
  • Use the loop variable directly; it contains the array value rather than the index
  • 直接使用循环变量;它包含数组值而不是索引

#2


15  

Why bother using the array? This can be done in portable sh very easily:

为什么要使用这个阵列呢?这可以很容易地在便携式中完成:

lw='plugin1 plugin2 plugin3'

for i in $lw;
  do . ~/Library/Rogall/plugins/$i/lw.prg end
done

Note that for this to work in zsh, you need to make zsh do the right thing with: set -o shwordsplit

请注意,要使其在zsh中工作,您需要使用以下命令使zsh做正确的事:set -o shwordsplit


推荐阅读
author-avatar
手机用户2502897397
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有