dimanche 12 mai 2019

Bash: How to build a string to get output from a string of a certain name

This is totally a noob question, but in a bash script, I have a code returned from the output of another program in ipsumCode, and customised explanatory notes corresponding to each code (there are hundreds) set as a string to make for easy editing.

Instead of relying on if statements for each code and building a custom note for each there, I'm trying to reduce it all down to one if test that can display the appropriate output.

#!/bin/bash

identifier="FooBar"

#...
msg_456="Some description here."
#...
msg_599="A different description here."
msg_600="Yet another different description here."
#...

# $ipsumCode is set by output from another program, but hard-code it here to test
ipsumCode=599
ipsumMessage=msg_$ipsumCode

#...

if [ "$ipsumCode" -ge 456 ]; then
    printf "$identifier: $ipsumMessage\n"
#...
fi

Since ipsumCode is 599 in this instance, how do I display the contents of msg_599?

Above displays: "FooBar: msg_599" of which msg_599 is what I need, but it's not the contents of that string.

I've looked several similar questions and answers, but all deal with combining outputs in various ways, rather than building a string name from another string and outputting the contents of the matching string.

Aucun commentaire:

Enregistrer un commentaire