Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell ASH将两个变量命名为变量名_Shell_Ash - Fatal编程技术网

Shell ASH将两个变量命名为变量名

Shell ASH将两个变量命名为变量名,shell,ash,Shell,Ash,所以我正在制作一个脚本,允许您输入一个数字,导出它,然后导入它并在循环中使用它。我的意思是: read NumberOfVMs "How many do you want? " echo $NumberOfVMs > /variables/NumberOfVMs 后来, while [ $NumberOfVMs -gt 0 ];do # This is the loop I use to repeat the effects I want. # This method

所以我正在制作一个脚本,允许您输入一个数字,导出它,然后导入它并在循环中使用它。我的意思是:

read NumberOfVMs "How many do you want? "
echo $NumberOfVMs > /variables/NumberOfVMs
后来,

while [ $NumberOfVMs -gt 0 ];do
    # This is the loop I use to repeat the effects I want. 
    # This method works fine for me.
    NumberOfVMs=$((NumberOfVMs-1))
done
但是,让我感到困惑的是,我需要使用按数字列出的变量(基于$NumberOfVMs等于多少。我还想将数字归零到四个0。我知道我可以通过执行
$(printf%04g$NumberOfVMs)
来归零

例如,我希望在提问时能够生成3个变量(分别在变量名称的末尾添加0001、0002和0003)

while [ $NumberOfVMs -gt 0 ];do          
    read -p "Enter percentage of RAM to allot GuestOS (1-99): " percentram$(printf %04g $NumberOfVMs)
    NumberOfVMs=$((NumberOfVMs-1))
done
而且,虽然我确实相信(我可能错了)正在编写percentram0001,但我不知道在将变量用作变量时如何动态使用它
percentram$(printf%04g$NumberOfVMs)
不等于
percentram0001
,而是等于添加了0001的
percentram
的输出

请,如果你能帮助我,我将永远爱你。

你可以使用eval hack:

NumberOfVMs=10
read -p "Enter percentage of RAM to allot GuestOS (1-99): " count
eval "percent$(printf %04g $NumberOfVMs)=$count"
echo $percent0010

你在使用bash。你有数组。使用数组。-这个链接有相同的信息。我实际上在使用ash,只是标记为bash,因为它更常见等等…但是我如何将它再次读为动态的呢?我现在知道如何设置$percent0010,但是如何读取$percent$(printf%04g$NumberOfVMs)(在本例中为%0010)基于虚拟机的数量?同样的方法也适用:)eval“somevar=\$percent$(printf%04g$NumberOfVMs)”尽管如此,这是一种非常有攻击性的方法,但最好使用数组(常规数组或关联数组):或者。语法有点毛茸茸的,我几乎总是要查一下……很好。:)谢谢最后一个问题-我有一个部分,我必须使用
echo$percent>/variables/percent
导出到文件中,然后导出
percent='cat/variables/percent'
再次导入命令。。。我如何使用这个黑客来实现它?我知道我使用了问题中提到的循环。。。但是现在我如何使用这个方法来知道我在说什么变量呢?