【问题标题】:KSH Shell Double Variable names?KSH Shell 双变量名称?
【发布时间】:2012-09-19 21:48:30
【问题描述】:

我正在尝试基于 $i 创建一个变量

i=1
line="one two three four five six"

while [[ $i -lt 3 ]]; do 
set string$i=`echo $line | cut -d" " -f1-3`
echo $string$i
do_stuff_here
done

当我这样做时,我得到以下输出

1

预期的输出是

one two three

事实上,当我回显 $String1 时……我得到了预期的输出……所以它的存储正确。 我知道它是我调用 $string$1 的方式......但我尝试了各种引号/括号,但它不起作用。谁能告诉我如何调用我的变量?

【问题讨论】:

  • $string$1$string$i 的拼写错误吗?
  • @chepner 是的……谢谢

标签: shell variables set ksh typeset


【解决方案1】:

除非您在 do_stuff_there 中增加 i,否则您的 while 循环永远不会停止,无论如何,这应该更接近您的预期:

i=1
line="one two three four five six"

while [[ $i -lt 3 ]]; do
  eval string$i=\"`echo $line | cut -d" " -f1-3`\"
  eval echo \$string$i
  do_stuff_here
done

【讨论】:

    猜你喜欢
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 2020-12-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多