1

I would like to know how to assign a variable inside if block in shell script..

Below is my code..

if [[ -z "$MMBOX_PATH" || -z "$BACKUP_PATH" || -z "$REMOTE_SERVER" || -z "$LOG_PATH" ]]
then
    echo -e "Must Provide All Required Paths [$FLAG is Empty].."
    exit 1
fi

The above code will run whenever it found empty variable, but I also wants to know which variable is empty (E.g., In above code suppose if LOG_PATH variable is empty then it should display in echo output in place of $FLAG )

I tried following codes..

if [[ `FLAG='MMBOX_PATH'` && -z "$MMBOX_PATH" || `FLAG='BACKUP_PATH'` && -z "$BACKUP_PATH" || `FLAG='REMOTE_SERVER'` && -z "$REMOTE_SERVER" || `FLAG='LOG_PATH'` && -z "$LOG_PATH" ]]
then
    echo -e "Must Provide All Required Paths [$FLAG is Empty].."
    exit 1
fi

But above code returns false hence it is not printing the content inside echo.

I also tried to keep FLAG variable before condition execution, but every time it returns 'Nothing'

if FLAG='MMBOX_PATH' && [[ -z "$MMBOX_PATH" ]]
then
    echo -e "Must Provide All Required Paths [$FLAG is Empty].."
    exit 1
fi

In above case I'm getting FLAG='MMBOX_PATH' in output but if I add one more condition to that if nothing is printing (Means if I check same thing for BACKUP_PATH,REMOTE_SERVER..)

if FLAG='MMBOX_PATH' && [[ -z "$MMBOX_PATH" ]] && FLAG='LOG_PATH' && [[ -z "$LOG_PATH" ]]
then
    echo -e "Must Provide All Required Paths [$FLAG is Empty].."
    exit 1
fi

In this case nothing is printing even though MMBOX_PATH present and LOG_PATH empty.

Note: Using if condition each and every variable it is possible to know which variable is empty,but I don't want to extend my lines with if-else conditions I just want to know in that if block itself how to assign a variable and prints once condition is true.

Can anybody help me how to get empty variable..? (/bin/bash)

2
  • 1
    USe a case statement instead Commented Nov 18, 2015 at 11:44
  • but that is also makes code lengthy. I have to write code for every variable case "$MMBOX_PATH" in "") echo "MMBOX EMPTY" ;; esac I just wanted to use inside if block because we already testing whether a variable is empty or not during that time only I want to assign some other variable to use later. Commented Nov 18, 2015 at 11:50

2 Answers 2

3

If all you are doing is checking existence with the if you could use a function.

check() {
for i in "$@";do
    if [[ -z "${!i}" ]]
    then
        echo -e "Must Provide All Required Paths [\$$i is Empty].."
        exit 1
    fi
done
}

check MMBOX_PATH BACKUP_PATH REMOTE_SERVER LOG_PATH
Sign up to request clarification or add additional context in comments.

2 Comments

What version of bash are you using? bash 4 has the -v operator, which tells you if the variable is set, not just non-zero length: if [[ ! -v $i ]]; then
@chepner I use bash 3.2 as it is the version that comes with sles. No idea about OP.
1

Shell already provides a syntax for verifying that a variable has a value and exits if it does not:

: ${MMBOX_PATH:?Must provide MMBOX_PATH}
: ${BACKUP_PATH:?Must provide BACKUP_PATH}
: ${REMOTE_SERVER:?Must provide REMOVE_SERVER}
: ${LOG_PATH:?Must provide LOG_PATH}

There's no need to define a check function that does the same thing.

The initial colon is the do-nothing command; the shell evaluates its arguments, and : exits with status 0 immediately. The parameter expansion is what verifies that the named parameter has a value. If it does not, the given error message is printed. If the shell is not interactive, it also exits with status 1.

7 Comments

Neither is reimplementing built-in shell features. I'm of the opinion that DRY can be overdone.
i am in favor of it when it means not rewriting the same thing tons of times. What if you want to change the warning message, you would have to change every single variable. The fact that you have included only 3 of the 4 params with a significantly shorter error message may be testament to this. Also the function doesn't mimic shell behaviour, the if statement inside does, and if i rewrote it i would probably use this syntax instead.
How often do you think warning messages for unset parameters actually change? Or the number of arguments to a program? Making a little-touched section of the code easier to read and understand at the expense of a little repetition is not a bad thing.
The number of variables used in a program changes a lot. Especially if the functionality changes. And for each new variable you would have to rewrite the warning. Also this is in no way more readable than the function. In fact i would say it is less readable and less obvious what it is doing. Terser:yes Readable:no
I'm not talking about internal variables, which should be explicitly initialized rather than checked if they are initialized. I'm talking about arguments to the program, which rely on the caller to set. They should remain relatively static. This syntax needs to be learned once. It's valid in every script written in bash, without the need to rewrite your custom function, or add an external dependency in the form of a sourced file for every script that wants to use it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.