0

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 ?

6
  • 1
    That error comes from the original attempt to define C, not your attempt to check if C is empty; it means either A or B is empty. Commented May 25, 2020 at 12:53
  • @chepner expr is not able to "detect" it ? Like assigning a default value to an empty variable ? Commented May 25, 2020 at 12:54
  • 1
    No; parameter expansion happens before expr runs, so when A is empty, expr $A - $B is equivalent to expr - 1590414895. expr has no idea that what the original expression looked like before parameter expansion. Commented May 25, 2020 at 12:56
  • You could use C=$(( A - B )), which would let the arithmetic expression "expand" A and use a default value of 0 instead of an empty string. Commented May 25, 2020 at 12:58
  • The question is: do you want A to be 0 if it wasn't set? Commented May 25, 2020 at 12:59

2 Answers 2

1

The error comes from expr itself, because A has no value, and the resulting command is expr - 1590414895. You are seeing the error before trying to check if C is empty (which indeed it is, because expr never produced any output).

Instead of using expr (which is virtually never needed), use an arithmetic expression. It will expand string values as if they were parameters, with the side effect of treating null or unset names as having a value of zero.

C=$(( A - B ))
Sign up to request clarification or add additional context in comments.

3 Comments

beware of Integer Overflow
Is there any benefit when writing C=$(( A - B )) instead of ((C = A - B))?
Aside from POSIX compatibility, probably not. If you are using set -e, be aware that the exit status of ((C = 0)) is 1, while the exit status of C=$((0)) is 0.
0

you can use parameter substitution and set a default value ${parameter:-default}

C=$(expr ${A:-0} - ${B:-0})
(( $C )) && echo -e "different"

(the other stuff is just playing, basically more posix conform and not related to bash)

because -z negated with ! will extinguish itself you can just skip both test conditions.

for arithmetic tests double parentheses can be used instead of (double) brackets:
(( $C == 1 )) is just a test like [ "$C" = "1" ] returning true or false.

as one liner for if..[]..then..else..fi conditional evaluation can be used []..&&..||
&& <cmd> is executed when [] returned true, || <cmd> is executed when [] returned false

1 Comment

Please add some explanation to your answer such that others can learn from it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.