0

I'm comparing information between a csv database and PHP table. There are some instances where the PHP table has 'blank' or 'empty' value.

How can I make sure that the comparisons in my if statement actually compare two values as opposed to one value being compared to a 'blank' value?

This is the snippet of code

if [ "$CSV_PartDir" == "$PHP_PartDir" ] && [ "$CSV_PartName" == "$PHP_PartName" ]
then
    echo "MATCH:[$direction / $PHP_PartDir]  PN: [$part_name / $PHP_PartNam]"
    match=$((match+1))
elif [ "$CSV_PartDir" != "$PHP_PartDir" ] && [ "$CSV_PartName" != "$PHP_PartName" ]
then 
    echo "WRONG: [$direction / $PHP_PartDir]  PN: [$part_name / $PHP_PartNam]"
    mismatch=$((mismatch+1))
fi

This is a sample OUTPUT:

MATCH:[OUT / OUT]  PN: [JSP-015123 / JSP-015123]
MATCH:[OUT / OUT]  PN: [02118 / 02118]
MATCH:[OUT / OUT]  PN: [02132 / 02132]
MATCH:[OUT / OUT]  PN: [JSP-015123 / JSP-015123]
MATCH:[OUT / OUT]  PN: [JSP-015123 / JSP-015123]
WRONG: [OUT / ]  PN: [02132 / ]
WRONG: [OUT / ]  PN: [02132 / ]
WRONG: [OUT / IN ]  PN: [OH-VPS-023 / JSP-01512 ] <--- a "legitimate" comparison to be define as "wrong"
WRONG: [OUT / ]  PN: [02132 / ]   <--- NOT a "legitimate" comparison to be define as "wrong"

Q: Is there a certain way of checking that the PHP_PartName and/or PHP_PartDir actually holds a value as opposed nothing within the if.

Ideally I would like to print out "wrong" or "mismatch" should my PHP values and CSV values don't match. So I would like to suppress the empty values.

Barmar
  • 741,623
  • 53
  • 500
  • 612
3kstc
  • 1,871
  • 3
  • 29
  • 53
  • possible duplicate of [how to find variable is empty or "" in shell script](http://stackoverflow.com/questions/3061036/how-to-find-variable-is-empty-or-in-shell-script) – l'L'l Aug 04 '15 at 02:08
  • You want to say `MATCH` if one of them is blank, instead of `WRONG`? – Barmar Aug 04 '15 at 02:08

2 Answers2

0

Read up on the test command, specifically the -z test for null, and the -n test for NOT NULL.

http://wiki.bash-hackers.org/commands/classictest

Gary_W
  • 9,933
  • 1
  • 22
  • 40
0

Use the syntax for substituting a default value if a variable is unset or null, and use the value of the variable you're comparing with as the default:

if [ "${CSV_PartDir:-$PHP_PartDir}" = "${PHP_PartDir:-$CSV_PartDir}" ] && [ "${CSV_PartName:-$PHP_PartName}" = "${PHP_PartName:-$CSV_PartName}" ]

So if one of the variables is is empty, it will default to the other variable, and they'll match instead of saying WRONG.

Barmar
  • 741,623
  • 53
  • 500
  • 612