In my script I have an unknown number of variables containing angles which I want to convert to vectors. I made the variable-names such that the angles of each 'parameter' are of the form: {parameter}_angle_{lat/perp} Hence, each parameter has a 'lat' and 'perp' angle variable. So what I want to do is to find all variables containing '_angle_lat', do some calculations on the values of these variables and create a new variable which contains the 'parameter'-name in it. for example:
export M0_angle_lat=4
export M1_angle_lat=70
export M1_angle_perp=8
export M0_angle_perp=90
# Now I want to use these values to calculate vectors
for varname in *_angle_lat
do
# at first iteration it will get for example "varname=M0_angle_lat" in which case
# I want it to do:
M0_X=$(( $M0_angle_lat * $M0_angle_perp ))
# The second iteration in case would then give "varname=M1_angle_lat" in which case
# I want it to do:
M1_X=$(( $M1_angle_lat * $M1_angle_perp ))
done
I hope it's clear what my goal is here. Thanks for the help!
What you can do is use env
to get a list of all variables and then iterate through them:
while IFS='=' read -r name value ; do
if [[ $name == *'_angle_lat'* ]]; then
echo "$name" ${!name}
prefix=${name%%_*} # delete longest match from back (everything after first _)
angle_lat="${prefix}_angle_lat"
angle_perp="${prefix}_angle_perp"
result="${prefix}_X"
declare "${result}=$(( ${!angle_lat} * ${!angle_perp} ))"
fi
done < <(env)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With