I was in a situation where I had to find a simple way to a variable mapping.
The script whatIs would have to translate the received parameter into the predefined values within the script.
I wanted something straightforward, and didn't want to do a series of ifs to compare the contents.
The easiest solution I found was to simply use 'eval' to retrieve the matching value of the passed parameter and provide me with the correlated value.
whatIs:
orange="fruit" potato="vegetable" cod="fish" eval "v=\$$1" echo $v
Call script whatIs with parameter orange and the output will be the value assigned to orange in the script.
whatIs orange > fruit
As for my use case, I would then use the output ( in this example: fruit ) as a variable name
and use in my program.
Comments
Post a Comment