If statement advice

Hi all,
Need some advice.

I am attempting to write a if statment, to check a varaibale, but the output of the variable is empty (which is correct), but i am having issues write a if expression for this empty variable.

Can you please advice.

If statement - Output below

  if [[ $FRAG_DUP_LTO5_1 -eq 1 || $FRAG_DUP_LTO5_2 -eq 1 ]];  then
      if [[ $FRAG_LTO3_COPY_1 == "" && $FRAG_LTO3_COPY_2 == "" ]] ; then

Output of variable

+ bpimagelist -backupid ibl_1357326073
+ grep 'FRAG '
+ awk '{print $2,$9}'
+ sort -u
+ awk '{ if (L!=$1) print; L=$1}'
+ egrep -vi '\<C5|\<C6|\<W5|\<W6'
+ awk NR='=1{print $1}'
+ FRAG_LTO3_COPY_1='' 
+ bpimagelist -backupid ibl_1357326073
+ grep 'FRAG '
+ awk '{print $2,$9}'
+ sort -u
+ awk '{ if (L!=$1) print; L=$1}'
+ awk NR='=2{print $1}'
+ egrep -vi '\<C5|\<C6|\<W5|\<W6'
+ FRAG_LTO3_COPY_2=''
+ bpimagelist -backupid ibl_1357326073
+ grep 'FRAG '
+ awk '{print $2,$9}'
+ sort -u
+ awk '{ if (L!=$1) print; L=$1}'
+ egrep -i '\<C5|\<C6\<W5|\<W6'
+ awk NR==1
+ wc -l
+ FRAG_DUP_LTO5_1=0
+ bpimagelist -backupid ibl_1357326073
+ grep 'FRAG '
+ awk '{print $2,$9}'
+ sort -u
+ awk '{ if (L!=$1) print; L=$1}'
+ egrep -i '\<C5|\<C6\<W5|\<W6'
+ wc -l
+ awk NR==1
+ FRAG_DUP_LTO5_2=0
+ echo ''

Output of the if statement

+ [[ 0 -eq 1 ]]
+ [[ 0 -eq 1 ]]
+ [[ '' -eq 0 ]]

Try using -z in your if statement to check for a null value in a variable:

if [ -z $VARIABLE ]
1 Like

Hello,

You can use the man test command to check further about the same.

if [[ -z $variable ]] ## Will be true in case variable having no value/values.
 
OR
 
if [[ -n $variable ]] ## Will be true in case variable having value/values.

Also it is be adviced to keep always values variables not hardcoded. Hardcoding the values may cause the issue in case you need to change their values later in script.

Thanks,
R. Singh

1 Like