gpt4 book ai didi

shell - 如何使用变量读取属性

转载 作者:行者123 更新时间:2023-12-04 16:17:56 25 4
gpt4 key购买 nike

过去 2/3 天我一直面临这个问题。我有一个简单的脚本,我将在其中读取属性文件并输出一些内容。这是我的脚本

vi testloop.properties
dump_1=abc


vi testloop.sh
. testloop.properties
i=1
dmp="dump_"$i //as per my understanding dmp=dump_1
echo $dump_1 //abc will echod since dump_1 in property file is abc
echo $dmp ***i thought here also abc will come but unfortunately it is "dump_1"***
echo $(($dmp)) ***here output is 0***


sh testloop.sh
abc
dump_1
0

有人可以帮我指出 echo $dmp 和 echo $(($dmp)) 中发生了什么吗?为什么它不回显 abc

最佳答案

. ./testloop.properties
i=1
dmp="dump_"$i
echo $dump_1 # print abc as expected
echo $dmp # prints dump_1 since this is what dmp contains
# ***here output is 0***
eval echo \$$dmp # evaluates \$$dmp to $dump1 then echoes abc

至于 echo $(($dmp)),它尝试评估 arithmetic expression结果是0:

Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:

$(( expression ))

关于shell - 如何使用变量读取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13678404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com