I have a variable that is the result of an expression. I want to check if this varaible is empty or not.
Here are two variables:
A=$(cat ${FICHIER_BROKERS} | grep ${DATABASE_NAME} | grep dbo.${TABLE_NAME}\ | awk '{print $4}')
B=$(cat ${FICHIER_SUBSCRIBERS} | grep ${DATABASE_NAME} | grep dbo_${TABLE_NAME}_CT | awk '{print $4" "$5}' | sed "s/....$//" | sed "s/[-:]/ /g" | awk '{print mktime($1" "$2" "$3" "$4" "$5" "$6)}')
The result of A is empty, because in the awk statement, there is not fourth element (expected).
Now I have this variable that calculates timestamp difference:
C=$(expr $A - $B)
So far, the variables content are:
echo "A $A"
echo "B $B"
echo "C $C"
Output:
A
B 1590414895
C
I would like to check if C is empty or not. Later on, I need to execute checks (greater than) on it and an empty value returns [: : integer expression expected
To check if the variable is empty, I have tried the following:
if [[ ! -z $C ]]; then
Throws error:
expr: syntax error
if [[ "$C" != "" ]]; then
Throws error:
expr: syntax error
How can I perform a check on this value ?
C, not your attempt to check ifCis empty; it means eitherAorBis empty.expris not able to "detect" it ? Like assigning a default value to an empty variable ?exprruns, so whenAis empty,expr $A - $Bis equivalent toexpr - 1590414895.exprhas no idea that what the original expression looked like before parameter expansion.C=$(( A - B )), which would let the arithmetic expression "expand"Aand use a default value of0instead of an empty string.Ato be 0 if it wasn't set?