0

When i am comparing a blank variable with a string i am getting below error

[: !=: unary operator expected

Below is line in my script

if [ $monthlystatus = Completed ];then

command x

When i try to change this as below

if [ "$monthlystatus" = Completed ];then

command x

It does not give me expected result i.e. if statement give me wrong result. For example when i am using double quote and even the value of variable monthlystatus is equal to Completed but still command x is not executed.

Below is the real code .

if [ $monthlystatus != Failed ] && [ $monthlystatus != Aborted ];then

cat /home/a-hkataria/objectstatus_filesystem2.txt /home/a-hkataria/objectstatus_filesystem3.txt > /home/a-hkataria/objectstatus_filesystem4.txt

awk '$2 = $2 FS "Yes"'  /home/a-hkataria/objectstatus_filesystem4.txt

else

cat /home/a-hkataria/objectstatus_filesystem2.txt /home/a-hkataria/objectstatus_filesystem3.txt > /home/a-hkataria/objectstatus_filesystem4.txt

awk '$2 = $2 FS "No"'  /home/a-hkataria/objectstatus_filesystem4.txt

fi

So in case variable monthlystatus is blank it is giving me error and when i use the double quote even value of variable is Completed but still it is not displaying yes in second column.

16
  • 1
    "does not work" is not a problem description. Please read minimal reproducible example.
    – melpomene
    Commented Apr 2, 2017 at 13:00
  • Given a example Commented Apr 2, 2017 at 13:05
  • That's not an example (it's not complete or verifiable). Show real code.
    – melpomene
    Commented Apr 2, 2017 at 13:05
  • Updated question with real code. Commented Apr 2, 2017 at 13:19
  • 1
    @HiteshKataria It means variable monthlystatusXX is unset or contains a null string (unless set -e is active in which case not getting an error means it is set but contains a null string). Use the technique suggested by @CharlesDuffy, but it you really want to try expanding the string this way, add braces so that the shell uses the right variable name : echo "XX${monthlystatus}XX".
    – Fred
    Commented Apr 2, 2017 at 14:05

1 Answer 1

1

Your second approach is correct. Need quote the variable. Demo:

while read -r line; do
    [ "$line" = Completed ] && echo "true1 for =$line=" || echo "false1 for =$line="
    [[ "$line" == Completed ]] && echo "true2 for =$line=" || echo "false2 for =$line="
    [[ "$line" =~ ^Completed$ ]] && echo "true3 for =$line=" || echo "false3 for =$line="
    echo
done <<EOF
Completed
completed
Completediano
not Completed
notCompleted

etc
EOF

output

true1 for =Completed=
true2 for =Completed=
true3 for =Completed=

false1 for =completed=
false2 for =completed=
false3 for =completed=

false1 for =Completediano=
false2 for =Completediano=
false3 for =Completediano=

false1 for =not Completed=
false2 for =not Completed=
false3 for =not Completed=

false1 for =notCompleted=
false2 for =notCompleted=
false3 for =notCompleted=

false1 for ==
false2 for ==
false3 for ==

false1 for =etc=
false2 for =etc=
false3 for =etc=

Not the answer you're looking for? Browse other questions tagged or ask your own question.