Bash: Checking if a parameter is set, by function call.
I'm looking for a better way to check if a parameter variable is defined and also if it contains a value. I'll need to be able to check possibly hundereds of parameters throughout the code so the solution must be compact and easy to read. Below is what I have working at the momment. Notice that I need to display an error message that states which parameter is not set.
Code:
check_parameter () { |
What do you not like with your check_parameter function?
|
Quote:
Below is an example of the context that the check_parameter function would be used in. It has to check that the parameter is set, if it's not set then I need it to skip this step and also tell the user presicely which parameter is not set in the log output. Code:
storage_controller_firmware_adaptec () { |
Looking at the example I am guessing all the variables not being set are global?
You do mention that variable substitution was not working, but we would need more information or examples to be able to assist with that. You have also mentioned logging which I do not believe any substitution has the ability to do on its own so your function is probably the best alternative. 2 points on your code: 1. grep/awk/sed is not required as single awk/sed can perform all functions 2. FW_DIRECTORY is never used I am kind of curious though, why is it that the specific, in this case, Adaptec controllers be set within the function and not the version or file details? |
If there is a better way to approach the overall situation, it might be at a higher level; for example I would wonder why aren't some sort of arrays, lists, etc. being used? If such things aren't formally available at some level of the environment that's in use, perhaps they could be done in a more or less synthetic fashion ala A="element_1,element_2".
But as has already been mentioned, more details would be helpful. Looking at just the snippet of code that's been provided, I have to wonder why not: Code:
SUPPORTED_CONTROLLERS_ADAPTEC="ASR8805,ASR8885"; Code:
SUPPORTED_CONTROLLERS="ASR8805,ASR8885"; Code:
SUPPORTED_CONTROLLERS="ADAPTEC:ASR8805,ASR8885;JOEBOBS:AK47,AR1;MA_AND_PA:APPLE_PI_314,CHERRY_PI_159"; Finally, if other languages could be used, if the program is to be written for a specific environment, and expected to remain there, and there is a *lot* of data, I would probably use a language that actually compiles to an executable binary, if that's a possibility in this case. But without all the details, we can just speculate about different possibilities. |
All times are GMT -5. The time now is 01:48 AM. |