How to check if multiple variables are defined or not in bash

前端 未结 3 1199
情话喂你
情话喂你 2021-01-31 08:19

I want to check, if multiple variable are set or not, if set then only execute the script code, otherwise exit.

something like:

if [ ! $DB==\"\" &&a         


        
3条回答
  •  情话喂你
    2021-01-31 08:59

    You can check it also by put the variables name in a file

    DB=myDB
    HOST=myDB
    DATE=myDATE
    

    then test them if currently empty or unset

    #!/bin/bash
    while read -r line; do
        var=`echo $line | cut -d '=' -f1`
        test=$(echo $var)
        if [ -z "$(test)" ]; then 
            echo 'one or more variables are undefined'
            exit 1
        fi
    done 

提交回复
热议问题