shell - KSH Shell 双变量名称?

标签 shell variables set ksh typeset

我正在尝试创建一个基于 $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

事实上,当我 echo $String1...我得到了预期的输出...所以它存储正确。 我知道这是我调用 $string$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

关于shell - KSH Shell 双变量名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12503406/

相关文章:

variables - Rust 服务器必须保存变量的更新值

Python 打印不带括号的集合列表

c++ - 如何从 C++ 中的 set<int> 中删除所有偶数

shell - 基诺数解析器

linux - 重新处理文本文件以将包含回车符 (CR) 字符的行合并到屏幕结果中

javascript - 更改子局部函数javascript中的全局变量的值

创建集合的 Python 性能比较 - set() 与 {} 文字

php - 树莓派相机流媒体不会从 php 开始

unix - shell命令相当于按键盘上的回车键

javascript - 如何在bxslider中制作变体显示幻灯片?