我可以在bash shell中对变量进行for循环吗? [英] Can I do a for loop over variables in the bash shell?

查看:69
本文介绍了我可以在bash shell中对变量进行for循环吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习shell,并且希望能够遍历一些变量.我似乎找不到任何人能做到这一点的地方,所以我不确定这是否有可能.

I'm learning the shell, and I want to be able to loop over some variables. I can't seem to find anywhere where anyone has done this so I'm not sure it's even possible.

基本上,我只想通过对每个变量使用相同的sed命令来避免麻烦.但是,代码显然不起作用.我的问题是,是否可以遍历变量?如果没有,该如何做?

Basically I just want to save myself trouble by using the same sed command on each of these variables. However the code obviously doesn't work. My question is, is it possible to loop over variables and if not how should I be doing this?

title="$(echo string1)"
artist="$(echo string2)"
album="$(echo string3)"

for arg in title artist album do
    $arg="$(echo "$arg" | sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g')"
done

这是错误:

line 12: syntax error near unexpected token `$arg="$(echo "$arg" | sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g')"'

推荐答案

您的问题不在于循环,而在于分配.变量名称在赋值中必须为文字,即您可以编写 title = some_value ,但不能编写 $ arg = some_value .

Your problem isn't with the loop, it's with the assignment. The variable name needs to be literal in an assignment, i.e. you can write title=some_value but not $arg=some_value.

分配给变量命名变量的一种便携式方法是使用 eval .您还需要获取 $ arg 的值(而不仅仅是 arg 的值,它是 $ arg 的值),再次需要使用<代码>评估.

A portable way to assign to a variably-named variable is to use eval. You also need to obtain the value of $arg (not just the value of arg, which is $arg), which again requires using eval.

new_value="$(eval printf %s \"\$$arg\" | …)"
eval $arg=\$new_value

分配给特定于bash/ksh/zsh但在普通sh中不起作用的变量命名变量的另一种方法是使用内置的 typeset .在bash中,如果您在函数中执行此操作,则会使分配在函数中成为本地.要获取可变名称变量的值,可以使用 $ {!arg} ;.这是bash特有的.

Another way to assign to a variably-named variable that's specific to bash/ksh/zsh but won't work in plain sh is to use the typeset built-in. In bash, if you do this in a function, this makes the assignment local to the function. To obtain the value of the variably-named variable, you can use ${!arg}; this is specific to bash.

typeset $arg="$(printf %s "${!arg}" | …)"

您的代码段存在其他问题:

Other problems with your snippet:

  • title ="$(echo string1)" 是一种编写 title ="string1" 的复杂方法,而且可能会破坏 string1 如果包含反斜杠或以-开头.
  • do 关键字之前,您需要一个命令终止符(; 或换行符).
  • title="$(echo string1)" is a complicated way to write title="string1", which furthermore may mangle string1 if it contains backslashes or begins with -.
  • You need a command terminator (; or newline) before the do keyword.

如果您依赖bash/ksh/zsh,则可以使用 $ {VARIABLE//PATTERN/REPLACEMENT} 构造在shell内进行替换.

If you're relying on bash/ksh/zsh, you can make the replacements inside the shell with the ${VARIABLE//PATTERN/REPLACEMENT} construct.

title="string1"
artist="string2"
album="string3"
for arg in title artist album; do
  eval value=\$$arg
  value=${value//&/&amp;}
  value=${value//</&lt;}
  value=${value//>/&gt;}
  eval $arg=\$value
done

这篇关于我可以在bash shell中对变量进行for循环吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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