LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: Checking if a parameter is set, by function call. (https://www.linuxquestions.org/questions/programming-9/bash-checking-if-a-parameter-is-set-by-function-call-4175591862/)

nbritton 10-20-2016 05:11 PM

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 () {

    if [[ -z ${!1} ]]; then
        echo "Parameter $1 is not set, skipping step.";
        return 1;
    fi

}

check_parameter PARAMETER || return 1;


keefaz 10-21-2016 08:56 AM

What do you not like with your check_parameter function?

nbritton 10-21-2016 09:02 AM

Quote:

Originally Posted by keefaz (Post 5620975)
What do you not like with your check_parameter function?

It's not so much that I don't like it, I'm just not sure if it's the right approach and I'm wondering if there is a simpler or more elegant way to do this in bash. I was trying variable substatution (i.g. ${var:?}) and [[ -v \$$var ]], [[ -z \$$var ]], [[ -n \$$var ]], [[ -v $(\\$var) ]], etc. but I could not get any of those to work. I was banging my head against a wall for a couple hours trying to find a solution for something I thought was very simple, so that made me question if I was trying to go about it the wrong way.

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 () {

    ## Adaptec ASR8805.
    SUPPORTED_CONTROLLERS_ADAPTEC_ASR8805="ASR8805";
    ## Adaptec ASR8885.
    SUPPORTED_CONTROLLERS_ADAPTEC_ASR8885="ASR8885";

    PRODUCT=$( arcconf getconfig ${CONTROLLER_ID} ad | grep "Model" | awk -F ':' '{print $NF}' | sed 's/^\ //' );

    FW_DIRECTORY=adaptec;

    ## Adaptec ASR8805:
    if [[ $PRODUCT =~ $SUPPORTED_CONTROLLERS_ADAPTEC_ASR8805 ]]; then
      check_parameter FW_ADAPTEC_ASR8805_VERSION || return 1;
      arcconf_firmware_update $FW_ADAPTEC_ASR8805_VERSION $FW_ADAPTEC_ASR8805_FILE;
    fi

    ## Adaptec ASR8885:
    if [[ $PRODUCT =~ $SUPPORTED_CONTROLLERS_ADAPTEC_ASR8885 ]]; then
      check_parameter FW_ADAPTEC_ASR8885_VERSION || return 1;
      arcconf_firmware_update $FW_ADAPTEC_ASR8885_VERSION $FW_ADAPTEC_ASR8885_FILE;
    fi

}


grail 10-21-2016 09:40 AM

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?

rigor 10-21-2016 02:59 PM

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";
or even:
Code:

SUPPORTED_CONTROLLERS="ASR8805,ASR8885";
If there are controllers made by different manufacturers involved, and there is some concern that two manufacturers might happen to use the same model ID, then maybe something like this might work, creating a single overall list that has labeled sub-sections:
Code:

SUPPORTED_CONTROLLERS="ADAPTEC:ASR8805,ASR8885;JOEBOBS:AK47,AR1;MA_AND_PA:APPLE_PI_314,CHERRY_PI_159";
If something like awk might be run repeatedly during some overall procedure, and there's any significant amount of data, then if the choices were amongst bash, grep, awk, and sed, I might be tempted to write the majority of the program code in some form of awk ( perhaps gawk if it's available ) .

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.