Error in if condition string comparison

Hello all! I need help in debugging following script. I have no idea where I am going wrong.

#!/bin/bash
for p1 in A1 TM MP
do
    for p2 in A1 TM MP
    do
 
  for mp1 in N1 N2
  do
   for mp2 in N1 N2
   do
    for mp3 in N1 N2
    do
     for mp4 in N1 N2
     do
      for mp5 in N1 N2
      do
       for mp6 in N1 N2
       do
        for mp7 in N1 N2
        do
         for mp8 in N1 N2
         do
 
          if [ "$mp1" == "N1" || "$mp2" == "N1" || "$mp3" == "N1" || "$mp4" == "N1" || "$mp5" == "N1" || "$mp6" == "N1" || "$mp7" == "N1" || "$mp8" == "N1" ]; then
           if [ "$mp1" == "N2" || "$mp2" == "N2" || "$mp3" == "N2" || "$mp4" == "N2" || "$mp5" == "N2" || "$mp6" == "N2" || "$mp7" == "N2" || "$mp8" == "N2" ]; then
 
            echo " $p1 $p2 $mp1 $mp2 $mp3 $mp4 $mp5 $mp6 $mp7 $mp8 "
           fi
          fi
 
         done
        done
       done
      done
     done
    done
   done
  done
 done
done

What I want to achieve is, "if" condition must be true only when at least one variable from $mp1 to $mp8 is N1 and at least one variable from $mp1 to $mp8 is N2.

The errors I am getting are:

./script.sh: line 25: N1: command not found
./script.sh: line 25: N2: command not found
./script.sh: line 25: [: missing `]'

Line 25 is the line of first "if" statement

Could you please tell me where I am going wrong. Sorry if the question is noobish. Thanks in advance! :slight_smile:

Your if loop is wrong. If you want to club more than one condition it should be done like this

if [ $a == 1 ] || [ $b == 2]; then...

Your code should look something like this

if [ "$mp1" == "N1" ] || [ "$mp2" == "N1" ] || [ "$mp3" == "N1" ] 
|| [ "$mp4" == "N1" ] || [ "$mp5" == "N1" ] || [ "$mp6" == "N1" ] 
|| [ "$mp7" == "N1" ] || [ "$mp8" == "N1" ]; then

Never checked your logic, am just correcting your syntax
regards,
Ahamed

1 Like

Also, the correct syntax requires a single = sign, not ==

1 Like

Thanks a ton guys for replying!

I made the changes and script works now. But now I'm getting following errors.

./script.sh: line 25: [: N1==: unary operator expected
./script.sh: line 25: [: N2==: unary operator expected
./script.sh: line 26: [: N1==: unary operator expected
./script.sh: line 26: [: N2==: unary operator expected

Script continues to run even after this. I'm not sure whether I should ignore this. What might be the reason for error?

Thanks! :slight_smile:

Make sure you have enough space in the if condition

Following code will fail

if ["$a" = "oops"];then

Correct it like this

if [ "$a" = "ahaa" ];then

regards,
Ahamed

1 Like

@ahamed,

Yeah, I have kept spaces after opening and before closing braces. Still this error keeps on popping up.

can you paste your code your executing?

regards,
Ahamed

It works now. Here is the script. But when I copy this into different file and execute it, it again gives the same error.

#!/bin/bash
 
flag=0
 
for p1 in A1 TM MP
do
    for p2 in A1 TM MP
    do
  if [ "$p1" = "A1" ] && [ "$p2" = "A1" ]; then
   flag=1
  elif [ "$p1" = "TM" ] && [ "$p2" = "TM" ]; then
   flag=1;
  elif [ "$p1" = "TM" ] && [ "$p2" = "A1" ]; then
   flag=1;
  elif [ "$p1" = "MP" ] && [ "$p2" = "MP" ]; then
   flag=1;
  elif [ "$p1" = "MP" ] && [ "$p2" = "A1" ]; then
   flag=1;
  elif [ "$p1" = "MP" ] && [ "$p2" = "TM" ]; then
   flag=1;
  fi
 
  if [ "$flag" -eq "1" ]; then
   flag=0
 
  for mp1 in N1 N2
  do
   for mp2 in N1 N2
   do
    for mp3 in N1 N2
    do
     for mp4 in N1 N2
     do
      for mp5 in N1 N2
      do
       for mp6 in N1 N2
       do
        for mp7 in N1 N2
        do
         for mp8 in N1 N2
         do
 
          if [ "$mp1" = "N1" ] || [ "$mp2" = "N1" ] || [ "$mp3" = "N1" ] || [ "$mp4" = "N1" ] || [ "$mp5" = "N1" ] || [ "$mp6" = "N1" ] || [ "$mp7" = "N1" ] || [ "$mp8" = "N1" ]; then
           if [ "$mp1" = "N2" ] || [ "$mp2" = "N2" ] || [ "$mp3" = "N2" ] || [ "$mp4" = "N2" ] || [ "$mp5" = "N2" ] || [ "$mp6" = "N2" ] || [ "$mp7" = "N2" ] || [ "$mp8" = "N2" ]; then
 
            echo " $p1 $p2 $mp1 $mp2 $mp3 $mp4 $mp5 $mp6 $mp7 $mp8 " >> Results.log
           fi
          fi
 
         done
        done
       done
      done
     done
    done
   done
  done
 
  fi
 
 done
done
echo Done.
 

BTW, I'm modifying the script on Windows machine and copying it over to Linux machine (must do like this for some reason). I also tried "dos2unix script.sh" but no use.'

Has it got something to do with ASCII and UTF-8 formatting and stuff :confused: