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.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 1
    "does not work" is not a problem description. Please read [mcve]. – melpomene Apr 02 '17 at 13:00
  • Given a example – Hitesh Kataria Apr 02 '17 at 13:05
  • That's not an example (it's not complete or verifiable). Show real code. – melpomene Apr 02 '17 at 13:05
  • Updated question with real code. – Hitesh Kataria Apr 02 '17 at 13:19
  • That's still not complete/verifiable. Nothing in your code sets `monthlystatus` so how can it be `Complete`? – melpomene Apr 02 '17 at 13:21
  • Is it possible your `monthlystatus` variable contains extra characters (such as spaces) that would cause the comparison to fail? – Fred Apr 02 '17 at 13:21
  • I am printing the value of variable monthlystatus to verify its value before above code – Hitesh Kataria Apr 02 '17 at 13:26
  • @HiteshKataria, use `set -x` to log variables' values in an unambiguous way, or `printf 'monthlystatus=%q\n' "$monthlystatus"`. You can't trust `echo`. – Charles Duffy Apr 02 '17 at 13:27
  • @HiteshKataria, ...to be clear, the likely scenario here is that the *actual* value is something like `$'Completed\r'` or `'Completed '` instead of really being `Completed`. If you `echo "$monthlystatus"`, those two look exactly the same. This is why I suggested some better means of logging the actual value above. – Charles Duffy Apr 02 '17 at 13:32
  • See also http://stackoverflow.com/questions/951336/how-to-debug-a-bash-script – Charles Duffy Apr 02 '17 at 13:35
  • Let me try this please to check the if there is any space. Thanks – Hitesh Kataria Apr 02 '17 at 13:38
  • @CharlesDuffy when i use echo "XX$monthlystatusXX" it is printing XX only so what does it mean? – Hitesh Kataria Apr 02 '17 at 13:49
  • 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 Apr 02 '17 at 14:05
  • @Fred Yep bad suggestion on my part, didn't test it. – 123 Apr 02 '17 at 14:18
  • @HiteshKataria, I didn't suggest `echo "XX$monthlystatusXX"` -- that was someone else. I suggested either running your script with `bash -x`, or using `printf 'monthlystatus=%q\n' "$monthlystatus"`. – Charles Duffy Apr 02 '17 at 14:55
  • Hi Charles after removing the space from value of monthlystatus it work for me. Thanks for help. – Hitesh Kataria Apr 04 '17 at 13:29

1 Answers1

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=
clt60
  • 62,119
  • 17
  • 107
  • 194