ksh

时间:2017-06-06 11:29:57

标签: ksh

我是这个论坛上的新手 我正在寻找动态修改代码的变量 实施例

#!/bin/ksh -f
VAR1=123456
VAR2=$VAR1
echo VAR1 # Will return '123456', good
echo VAR2 # Will return '123456', good
VAR1=azerty
echo VAR2 # Will return 123456, not good, I hope it was 'azerty', the new value of VAR1

如果有人对此问题有答案或暗示

提前, Antrema

1 个答案:

答案 0 :(得分:0)

尝试使用nameref,例如:

$ VAR1=123456
$ nameref VAR2=VAR1
$ echo "$VAR1 : $VAR2"

123456 : 123456

$ VAR1=azerty
$ echo "$VAR1 : $VAR2"

azerty : azerty

# NOTE: this relationship works in both directions, eg:

$ VAR2=xyz123
$ echo "$VAR1 : $VAR2"

xyz123 : xyz123

请参阅:O'Reilly: Learning the Korn Shell: 4.4 Indirect Variable References