LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2016, 05:11 PM   #1
nbritton
Member
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 89

Rep: Reputation: Disabled
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;

Last edited by nbritton; 10-20-2016 at 05:18 PM.
 
Old 10-21-2016, 08:56 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
What do you not like with your check_parameter function?
 
Old 10-21-2016, 09:02 AM   #3
nbritton
Member
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 89

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
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

}

Last edited by nbritton; 10-21-2016 at 09:21 AM.
 
Old 10-21-2016, 09:40 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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?
 
Old 10-21-2016, 02:59 PM   #5
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH:Rewrite a function using parameter expansion instead of changing IFS Mes9 Programming 7 09-26-2012 02:50 PM
global variable in bash function call mvairavan Programming 3 07-18-2012 09:44 PM
Compilation issue when Function is parameter in function call on LINUX sa20358 Linux - Software 2 07-24-2008 10:19 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
Bash function parameter Misel Programming 2 05-17-2003 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration