简体   繁体   English

我可以在bash shell中对变量执行for循环吗?

[英]Can I do a for loop over variables in the bash shell?

I'm learning the shell, and I want to be able to loop over some variables. 我正在学习shell,我希望能够遍历一些变量。 I can't seem to find anywhere where anyone has done this so I'm not sure it's even possible. 我似乎找不到任何人做过这个的地方,所以我不确定它是否可能。

Basically I just want to save myself trouble by using the same sed command on each of these variables. 基本上我只想通过对每个变量使用相同的sed命令来省去自己的麻烦。 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

here is the error: 这是错误:

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

Your problem isn't with the loop, it's with the assignment. 你的问题不在于循环,而在于赋值。 The variable name needs to be literal in an assignment, ie you can write title=some_value but not $arg=some_value . 变量名在分配中需要是文字的,即你可以写title=some_value而不是$arg=some_value

A portable way to assign to a variably-named variable is to use eval . 分配给可变命名变量的可移植方法是使用eval You also need to obtain the value of $arg (not just the value of arg , which is $arg ), which again requires using eval . 您还需要获得价值$arg (不只是价值arg ,这是$arg ),而这又需要使用eval

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

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. 另一种分配给特定于bash / ksh / zsh但在普通sh中不起作用的变量命名变量的方法是使用内置的typeset In bash, if you do this in a function, this makes the assignment local to the function. 在bash中,如果在函数中执行此操作,则会使赋值对函数本地化。 To obtain the value of the variably-named variable, you can use ${!arg} ; 要获取变量命名变量的值,可以使用${!arg} ; this is specific to bash. 这是针对bash的。

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

Other problems with your snippet: 您的代码段的其他问题:

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

If you're relying on bash/ksh/zsh, you can make the replacements inside the shell with the ${VARIABLE//PATTERN/REPLACEMENT} construct. 如果您依赖于bash / ksh / zsh,则可以使用${VARIABLE//PATTERN/REPLACEMENT}构造在shell中进行${VARIABLE//PATTERN/REPLACEMENT}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM