bash - 换人失败
问题描述
我正在尝试使用另一个变量的值创建一个变量名。首先,我正在编写的其他代码示例确实有效:
#!/bin/bash
j=0
let "AVG_T_${j}"+="(${TEMPS[$j]})" #Works
let "tLen"="(${#AVG_T_0[@]})" #Works
tLen=${#AVG_T_0[@]} #Works
let "AVG_T_0=${AVG_T_0[@]:1}" #Works
let "AVG_T_${j}"="${AVG_T_0[@]:1}" #Works
这就是我遇到的麻烦:
#!/bin/bash
j=0
"tLen"="(${#AVG_T_${j}[@]})" #Bad Substitution
let "AVG_T_${j}"="${AVG_T_${j}[@]:1}" #Bad Substitution
我不知道有什么办法让它工作。我尝试了许多不同的语法、括号、没有等等。我不知道从这里到哪里去。
谢谢
这就是我现在的位置。该代码应该检查温度区的温度,然后给出电流输出和平均输出。
j=0
TEMPS=()
AVGtn=5
DIRECTORY="/sys/class/thermal/thermal_zone"${j}
while [ -d "$DIRECTORY" ];
do
eval "AVG_T_${j}_TOTAL="""
TEMPS+=($(cat $DIRECTORY/temp))
TEMPS[$j]=$(convert ${TEMPS[$j]})
eval "AVG_T_${j}+=(\"\${TEMPS[${j}]}\")"
eval "tLen=\${#AVG_T_${j}[@]}" #Works
if [ $tLen -gt $AVGtn ]; then
eval "AVG_T_${j}=(\"\${AVG_T_${j}[@]:1}\")"
eval "tLen=\${#AVG_T_${j}[@]}"
AVG_T=()
eval "AVG_T+=(\"\${AVG_T_${j}[@]}\")"
for i in ${AVG_T[@]}; do
let "AVG_T_${j}_TOTAL"+=${i}
done
eval "x=\${AVG_T_${j}_TOTAL}"
let "AVG_TMP_${j}=(1000*($x/$tLen)+5)/1000"
fi
let "j++"
DIRECTORY="/sys/class/thermal/thermal_zone"${j}
sleep 3
done
这就是我最终得到的结果。我知道它可以被收紧和清理,但我很高兴有工作代码。也许它会帮助像我这样的其他外行继续前进。
j=0
TEMPS=()
DIRECTORY="/sys/class/thermal/thermal_zone"${j}
while [ -d "$DIRECTORY" ];
do
# Put TOTAL to null
eval "AVG_T_${j}_TOTAL="""
# Add one directory temp to TEMPS
TEMPS+=($(cat $DIRECTORY/temp))
# Convert TEMP to Celsius
TEMPS[$j]=$(convert ${TEMPS[$j]})
# Put the converted TEMP into AVG_T_n
eval "AVG_T_${j}+=(\"\${TEMPS[${j}]}\")"
# Get the current Length of AVG_T_n
eval "tLen=\${#AVG_T_${j}[@]}"
# If the len of AVG_T_n is greater than AVGtn
if [ $tLen -gt $AVGtn ]; then
# Take Oldest temp out of Array
eval "AVG_T_${j}=(\"\${AVG_T_${j}[@]:1}\")"
# Reset tLen Length to the new length
eval "tLen=\${#AVG_T_${j}[@]}"
fi
# Clear AVG_T
AVG_T=()
# Get an AVG_T_${j} duplicate duplicate from eval named AVG_T so we can use it as a condition that the for loop understands
eval "AVG_T+=(\"\${AVG_T_${j}[@]}\")"
# Add all of AVG_T values together and put into AVG_T_n_TOTAL
for i in ${AVG_T[@]}; do
let "AVG_T_${j}_TOTAL"+=${i}
done
# Ceate a variable with eval so that we can use it in the formula that provides the average temperature
eval "x=\${AVG_T_${j}_TOTAL}"
# Create the AVG_TMP_n
eval let "AVG_TMP_${j}=\"(1000*(\$x/\$tLen)+5)/1000\""
# Convert the AVG_TMP_n to Fahrenheit
eval let "AVG_TMP_${j}f=\"\$(convertc2f AVG_TMP_${j})\""
# Create another variable for echo for testing
let "j++"
DIRECTORY="/sys/class/thermal/thermal_zone"${j}
done
解决方案
我们可以使用eval
.
#!/bin/bash
set -exu
declare -a AVG_T_0=(first second third fourth)
j=0
eval "tLen=\${#AVG_T_${j}[@]}"
eval "AVG_T_${j}=(\"\${AVG_T_${j}[@]:1}\")"
typeset -p tLen
typeset -p AVG_T_0
typeset -p "AVG_T_${j}"
推荐阅读
- c# - 从 c# 代码签名时,php 中的 RSA 签名验证不起作用
- google-maps - 使用边界选项限制 Google 自动完成结果
- java - java.lang.ArrayIndexOutOfBoundsException - 数组是二维的且不为空
- python - 无法在批处理文件中运行 python 脚本 - 可能的 Bootcamp 问题
- swiftui - SwiftUI 回到sceneWillEnterForeground SceneDelegate.swift 中的RootView
- python - 如何在python 3的正则表达式中包含字典中的键和值?
- javascript - React Native:如何防止状态变量在热重载时被删除?
- r - rvest 以前工作现在返回空内容
- java - libgdx:使用多个瓦片集时瓦片地图渲染不正确
- json - 更新 Firebase 的分支导入 Json 文件而不创建另一个分支