Link to home
Start Free TrialLog in
Avatar of pavelmed
pavelmed

asked on

compare variables in UNIX shell

I am trying to compare two variables in shell script, but it does not work although the code looks simple.
Basically the idea is to determine when an empty string is entered by a user and display a warning.
This is the code:
#!/bin/sh
echo "Enter value A:"
read  VALUEA
echo "$VALUEA"

echo "Enter VALUE B:"
read VALUEB
echo "$VALUEB"

X="5"
echo "$X"

if [ "$X$VALUEA"="$X" ];  then
 echo "You did not enter Value A.  Please try again $X$VALUEA and $X"
else
 echo "not empty"
fi

if [ "$X$VALUEB"="$X" ];  then
 echo "You did not enter ending date.  Please try again $X$VALUEB  and $X"
 else
 echo "not empty"
fi

exit
---------------------------
The problem is that regardless whether or not I enter a value A or B, it always displays:
You did not enter beginning date.  Please try again 5r and 5
You did not enter ending date.  Please try again 5  and 5

("r" is a value I entered for the value A.  I used that as a debugging display).

Please assist.
Avatar of pavelmed
pavelmed

ASKER

Please note: in the code below, the displayed message is "You did not enter Value A"  and "You did not enter Value B".
I copied and pasted the code, so forgot to change that.
Thanks.
I meant, "in the code above" ...
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Much easier to do

if [ -z "$VALUEA" ]
then
      echo "You did not enter VALUE A"
else
     echo "VALUE A is set to $VALUEA"
fi

Open in new window


or if you want to reverse the logic

if [ -n "$VALUEA" ]
then
       echo "VALUE A is set to $VALUEA"
else
     echo "You did not enter VALUE A"
fi

Open in new window

Thank you very much ozo!