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.
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.
ASKER
I meant, "in the code above" ...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Much easier to do
or if you want to reverse the logic
if [ -z "$VALUEA" ]
then
echo "You did not enter VALUE A"
else
echo "VALUE A is set to $VALUEA"
fi
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
ASKER
Thank you very much ozo!
ASKER
I copied and pasted the code, so forgot to change that.
Thanks.