我正在尝试创建一个脚本,其中差不多事物得到较早整理的在其中,我留下了需要存储在变量中的布尔值。我只需要把它们运行最后一个循环就完成了
我所说的最后一部分是类似于以下内容的可读性较差的版本:
[↑continues on top↑]
var5="$(some long operation that has different results on different systems )"
var6="$(bc of escaping or slighly different variations on the basic set of )"
var7="$(UNIXy commands preloaded on every system's minimal installation )"
var8="$(e.g: <grep> Um…what else…oh yeah! The output of these is 1 or 0
答案1
使用数组代替编号变量:
entries=(
"$(some long operation that has different results on different systems )"
"$(bc of escaping or slighly different variations on the basic set of )"
"$(UNIXy commands preloaded on every system's minimal installation )"
"$(e.g: <grep> Um…what else…oh yeah! The output of these is 1 or 0
答案2
您应该使用${!name}
变量间接语法。
Bash 使用由其余参数形成的变量值作为变量的名称
所以这会起作用:
varnames='var1 var2 ... var8'
for varname in $varnames
do
if [ "${!varname}" ]
...
至于可移植性,这取决于每个环境中的 bash 版本。