C-Shell:变量中的变量

C-Shell:变量中的变量

我想要 variabe 的值${var}_err_num,但我做不到。

#!/bin/csh -f

set list = "book bus apple"
foeach var $list
   set err_num
   *
   *
   @err_num = $err_num + 1
   set ${var}_err_num = $err_num
end

echo ""
echo "# Summary"
foeach var $list
   echo "$var error num : ${${var}_err_num}" ; # Error occured
end

答案1

根据这个 Stackoverflow 帖子,你需要使用eval.也许:

#!/bin/csh -f
set list = "book bus apple"
set err_num = 0
foreach var ($list)
   @ err_num += 1 
   set ${var}_err_num = $err_num
end

echo ""
echo "# Summary"
foreach var ($list)
   eval echo \$var error num : \$${var}_err_num ; # Error occured
end

(我修复了脚本中的一些语法错误,例如拼写错误foreach等。)

输出:

$ csh -f foo.csh

# Summary
book error num : 1
bus error num : 2
apple error num : 3

相关内容